0

我的数据如下所示:

   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)、日期(例如 2013 年 6 月 8 日)和标识符(S 或 T)。

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

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

我认为这会在我的真实数据中起作用,我有超过 130,000 个观察结果,但我没有意识到某些观察结果存在问题,即 id 在“ID:”和数字之间没有空格“”。例如,在上面的数据中,“ID:96”在冒号和数字之间没有空格。显然,我收到了这条警告信息:

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

为了解决这个问题,我这样做了,认为如果我可以在“ID:”之后引入任何空格,它可以工作:

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

但这并没有像我然后进行 strsplit 时那样工作,它会将双空格识别为拆分数据的两个位置。

如果有人知道多个 strsplits 的解决方案,然后可以将其强制回原始 df,并为 idnumber、date、identifier 单独列,那就太好了。

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

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
                                                                                                                                                                                                                                                                              ))
4

2 回答 2

6

在你解雇那个数据输入人员之后,我可能会考虑在这里使用正则表达式来捕获数据。首先,这只是“obs”列中的数据(在您的评论中添加附加值)

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。

但是,如果您确实想使用 a strsplit,则可以拆分为“:”或带有“:”前面的选项的空格

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"

直到您最新的新坏数据行为止。

于 2014-07-03T14:21:01.057 回答
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
于 2014-07-03T15:39:36.170 回答