在需要时使用strsplit拆分字符向量与变量(R)中的所有观察值不一致

时间:2014-07-03 14:10:47

标签: r gsub strsplit

我的数据如下所示:

   duration                       obs   another
 1 1.801760     ID: 10 DAY: 6/10/13 S    orange
 2 1.868500     ID: 10 DAY: 6/10/13 S     green
 3 0.233562     ID: 10 DAY: 6/10/13 S    yellow
 4 5.538760       ID:96 DAY: 6/8/13 T    yellow
 5 3.436700       ID:96 DAY: 6/8/13 T      blue
 6 0.533856       ID:96 DAY: 6/8/13 T      pink
 7 2.302250       ID:96 DAY: 6/8/13 T    orange
 8 2.779420       ID:96 DAY: 6/8/13 T     green

我只包含了3个变量,但实际上我的数据有很多。我的问题是与丑陋的外观" obs"变量。我从另一个人那里收到了这些数据,这些人不一致地将这些信息输入到他们正在使用的软件中。

' OBS'包含三条信息: - id(ID:10,ID:96等) - 日期(M / D / Y) - 标识符(S或T)

我想拆分此信息并提取ID号(10或96),日期(例如6/8/13)和标识符(S或T)。

为此,我尝试使用strsplit进行以下操作:

temp<-strsplit(as.character(df$obs), " ")
mat<-matrix(unlist(temp), ncol=5, byrow=TRUE)

我认为这可以像我的真实数据一样工作,我有> 130,000个观察结果,而我没有意识到某些观察结果存在id没有空格的问题&#34; &#34;在&#34; ID之间:&#34;和号码。例如,在上面的数据中,&#34; ID:96&#34;冒号和数字之间没有空格。显然,我收到了这条警告信息:

Warning message:
  In matrix(unlist(temp), ncol = 5, byrow = TRUE) :
  data length [796454] is not a sub-multiple or multiple of the number of rows [159291]
显然,由于strsplit的输出有两种形式,因此strsplit不能被强制转换为常规列。

[1] "ID:"     "10"      "DAY:"    "6/10/13" "S"   #when there is whitespace
[1] "ID:96"  "DAY:"   "6/8/13" "T"   #when there isn't whitespace

为了尝试绕过这个,我做了这个,以为如果我可以在&#39; ID之后引入任何空格:&#39;它可以工作:

df$obs <- gsub("ID:", "ID: ", df$obs)

但是当我执行strsplit时,这并没有起作用,它会将双空格识别为分割数据的两个位置。

如果有人知道多个strsplits的解决方案,那么可以使用单独的列为idnumber,date,identifier强制转换回原始df,这将是很好的。

编辑:抱歉,忘了为可重现的示例添加数据:

df<-structure(list(duration = c(1.80176, 1.8685, 0.233562, 5.53876, 
                        3.4367, 0.533856, 2.30225, 2.77942), obs = structure(c(1L, 1L, 
                                                                               1L, 2L, 2L, 2L, 2L, 2L), .Label = c("ID: 10 DAY: 6/10/13 S", 
                                                                                                                   "ID:96 DAY: 6/8/13 T"), class = "factor"), another = structure(c(3L, 
                                                                                                                                                                                    2L, 5L, 5L, 1L, 4L, 3L, 2L), .Label = c("blue", "green", "orange", 
                                                                                                                                                                                                                            "pink", "yellow"), class = "factor")), .Names = c("duration", 
                                                                                                                                                                                                                                                                              "obs", "another"), class = "data.frame", row.names = c(NA, -8L
                                                                                                                                                                                                                                                                              ))

2 个答案:

答案 0 :(得分:6)

在您触发该数据输入人员后,我可能会在此处考虑使用正则表达式来捕获数据。首先,这里只是&#34; obs&#34;中的数据。列(在评论中添加附加值)

obs<-c("ID: 10 DAY: 6/10/13 S", "ID: 10 DAY: 6/10/13 S", "ID: 10 DAY: 6/10/13 S", 
"ID:96 DAY: 6/8/13 T", "ID:96 DAY: 6/8/13 T", "ID:96 DAY: 6/8/13 T", 
"ID:96 DAY: 6/8/13 T", "ID:96 DAY: 6/8/13 T", "ID: 84DAY: 6/8/13 T")

接下来,我可以使用

捕获数据
m<-regexpr("ID:\\s*(\\d+) ?DAY: (\\d+/\\d+/\\d+) (S|T)", obs, perl=T)

接下来,我使用辅助函数regcapturedmatches()来提取捕获的匹配项(它类似regmatches(),但对于捕获组)

do.call(rbind, regcapturedmatches(obs,m))

#      [,1] [,2]      [,3]
# [1,] "10" "6/10/13" "S" 
# [2,] "10" "6/10/13" "S" 
# [3,] "10" "6/10/13" "S" 
# [4,] "96" "6/8/13"  "T" 
# [5,] "96" "6/8/13"  "T" 
# [6,] "96" "6/8/13"  "T" 
# [7,] "96" "6/8/13"  "T" 
# [8,] "96" "6/8/13"  "T" 
# [9,] "84" "6/8/13"  "T"

返回值矩阵。然后,您可以根据自己的喜好处理这些字符值。您可以将它们转换为正确的类并附加到data.frame。

但如果您确实想要使用strsplit,则可以拆分&#34;:&#34;或带有&#34;之前的选项的空格:&#34;

do.call(rbind, strsplit(obs,"(:|:?\\s+)", obs))

#      [,1] [,2]    [,3]     [,4]      [,5]
# [1,] "ID" "10"    "DAY"    "6/10/13" "S" 
# [2,] "ID" "10"    "DAY"    "6/10/13" "S" 
# [3,] "ID" "10"    "DAY"    "6/10/13" "S" 
# [4,] "ID" "96"    "DAY"    "6/8/13"  "T" 
# [5,] "ID" "96"    "DAY"    "6/8/13"  "T" 
# [6,] "ID" "96"    "DAY"    "6/8/13"  "T" 
# [7,] "ID" "96"    "DAY"    "6/8/13"  "T" 
# [8,] "ID" "96"    "DAY"    "6/8/13"  "T" 
# [9,] "ID" "84DAY" "6/8/13" "T"       "ID"

直到你最新的坏数据系列为止。

答案 1 :(得分:3)

您也可以使用:

  read.table(text=gsub(":"," ", df$obs),header=F,stringsAsFactors=F)
  V1 V2  V3      V4 V5
# 1 ID 10 DAY 6/10/13  S
# 2 ID 10 DAY 6/10/13  S
# 3 ID 10 DAY 6/10/13  S
# 4 ID 96 DAY  6/8/13  T
# 5 ID 96 DAY  6/8/13  T
# 6 ID 96 DAY  6/8/13  T
# 7 ID 96 DAY  6/8/13  T
# 8 ID 96 DAY  6/8/13  T