循环数据框以创建箱线图
looping a data frame to create a boxplot
我正在尝试在数据框中使用 R 软件获取箱线图,但数据框中附加了很多列。
这是代码
path = "E:/plot/2"
fileList = list.files(path=path,pattern="\.teTestResult",full.names=T)
myfiles = lapply(fileList, read.csv,header=TRUE,sep=";" )
a <- data.frame(myfiles)
attach(a)
boxplot(Return~gama)
boxplot(Return~theta)
boxplot(Return~detectionsLimit)
boxplot(Return~NSMOOTH)
boxplot(Return~NREF)
boxplot(Return~NOBS)
boxplot(Return.1~gama.1)
boxplot(Return.1~theta.1)
boxplot(Return.1~detectionsLimit.1)
boxplot(Return.1~NSMOOTH.1)
boxplot(Return.1~NREF.1)
boxplot(Return.1~NOBS.1)
...
boxplot(Return.9~NOBS.9)
此代码有效,但由于太长而未能提供一个好的代码。我如何使用 R 简化它?
非常感谢您的帮助
~更新~
我正在尝试使用 for 循环,因为我尝试绘制箱线图的变量名称仅在数字上有所不同,所以这里是
for (i in 1:9){
boxplot(Return.[i]~gama.[i])
}
但是,这样说时发生错误
Error in eval(expr, envir, enclos) : object 'Return.' not found
我仍然目睹了 R 编程的许多问题
非常感谢您的回复。
如果将数据帧列表与 data.frame() 函数结合使用,则您正在绑定数据帧。如果所有文件都具有相同的结构,我建议您使用 rbind:
组合它们
library(dplyr)
for(i in 1:length(myfiles)) myfiles[[i]]$nr = i # if you need to know the origin of the data
df = bind_rows(myfiles)
不同文件之间的区别可以通过分面来完成,但是由于您需要不同变量的图,您可能无法进一步缩短代码:
library(ggplot2)
ggplot(df, aes(factor(gama), Return)) + geom_boxplot() + facet_wrap(~ nr)
ggplot(df, aes(factor(theta), Return)) + geom_boxplot() + facet_wrap(~ nr)
ggplot(df, aes(factor(detectionsLimit), Return)) + geom_boxplot() + facet_wrap(~ nr)
ggplot(df, aes(factor(NSMOOTH), Return)) + geom_boxplot() + facet_wrap(~ nr)
ggplot(df, aes(factor(NREF), Return)) + geom_boxplot() + facet_wrap(~ nr)
ggplot(df, aes(factor(NOBS), Return)) + geom_boxplot() + facet_wrap(~ nr)
我正在尝试在数据框中使用 R 软件获取箱线图,但数据框中附加了很多列。 这是代码
path = "E:/plot/2"
fileList = list.files(path=path,pattern="\.teTestResult",full.names=T)
myfiles = lapply(fileList, read.csv,header=TRUE,sep=";" )
a <- data.frame(myfiles)
attach(a)
boxplot(Return~gama)
boxplot(Return~theta)
boxplot(Return~detectionsLimit)
boxplot(Return~NSMOOTH)
boxplot(Return~NREF)
boxplot(Return~NOBS)
boxplot(Return.1~gama.1)
boxplot(Return.1~theta.1)
boxplot(Return.1~detectionsLimit.1)
boxplot(Return.1~NSMOOTH.1)
boxplot(Return.1~NREF.1)
boxplot(Return.1~NOBS.1)
...
boxplot(Return.9~NOBS.9)
此代码有效,但由于太长而未能提供一个好的代码。我如何使用 R 简化它? 非常感谢您的帮助
~更新~
我正在尝试使用 for 循环,因为我尝试绘制箱线图的变量名称仅在数字上有所不同,所以这里是
for (i in 1:9){
boxplot(Return.[i]~gama.[i])
}
但是,这样说时发生错误
Error in eval(expr, envir, enclos) : object 'Return.' not found
我仍然目睹了 R 编程的许多问题 非常感谢您的回复。
如果将数据帧列表与 data.frame() 函数结合使用,则您正在绑定数据帧。如果所有文件都具有相同的结构,我建议您使用 rbind:
组合它们library(dplyr)
for(i in 1:length(myfiles)) myfiles[[i]]$nr = i # if you need to know the origin of the data
df = bind_rows(myfiles)
不同文件之间的区别可以通过分面来完成,但是由于您需要不同变量的图,您可能无法进一步缩短代码:
library(ggplot2)
ggplot(df, aes(factor(gama), Return)) + geom_boxplot() + facet_wrap(~ nr)
ggplot(df, aes(factor(theta), Return)) + geom_boxplot() + facet_wrap(~ nr)
ggplot(df, aes(factor(detectionsLimit), Return)) + geom_boxplot() + facet_wrap(~ nr)
ggplot(df, aes(factor(NSMOOTH), Return)) + geom_boxplot() + facet_wrap(~ nr)
ggplot(df, aes(factor(NREF), Return)) + geom_boxplot() + facet_wrap(~ nr)
ggplot(df, aes(factor(NOBS), Return)) + geom_boxplot() + facet_wrap(~ nr)