list 将列添加到 R 中的空数据框中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26684072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add Columns to an empty data frame in R
提问by Michal
I have searched extensively but not found an answer to this question on Stack Overflow.
我已经广泛搜索,但没有在 Stack Overflow 上找到这个问题的答案。
Lets say I have a data frame a.
假设我有一个数据框 a。
I define:
我定义:
a <- NULL
a <- as.data.frame(a)
If I wanted to add a column to this data frame as so:
如果我想在此数据框中添加一列,如下所示:
a$col1 <- c(1,2,3)
I get the following error:
我收到以下错误:
Error in `$<-.data.frame`(`*tmp*`, "a", value = c(1, 2, 3)) :
replacement has 3 rows, data has 0
Why is the row dimension fixed but the column is not?
为什么行维是固定的,而列不是?
How do I change the number of rows in a data frame?
如何更改数据框中的行数?
If I do this (inputting the data into a list first and then converting to a df), it works fine:
如果我这样做(首先将数据输入列表,然后转换为 df),它工作正常:
a <- NULL
a$col1 <- c(1,2,3)
a <- as.data.frame(a)
回答by ctbrown
The row dimension is not fixed, but data.frames are stored as list of vectors that are constrained to have the same length. You cannot add col1
to a
because col1
has three values (rows) and a
has zero, thereby breaking the constraint. R does not by default auto-vivify values when you attempt to extend the dimension of a data.frame by adding a column that is longer than the data.frame. The reason that the second example works is that col1
is the only vector in the data.frame so the data.frame is initialized with three rows.
行维度不固定,但 data.frames 存储为限制为具有相同长度的向量列表。您不能添加col1
到a
因为col1
具有三个值(行)并且a
为零,从而破坏了约束。当您尝试通过添加比 data.frame 长的列来扩展 data.frame 的维度时,R 默认情况下不会自动激活值。第二个例子起作用的原因是它col1
是 data.frame 中唯一的向量,所以 data.frame 被初始化为三行。
If you want to automatically have the data.frame expand, you can use the following function:
如果要自动让 data.frame 展开,可以使用以下函数:
cbind.all <- 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)))))
}
This will fill missing values with NA
. And you would use it like: cbind.all( df, a )
这将用NA
. 你会像这样使用它:cbind.all( df, a )
回答by user2820516
You could also do something like this where I read in data from multiple files, grab the column I want, and store it in the dataframe. I check whether the dataframe has anything in it, and if it doesn't, create a new one rather than getting the error about mismatched number of rows:
你也可以做这样的事情,我从多个文件中读取数据,获取我想要的列,并将其存储在数据框中。我检查数据帧中是否有任何内容,如果没有,则创建一个新数据帧,而不是收到有关行数不匹配的错误:
readCounts = data.frame()
for(f in names(files)){
d = read.table(files[f], header=T, as.is=T)
d2 = round(data.frame(d$NumReads))
colnames(d2) = f
if(ncol(readCounts) == 0){
readCounts = d2
rownames(readCounts) = d$Name
} else{
readCounts = cbind(readCounts, d2)
}
}