想要在r

时间:2019-02-02 06:51:54

标签: r

我想在for循环中使用字符名来创建变量。

例如:

cultivar <- c("uri", "keumgang", "saeal", "ahnbaek")

for(i in cultivar){
    anova_height_i <- aov(plant_height ~ treatment, data = data_i)
}

从上面,我希望有变量

anova_height_uri <- aov(plant_height ~ treatment, data = data_uri)
...
...
anova_height_ahnbaek <- aov(plant_height ~ treatment, data = data_ahnbaek)

但是我收到了错误消息:

  

aov(plant_height〜treatment,data = data_i)中的错误:     找不到对象“ data_i”

如何获得期望的结果?

1 个答案:

答案 0 :(得分:0)

您需要使用assignget

cultivar <- c("uri", "keumgang", "saeal", "ahnbaek")

for(i in cultivar){
  assign(
    paste0('anova_height_', i),
    aov(plant_height ~ treatment, data = get(paste0('data_', i)))
  )
}