rmcorr: Pass column name with variable

Viewed 964

I am trying to use rmcorr R package for data analysis of repeated samples.
I want to make it go through all pairs of columns and estimate their correlation. The problem is rmcorr accepts only unquoted column names as arguments:

>> rmcorr(Subject, PacO2, pH, bland1995)

So, an expression like rmcorr(colnames(bland1995)[1], colnames(bland1995)[2], colnames(bland1995)[3], bland1995) throws an error:

Error in rmcorr(colnames(bland1995)[1], colnames(bland1995)[2], colnames(bland1995)[3],  : 
  'Measure 1' and 'Measure 2' must be numeric

So I hoped do.call would help me. Haha, fat cha

>> 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

How do I pass string variables to such a kind of function?

3 Answers

One way is to use 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

So far I've come up only with this ugly duckling:

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)
}

I wish there was a more concise way

Related