r - 如何在R中的“data $”之后动态分配变量名
问题描述
我想分配一个变量“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)
解决方案
以下代码似乎有效:
# 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)
我使用 $ 和列名向量从动态选择数据框列中推断出 这一点。
推荐阅读
- arrays - 不确定如何将此宏转换为使用多维数组而不是使用工作表比较
- java - 不显示在 html 中
- html - CSS - 表格数据不在固定表格标题下 - 溢出-X
- php - 在白色背景上打印一个 php 变量,然后停止执行
- sql - 当所有值都匹配一行时如何选择数据
- c++ - 在 C++ 中的一行中初始化多个变量
- android - 被谷歌保护阻止
- php - 使用 api.php 部署 laravel 项目后出现 NotFoundHttpException,为什么?
- javascript - Javascript:用闭包概括反应可加载
- django - 无法将日期选择器小部件中的日期保存到我的数据库中