我想我正在为rbind.fill
寻找plyr
(在Hadley的cbind
包中)的模拟。我看了,但没有cbind.fill
。
我想做的是以下内容:
#set these just for this example
one_option <- TRUE
diff_option <- TRUE
return_df <- data.frame()
if (one_option) {
#do a bunch of calculations, produce a data.frame, for simplicity the following small_df
small_df <- data.frame(a=1, b=2)
return_df <- cbind(return_df,small_df)
}
if (diff_option) {
#do a bunch of calculations, produce a data.frame, for simplicity the following small2_df
small2_df <- data.frame(l="hi there", m=44)
return_df <- cbind(return_df,small2_df)
}
return_df
可以理解,这会产生错误:
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 0, 1
我目前的解决方法是将行return_df <- data.frame()
替换为return_df <- data.frame(dummy=1)
,然后代码才有效。然后,我只是从最后的return_df
删除虚拟。添加假人并运行上面的代码后,我得到了
dummy a b l m
1 1 1 2 hi there 44
然后我只需要摆脱假人,例如:
> return_df[,2:ncol(return_df)]
a b l m
1 1 2 hi there 44
我确信我错过了一种更简单的方法。
编辑:我想我不是在寻找一个cbind.fill,因为这意味着在cbind之后会创建一个NA值,这不是我想要的。
答案 0 :(得分:41)
这是一个cbind fill:
cbind.fill <- function(...){
nm <- list(...)
nm <- lapply(nm, as.matrix)
n <- max(sapply(nm, nrow))
do.call(cbind, lapply(nm, function (x)
rbind(x, matrix(, n-nrow(x), ncol(x)))))
}
我们试一试:
x<-matrix(1:10,5,2)
y<-matrix(1:16, 4,4)
z<-matrix(1:12, 2,6)
cbind.fill(x,y)
cbind.fill(x,y,z)
cbind.fill(mtcars, mtcars[1:10,])
我想我是从某个地方偷走过的。
从这里编辑STOLE:LINK
答案 1 :(得分:11)
虽然我认为Tyler的解决方案是直接的,也是最好的,我只是提供另一种方式,使用我们已经拥有的rbind.fill()
。
require(plyr) # requires plyr for rbind.fill()
cbind.fill <- function(...) {
transpoted <- lapply(list(...),t)
transpoted_dataframe <- lapply(transpoted, as.data.frame)
return (data.frame(t(rbind.fill(transpoted_dataframe))))
}
答案 2 :(得分:3)
使用rowr::cbind.fill
rowr::cbind.fill(df1,df2,fill = NA)
A B
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 NA 6
答案 3 :(得分:1)
install.packages("qpcR")
library(qpcR)
qpcR:::cbind.na(1, 1:7)
答案 4 :(得分:0)
我建议修改泰勒的答案。我的函数允许cbind
- 使用向量的data.frames和/或矩阵而不会丢失列名称,就像在Tyler的解决方案中发生的那样
cbind.fill <- function(...){
nm <- list(...)
dfdetect <- grepl("data.frame|matrix", unlist(lapply(nm, function(cl) paste(class(cl), collapse = " ") )))
# first cbind vectors together
vec <- data.frame(nm[!dfdetect])
n <- max(sapply(nm[dfdetect], nrow))
vec <- data.frame(lapply(vec, function(x) rep(x, n)))
if (nrow(vec) > 0) nm <- c(nm[dfdetect], list(vec))
nm <- lapply(nm, as.data.frame)
do.call(cbind, lapply(nm, function (df1)
rbind(df1, as.data.frame(matrix(NA, ncol = ncol(df1), nrow = n-nrow(df1), dimnames = list(NULL, names(df1))))) ))
}
cbind.fill(data.frame(idx = numeric()), matrix(0, ncol = 2),
data.frame(qwe = 1:3, rty = letters[1:3]), type = "GOOD", mark = "K-5")
# idx V1 V2 qwe rty type mark
# 1 NA 0 0 1 a GOOD K-5
# 2 NA NA NA 2 b GOOD K-5
# 3 NA NA NA 3 c GOOD K-5
答案 5 :(得分:0)
我们可以添加 id 列,然后使用 merge :
df1 <- mtcars[1:5, 1:2]
# mpg cyl id
# Mazda RX4 21.0 6 1
# Mazda RX4 Wag 21.0 6 2
# Datsun 710 22.8 4 3
# Hornet 4 Drive 21.4 6 4
# Hornet Sportabout 18.7 8 5
df2 <- mtcars[6:7, 3:4]
# disp hp
# Valiant 225 105
# Duster 360 360 245
#Add id column then merge
df1$id <- seq(nrow(df1))
df2$id <- seq(nrow(df2))
merge(df1, df2, by = "id", all.x = TRUE, check.names = FALSE)
# id mpg cyl disp hp
# 1 1 21.0 6 225 105
# 2 2 21.0 6 360 245
# 3 3 22.8 4 NA NA
# 4 4 21.4 6 NA NA
# 5 5 18.7 8 NA NA
答案 6 :(得分:0)
当a
和b
是数据帧时,以下应该可以正常工作:
ab <- merge(a, b, by="row.names", all=TRUE)[,-1]
或其他可能性:
rows <- unique(c(rownames(a), rownames(b)))
ab <- cbind(a[rows ,], b[rows ,])
答案 7 :(得分:0)
我们可以使用list
代替data.frame
,最后将其转换为data.frame
。例如:
df = list()
df2 = data.frame(col1 = 1:3, col2 = c('a','b','c'))
df = as.data.frame(cbind(df, as.matrix(df2)))
df
# col1 col2
# 1 1 a
# 2 2 b
# 3 3 c