如何strsplit数据框列和相应的复制行?

enr*_*ero 6 split r dataframe melt reshape2

我有一个这样的数据框:

> df <- data.frame(Column1=c("id1", "id2", "id3"), Column2=c("text1,text2,text3", "text4", "text5,text6"), Column3=c("text7", "text8,text9,text10,text11", "text12,text13"))

> df
  Column1           Column2                   Column3
1     id1 text1,text2,text3                     text7
2     id2             text4 text8,text9,text10,text11
3     id3       text5,text6             text12,text13
Run Code Online (Sandbox Code Playgroud)

如何以这种格式转换它?

  Column1 variable                     value
1     id1  Column2                     text1
2     id1  Column2                     text2
3     id1  Column2                     text3
4     id2  Column2                     text4
5     id3  Column2                     text5
6     id3  Column2                     text6
7     id1  Column3                     text7
8     id2  Column3                     text8
9     id2  Column3                     text9
10    id2  Column3                    text10
11    id2  Column3                    text11
12    id3  Column3                    text12
13    id3  Column3                    text13
Run Code Online (Sandbox Code Playgroud)

我想第一步是melt()数据框(顺便说一下,我应该担心这个警告吗?):

> library(reshape2)    
> mdf <- melt(df, id.vars="Column1", measure.vars=c("Column2", "Column3"))
> mdf
  Column1 variable                     value
1     id1  Column2         text1,text2,text3
2     id2  Column2                     text4
3     id3  Column2               text5,text6
4     id1  Column3                     text7
5     id2  Column3 text8,text9,text10,text11
6     id3  Column3             text12,text13
Warning message:
attributes are not identical across measure variables; they will be dropped
Run Code Online (Sandbox Code Playgroud)

然后我基本上需要``strsplit()`'value'列并相应地复制行,但我想不出办法来做到这一点.

> strsplit(mdf$value, ",")
[[1]]
[1] "text1" "text2" "text3"

[[2]]
[1] "text4"

[[3]]
[1] "text5" "text6"

[[4]]
[1] "text7"

[[5]]
[1] "text8"  "text9"  "text10" "text11"

[[6]]
[1] "text12" "text13"
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏!谢谢.

Jaa*_*aap 6

一个data.table解决方案:

library(data.table)
mdt <- melt(setDT(df), id.vars="Column1")[,strsplit(as.character(value),",",fixed=TRUE),
                                          by=list(Column1,variable)]
Run Code Online (Sandbox Code Playgroud)

结果:

> mdt
    Column1 variable     V1
 1:     id1  Column2  text1
 2:     id1  Column2  text2
 3:     id1  Column2  text3
....
Run Code Online (Sandbox Code Playgroud)

您还可以使用最新版本(v1.9.5 +)中的tstrsplit函数来保留列的名称,而不是将其重命名为:data.tablevalueV1

mdt <- melt(setDT(df), id.vars="Column1")[,lapply(.SD, function(x) tstrsplit(x, ",", fixed=TRUE)),
                                          by=list(Column1,variable)]
Run Code Online (Sandbox Code Playgroud)

结果:

> mdt
    Column1 variable  value
 1:     id1  Column2  text1
 2:     id1  Column2  text2
 3:     id1  Column2  text3
....
Run Code Online (Sandbox Code Playgroud)

使用dplyr&的替代解决方案tidyr:

library(dplyr)
library(tidyr)
mdf <- df %>% gather(variable, value, -Column1) %>% 
  transform(value = strsplit(as.character(value),",")) %>%
  unnest(value)
Run Code Online (Sandbox Code Playgroud)

结果:

> mdf
   Column1 variable  value
1      id1  Column2  text1
2      id1  Column2  text2
3      id1  Column2  text3
....
Run Code Online (Sandbox Code Playgroud)

使用最新版本tidyr,您还可以使用separate_rows-function:

mdf <- df %>% 
  gather(variable, value, -Column1) %>% 
  separate_rows(value)
Run Code Online (Sandbox Code Playgroud)


akr*_*run 4

你可以尝试:

 library(reshape2)
Run Code Online (Sandbox Code Playgroud)

cSplit来自https://gist.github.com/mrdwab/11380733

 cSplit(melt(df, id.vars="Column1"), "value", ",", "long")
 #      Column1 variable  value
 # 1:     id1  Column2  text1
 # 2:     id1  Column2  text2
 # 3:     id1  Column2  text3
 # 4:     id2  Column2  text4
 # 5:     id3  Column2  text5
 # 6:     id3  Column2  text6
 # 7:     id1  Column3  text7
 # 8:     id2  Column3  text8
 # 9:     id2  Column3  text9
 #10:     id2  Column3 text10
 #11:     id2  Column3 text11
 #12:     id3  Column3 text12
 #13:     id3  Column3 text13
Run Code Online (Sandbox Code Playgroud)

或者,如果想坚持使用 CRAN 包中提供的功能:

library(reshape2)
library(splitstackshape)
library(dplyr)
select(na.omit(concat.split.multiple(melt(df, id.vars="Column1"), split.col="value", sep=",", direction="long")), -time)
Run Code Online (Sandbox Code Playgroud)