rmcorr:使用变量传递列名
rmcorr: Pass column name with variable
我正在尝试使用rmcorr R包对重复样本进行数据分析。
我想让它遍历所有的列对并估计它们的相关性。问题是 rmcorr
只接受不带引号的列名作为参数:
>> rmcorr(Subject, PacO2, pH, bland1995)
所以,像 rmcorr(colnames(bland1995)[1], colnames(bland1995)[2], colnames(bland1995)[3], bland1995)
这样的表达式会抛出一个错误:
Error in rmcorr(colnames(bland1995)[1], colnames(bland1995)[2], colnames(bland1995)[3], :
'Measure 1' and 'Measure 2' must be numeric
所以我希望do.call
能帮到我。哈哈,肥茶
>> do.call('rmcorr', args = list(colnames(bland1995)[1], colnames(bland1995)[2], colnames(bland1995)[3], bland1995))
Error in rmcorr("Subject", "pH", "PacO2", list(Subject = c(1L, 1L, 1L, :
'Measure 1' and 'Measure 2' must be numeric
In addition: Warning message:
In rmcorr("Subject", "pH", "PacO2", list(Subject = c(1L, 1L, 1L, :
'Subject' coerced into a factor
如何将字符串变量传递给这种函数?
到目前为止我只想到了这只丑小鸭:
combs = combn(c(2:(ncol(bland1995))), 2)
for (i in c(1:ncol(combs))){
j = combs[1,i]
k = combs[2,i]
temp = bland1995[,c(colnames(bland1995)[j], colnames(bland1995)[k], 'Subject')]
colnames(temp) = c('tax1','tax2','Subject')
s = rmcorr(Subject, tax1, tax2, temp)
print(x)
}
希望有更简洁的方式
一种方法是使用 get
:
rmcorr(participant = get('Subject'),
measure1 = get('PacO2'),
measure2 = get('pH'),
bland1995)
Repeated measures correlation
r
-0.5067697
degrees of freedom
38
p-value
0.0008471081
95% confidence interval
-0.7112297 -0.223255
rmcorr 软件包的最新更新 (0.3.0) 现在应该允许这样做。列名仍然可以不带引号输入,但现在也可以使用字符串动态输入。更新后尝试您的示例:
rmcorr(colnames(bland1995)[1], colnames(bland1995)[2], colnames(bland1995)[3], bland1995)
Repeated measures correlation
r
-0.5067697
degrees of freedom
38
p-value
0.0008471081
95% confidence interval
-0.7112297 -0.223255
我正在尝试使用rmcorr R包对重复样本进行数据分析。
我想让它遍历所有的列对并估计它们的相关性。问题是 rmcorr
只接受不带引号的列名作为参数:
>> rmcorr(Subject, PacO2, pH, bland1995)
所以,像 rmcorr(colnames(bland1995)[1], colnames(bland1995)[2], colnames(bland1995)[3], bland1995)
这样的表达式会抛出一个错误:
Error in rmcorr(colnames(bland1995)[1], colnames(bland1995)[2], colnames(bland1995)[3], :
'Measure 1' and 'Measure 2' must be numeric
所以我希望do.call
能帮到我。哈哈,肥茶
>> do.call('rmcorr', args = list(colnames(bland1995)[1], colnames(bland1995)[2], colnames(bland1995)[3], bland1995))
Error in rmcorr("Subject", "pH", "PacO2", list(Subject = c(1L, 1L, 1L, :
'Measure 1' and 'Measure 2' must be numeric
In addition: Warning message:
In rmcorr("Subject", "pH", "PacO2", list(Subject = c(1L, 1L, 1L, :
'Subject' coerced into a factor
如何将字符串变量传递给这种函数?
到目前为止我只想到了这只丑小鸭:
combs = combn(c(2:(ncol(bland1995))), 2)
for (i in c(1:ncol(combs))){
j = combs[1,i]
k = combs[2,i]
temp = bland1995[,c(colnames(bland1995)[j], colnames(bland1995)[k], 'Subject')]
colnames(temp) = c('tax1','tax2','Subject')
s = rmcorr(Subject, tax1, tax2, temp)
print(x)
}
希望有更简洁的方式
一种方法是使用 get
:
rmcorr(participant = get('Subject'),
measure1 = get('PacO2'),
measure2 = get('pH'),
bland1995)
Repeated measures correlation
r
-0.5067697
degrees of freedom
38
p-value
0.0008471081
95% confidence interval
-0.7112297 -0.223255
rmcorr 软件包的最新更新 (0.3.0) 现在应该允许这样做。列名仍然可以不带引号输入,但现在也可以使用字符串动态输入。更新后尝试您的示例:
rmcorr(colnames(bland1995)[1], colnames(bland1995)[2], colnames(bland1995)[3], bland1995)
Repeated measures correlation
r
-0.5067697
degrees of freedom
38
p-value
0.0008471081
95% confidence interval
-0.7112297 -0.223255