从xtabs中的变量名访问数据帧

时间:2013-12-31 03:49:05

标签: r

问题很简单。我想通过xtabs函数构建交叉表。我还想以xtabs函数传递参数data=,其形式为从向量中拉出的字符串。

考虑以下MWE

dataframe_names <- c("DF1","DF2","DF3")
DF1 <- as.data.frame(UCBAdmissions)
xtabs(Freq ~ Gender + Admit, data=DF1)
# Admit
# Gender   Admitted Rejected
# Male       1198     1493
# Female      557     1278
dataframe_names[1]
# [1] "DF1"
xtabs(Freq ~ Gender + Admit, data=dataframe_names[1])
# Error in eval(predvars, data, env) : 
#   invalid 'envir' argument of type 'character'

我应该如何传递参数?

1 个答案:

答案 0 :(得分:1)

您需要使用get

xtabs(Freq ~ Gender + Admit, data=get(dataframe_names[1]))
#         Admit
# Gender   Admitted Rejected
#   Male       1198     1493
#   Female      557     1278