我有一个维度[1] 1 11
和[1] 3 29
的数据。我正在尝试将这两个数据绑定,这样只有一行的dataframe将被三次复制到结果的dataframe中。
当我使用cbind时,它有时会工作,但会将错误抛出
Data.frame中的错误(.,check.names = FALSE):参数意味着不同的行数: 1,3
> dim(a)
[1] 1 11
> dim (b)
[1] 3 29
> cbind(a,b)
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 1, 3
然而,如果我尝试减法,它是有效的。
> cbind(a[1:10],b) #Works Fine
> cbind(a[1:11],b) #Throws Error
注意:它有时会工作,但是如果我再次运行代码,它就不能工作了。
谢谢
发布于 2017-10-09 05:33:40
发布于 2017-10-09 05:53:50
如果您使它们成为数据表,那么您就不再有错误了。
cbind(iris[1:10, 1], iris[ 1:11,2:3]) # error
cbind(iris[1:10, 1], iris[ 1:11,2]) # warning message, missing values are copied
cbind(iris[1:10, 1], iris[ 1:11,2:3] %>% as.data.table()) # same
cbind(iris[1:10, 1] %>% as.data.table(), iris[ 1:11,2:3] )# same
. Sepal.Width Petal.Length
1: 5.1 3.5 1.4
2: 4.9 3.0 1.4
3: 4.7 3.2 1.3
4: 4.6 3.1 1.5
5: 5.0 3.6 1.4
6: 5.4 3.9 1.7
7: 4.6 3.4 1.4
8: 5.0 3.4 1.5
9: 4.4 2.9 1.4
10: 4.9 3.1 1.5
11: 5.1 3.7 1.5
# you can also make the extra values as zero or NA etc
temp <- iris[1:10,1] %>% as.data.table()
temp <- temp[match(rownames(iris[ 1:11,2:3]), rownames(temp[1:10, 1]))]
# temp[is.na(temp)] <- ""
cbind(temp %>% as.data.table(), iris[ 1:11,2:3] )
. Sepal.Width Petal.Length
1: 5.1 3.5 1.4
2: 4.9 3.0 1.4
3: 4.7 3.2 1.3
4: 4.6 3.1 1.5
5: 5.0 3.6 1.4
6: 5.4 3.9 1.7
7: 4.6 3.4 1.4
8: 5.0 3.4 1.5
9: 4.4 2.9 1.4
10: 4.9 3.1 1.5
11: NA 3.7 1.5
https://stackoverflow.com/questions/46647670
复制相似问题