问题很简单。我想通过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'
我应该如何传递参数?
答案 0 :(得分:1)
您需要使用get
:
xtabs(Freq ~ Gender + Admit, data=get(dataframe_names[1]))
# Admit
# Gender Admitted Rejected
# Male 1198 1493
# Female 557 1278