2

这是我之前问过的这个问题的后续:R for loop: create a new column with the count of a sub str from a different column

我有一张大桌子(100+ 列,50k+ 行)。其中一列包含以下格式的数据:

col
chicken
chicken,goat
cow,chicken,goat
cow

我想去:

col         col2         col3
chicken
chicken     goat
cow         chicken      goat
cow

需要填充的列不止 3 列,我只是将其剥离为示例。我的脚本创建了要填充的适当数量的列,我只需要代码,我假设它是一个 for 循环,将字符串拆分为 'col' on ',',然后将拆分字符串放入后续列中。

谢谢你的帮助!

4

2 回答 2

11
read.table(text="chicken
 chicken,goat
 cow,chicken,goat
 cow", fill=TRUE, sep=",")
# Trivial to change the names of dataframe columns
        V1      V2   V3
1  chicken             
2  chicken    goat     
3      cow chicken goat
4      cow       
于 2013-09-18T21:53:45.503 回答
3

你可以试试这个:

library(splitstackshape)
concat.split(data = df, split.col = 1, sep = ",", drop = TRUE)

#     col_1   col_2 col_3
# 1 chicken              
# 2 chicken    goat      
# 3     cow chicken  goat
# 4     cow 
于 2013-09-18T21:53:06.217 回答