拆分数据框的列,然后将它们重新组合为一列而没有空白

时间:2018-07-26 10:47:06

标签: r dataframe split

我有两个要基于“;”拆分的数据框,然后将得到的列重新组合为具有相应值的一列。

检查以下示例。 df2中的每个数字都对应于df1中的字符串(“ aaa” <-> 111,“ bbbb” <-> 2222,“ ccc” <-> 333等)

#The dataframes I have
df1 = c("aaa","bbbb;ccc","dd;eeee;ffff","gg") #1st dataframe
df2 = c("111","2222;333","44;5555;6666","77") #2nd dataframe
df = as.data.frame(cbind(df1,df2)) #combine df1 and df2

#The output I'm trying to achieve
df1_desired = c("aaa","bbbb","ccc","dd","eeee","ffff","gg")
df2_desired = c("111","2222","333","44","5555","6666","77")
df_desired = as.data.frame(cbind(df1_desired,df2_desired)) #this is the format I want

我尝试了以下操作,但没有给我我所需的安排。

split_df1 = str_split_fixed(df$df1, ";", 3)
split_df2 = str_split_fixed(df$df2, ";", 3)
combined_output = cbind(split_df1 ,split_df2 )

将感谢您的建议!

更新

@snoram提供的此解决方案非常适合我:

library(data.table)
setDT(df)
dfd <- df[, lapply(.SD, tstrsplit, ";"), by = seq_len(nrow(df))][, seq_len := NULL]
dfd

2 个答案:

答案 0 :(得分:0)

> data.frame(cbind(unlist(strsplit(df1,";")),unlist(strsplit(df2,";"))))
    X1   X2
1  aaa  111
2 bbbb 2222
3  ccc  333
4   dd   44
5 eeee 5555
6 ffff 6666
7   gg   77

首先根据“;”分割文本,然后将其取消列出,将两个结果绑定并转换为数据框。

答案 1 :(得分:0)

library(data.table)
setDT(df)
dfd <- df[, lapply(.SD, tstrsplit, ";"), by = seq_len(nrow(df))][, seq_len := NULL]
dfd
    df1  df2
1:  aaa  111
2: bbbb 2222
3:  ccc  333
4:   dd   44
5: eeee 5555
6: ffff 6666
7:   gg   77

受R启发的Base R:

data.frame(lapply(lapply(df, strsplit, ";"), unlist))