让我说清楚,我不想添加,乘法,减法或除法数据。我希望新列包含第一列和第二列的所有信息。这是我的意思的一个例子。
data 1 data 2 new data
1 Q 1 Q
2 T 5 T
3 R 3 R
4 1
5 5
6 3
我所看到的值并不是绝对的,但我以它们为例来说明差异。
答案 0 :(得分:1)
cbind.fill <- function(...){
# From a SO answer by Tyler Rinker
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)))))
}
df1 <- data.frame("data 1"=c("Q","T","R"),"data 2"=c(1,5,3), stringsAsFactors = F)
df2 <- cbind.fill(df1, c(df1$data.1, df1$data.2))
colnames(df2) <- c(colnames(df2)[1:2], "new data")
df2
data.1 data.2 new data [1,] "Q" "1" "Q" [2,] "T" "5" "T" [3,] "R" "3" "R" [4,] NA NA "1" [5,] NA NA "5" [6,] NA NA "3"
cbind.fill
函数的来源:cbind a df with an empty df (cbind.fill?)