我想在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”
如何获得期望的结果?
答案 0 :(得分:0)
您需要使用assign
和get
:
cultivar <- c("uri", "keumgang", "saeal", "ahnbaek")
for(i in cultivar){
assign(
paste0('anova_height_', i),
aov(plant_height ~ treatment, data = get(paste0('data_', i)))
)
}