简体   繁体   中英

How to use a string as a formula in r

I'm trying to do an ANOVA of all of my data frame columns against time_of_day which is a factor. The rest of my columns are all doubles and of equal length.

x = 0
pdf("Time_of_Day.pdf")
for (i in names(data_in)){
  if(x > 9){
    test <- aov(paste(i, "~ time_of_day"), data = data_in)
  }
  x = x+1
}
dev.off()

Running this code gives me this error:

Error: $ operator is invalid for atomic vectors

Where is my code calling $ ? How can I fix this? Sorry, I'm new to r and am quite lost.

My research question is to see if time of day has an affect on brain volume at different ROIs in the brain. Time of day is divided into three categories of morning, afternoon or night.

Edit: SOLVED treating the string as a formula will allow this to run although I have been advised to not have this many independent values as it will inflate the statistical results of the model. I am not removing this incase someone has a similar problem with the aov() call.

x = 0
pdf("Time_of_Day.pdf")
for (i in names(data_in)){
  if(x > 9){
    test <- aov(as.formula(paste(i, "~ time_of_day")), data = data_in)
  }
  x = x+1
}
dev.off()

I guess your problem is that you don't have an ANOVA formula integrated into your aov() function. See the following working example:

data_in <- data.frame(c(1,2,3),c(4,5,6),c(7,8,9))
names(data_in) <- c("first","second","third")

for (i in seq_along(names(data_in))){
  test <- aov(data_in$first ~ data_in$second, data = data_in)
  print(summary(test))
}

However, it seems that you tried to calculate an ANOVA for each column, whereas you need at least two variables. That is, a nominal scaled condition variable and an interval scaled dependent variable (eg gender and weight). So I'm generally wondering if an ANOVA is the correct method for your question. Anyways, in order to answer this question, sample data and a summary of your research question would be needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM