Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试构建一个函数,该函数创建一个 data.frame 仅包含0两个变量,指示行名和列名(同时它还指示数据框的大小)
0
> mycolnames = c('a','b','c') > myrownames = c('e','f')
对于这种特殊情况,我的 data.frame 应该是:
df = data.frame(a=rep(0,2), b=rep(0,2), c=rep(0,2)) rownames(df) = myrownames
使用matrix并包装它data.frame(如果你真的需要 adata.frame作为输出)。
matrix
data.frame
data.frame(matrix(0, ncol = length(mycolnames), nrow = length(myrownames), dimnames = list(myrownames, mycolnames))) # a b c # e 0 0 0 # f 0 0 0