簡體   English   中英

“for循環”中的箱線圖:將字符串轉換為變量

[英]Boxplots in "for loop": Transforming character string to a variable

我正在嘗試從數據框中繪制箱線圖,並分別保存每個箱線圖的圖像,但出現錯誤。 在代碼的第 4 行,我相信“x”是一個字符串而不是一個變量。 那么,我應該如何將此字符串轉換為循環變量?

(i) 示例數據框:

df <- data.frame(var1=c(1, 3, 3, 4, 5), 
                 var2=c(7, 7, 8, 3, 2),
                 type=factor(c('A','B','B','C','D'))) #dataframe

(ii) 示例查詢:

LIST<-c('var1', 'var2')

(iii) 實施的代碼

for (j in LIST) #
{
png(print(paste0((j),".png")))
boxplot(as.numeric(paste("df",j, sep = "$")) ~ as.factor(df$type))  #..(4)
dev.off()
}

(iv) 錯誤信息

Error in stats::model.frame.default(formula = as.numeric(paste("df$",  : 
variable lengths differ (found for 'as.factor(df$type)')
In addition: Warning message:
In eval(predvars, data, env) : NAs introduced by coercion

你有比你需要的更多的代碼。 嘗試這個

for (j in LIST) #
{
    png(paste0(j, ".png"))
    boxplot(df[, j] ~ df$type)
    dev.off()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM