如何在R中的“ data $”之后动态分配变量名

时间:2018-06-21 19:15:31

标签: r

我想分配一个变量“ dv1”,该变量会将给定的变量名称传递给公式,以便创建动态数据引用。

代码

# Define terms
esto1<-g6e ### temporarily stored variable #1
esto2<-g6c ### temporarily stored variable #2, etc. 
dv1<-"attitude"     ### The dependent variable that you'll be testing
# 
shapiro.test(esto1$dv1)
shapiro.test(esto2$dv1)

# Given esto1=g6e, esto2=g6c, and dv1=attitude, the statements above should be the equivalent of: 
shapiro.test(g6e$attitude)
shapiro.test(g6c$attitude)

1 个答案:

答案 0 :(得分:-1)

以下代码似乎可以正常工作:

  # Define terms
esto1<-g6e ### temporarily stored variable #1
esto2<-g6c ### temporarily stored variable #2, etc. 
dv1<-"attitude"     ### The dependent variable that you'll be testing

# attempt 2
shapiro.test(esto1[[dv1]])
shapiro.test(esto2[[dv1]])

# Given esto1=g6e, esto2=g6c, and dv1=attitude, the statements above should be the equivalent of: 
shapiro.test(g6e$attitude)
shapiro.test(g6c$attitude)

我从 Dynamically select data frame columns using $ and a vector of column names来解决这个问题。