我有一个数据框,其中包含一个列,我想在其中放置多个值(每条记录可以有不同的值数量(。
我正在尝试使用 for 循环将数据附加到它,但它一直给出错误:
Error in `[<-.data.frame`(`*tmp*`, 1, "piet", value = c(1, 2)) :
replacement has 2 rows, data has 1
是否无法覆盖/追加到此列?小示例代码:
a = data.frame(piet=numeric())
a[1,'piet'] = c(1)
a[1,'piet'] = c(1,2) # this throws the error
我意识到有比使用 for 循环更好的方法,但我的数据有点随机分布在多个.csv文件中(其中一些有重叠的记录,我试图在这里合并其结果(。
好吧,
这真的很奇怪,但它可以使用list(c())
(感谢@Frank(和非常具体的编码格式:
a = data.frame(piet=list()) #initialize data frame
a[1,'piet'] = '' # create first row
a$piet[c(T)] = list(c(1,2,3)) # overwrite with list
a$piet[c(T)] = list(c(4,5,6)) # overwrite with new list
a$piet[c(T)] = list(c(unlist(a[1,'piet']),8,9)) # append to existing list
A的最终内容:
> a
piet
1 4, 5, 6, 8, 9
请注意,a$piet[row selection]
是强制性的,并且使用list(c())
!