如何在R中设置占位符

6rqinv9w  于 2023-01-10  发布在  其他
关注(0)|答案(2)|浏览(249)
df1 <- 
structure(c(3L, NA, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 3L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 1L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 2L, 1L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 1L, 3L, 
2L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 2L, 3L, 
3L, 1L, 3L, 2L, 2L, 3L, 2L, 3L, 1L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 
3L, 3L, 2L, 1L, 2L), levels = c("aaa", "bbb", 
"ccc"), class = c("ordered", "factor"))

df2 <-
structure(c(1L, NA, 3L, 1L, 1L, 3L, 2L, 1L, 3L, 2L, 2L, 3L, 1L, 
1L, 2L, 3L, 1L, 3L, 2L, 2L, 1L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 3L, 
1L, 2L, 1L, 3L, 2L, 3L, 1L, 2L, 3L, 2L, 3L, 2L, 1L, 3L, 3L, 3L, 
2L, 2L, 1L, 3L, 2L, 1L, 2L, 3L, 2L, 2L, 3L, 2L, 2L, 3L, 2L, 3L, 
1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 3L, 3L, 3L, 1L, 2L, 1L, 2L, 2L, 
3L, 3L, 1L, 3L, 3L), levels = c("aaa", "bbb", 
"ccc"), class = c("ordered", "factor"))

df3 <- 
structure(c(3L, 2L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
2L, 3L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 
3L, 1L, 3L, 2L, 2L, 3L, 3L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 
1L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 2L, 3L, 3L, 3L, 3L, 3L, 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")

我想把df1df2df3(在我的示例中是因子)放在一起作为占位符vars2use

var2use <- c(var1, var2, var3)

然后,应将此占位符与其他因子(dftest1dftest2)组合成一个数据集。
此实现按预期工作:

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

背景是我想在不同的地方使用这种类型的占位符,有人知道如何解决这个问题吗?

inb24sb2

inb24sb21#

更直接的方法是把你的值放到一个单独的data.frame中,然后在你需要的时候把值放进cbind()中。

vars2use <- data.frame(df1, df2, df3)
df <- cbind(data.frame(dftest1, dftest2), vars2use)
# df <- data.frame(dftest1, dftest2) |> cbind(vars2use) # alternative syntax

如果出于某种原因,这些名称确实需要成为字符向量,则可以在使用cbind之前使用mget()获取值

varnames <- c("df1", "df2", "df3")
df <- cbind(data.frame(dftest1, dftest2), mget(varnames))
j8yoct9x

j8yoct9x2#

假设dftest1dftest2都是大小与(var 1-var 3)相等的 Dataframe ,那么根据下面的代码,两者都能完全正常工作。
我认为您的示例中的问题在于您调用的是df 1-df 3,而您的对象却被称为var 1、var 2和var 3
然而,由于您没有在示例中显示抛出了什么错误,因此我不确定我们应该如何得出正确的结论。

var1 <- "df1" 
var2 <- "df2" 
var3 <- "df3" 
var2use <- c(var1, var2, var3)

dftest1 <- "df4"
dftest2 <- "df5"

df <- data.frame(dftest1, dftest2, var1, var2, var3)

  
df <- data.frame(dftest1, dftest2, var2use)

相关问题