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L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 2L, 3L, 3L), levels = c("ddd", "eee", "fff"
), class = c("ordered", "factor"))
dftest1 <-
structure(c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 1L, NA, NA, NA, 1L, NA, NA, NA, 2L, NA, NA, NA,
1L, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 1L, NA, 2L), levels = c("AAA", "BBB"
), class = "factor")
dftest2 <-
structure(c(NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, 1L, NA,
1L, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA,
NA, NA, 1L, 1L, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L,
NA, NA, NA, NA, 1L, NA, NA, NA, 1L, NA, 1L, NA, 1L, NA, NA, NA,
1L, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 1L, 1L, 1L), levels = "CCC", class = "factor")
我想把df1
、df2
和df3
(在我的示例中是因子)放在一起作为占位符vars2use
。
var2use <- c(var1, var2, var3)
然后,应将此占位符与其他因子(dftest1
、dftest2
)组合成一个数据集。
此实现按预期工作:
df <- data.frame(dftest1, dftest2, df1, df2, df3)
我希望它也能以这种形式实现:
df <- data.frame(dftest1, dftest2, var2use)
但我得到一个错误:
Error in data.frame(dftest1, dftest2, var2use):
arguments imply differing number of rows: 82, 3
背景是我想在不同的地方使用这种类型的占位符,有人知道如何解决这个问题吗?
2条答案
按热度按时间inb24sb21#
更直接的方法是把你的值放到一个单独的data.frame中,然后在你需要的时候把值放进
cbind()
中。如果出于某种原因,这些名称确实需要成为字符向量,则可以在使用
cbind
之前使用mget()
获取值j8yoct9x2#
假设
dftest1
和dftest2
都是大小与(var 1-var 3)相等的 Dataframe ,那么根据下面的代码,两者都能完全正常工作。我认为您的示例中的问题在于您调用的是df 1-df 3,而您的对象却被称为var 1、var 2和var 3
然而,由于您没有在示例中显示抛出了什么错误,因此我不确定我们应该如何得出正确的结论。