【发布时间】:2020-10-25 04:11:32
【问题描述】:
这里是 R 的新手。
我发现了关于我的问题的类似主题和教程,但是由于我无法找到解决问题的方法,并且随着项目截止日期的临近,我想寻求帮助。
我在一个目录中有多个具有相似名称(2fg、20fg、...)的相似 csv 文件。它们都包含 8 列和 360 行的数据框。行代表时间,每列代表一系列测量值。
我想要做的是找到每列的平均值和 sd,然后是这 8 个平均值的集体平均值和 sd,最后绘制每个 csv 的时间序列(每条线具有不同的颜色)和比较的直方图所有 csv 的集合均值(和 sd)。
到目前为止,我能够找到平均值、标准差和它们的集合,并绘制单个 csv 文件的时间序列,使用
plot.ts(2fg, plot.type=c("single"), col=rainbow(ncol(2fg)))
我正在寻找的是一种在我目录中的每个 csv 上执行所有这些操作的方法,然后绘制直方图,该直方图将比较集体均值并在单个 xlsx 上输出每个 csv 的图和集体均值和 sds 或csv 文件。
我正在研究的方法是将我所有的 csv 放在一个列表中,使用
list <- list.files(pattern = "csv")
然后尝试使用 lapply 但我无法产生任何结果。
我希望我足够清楚,并提前感谢您的帮助!
编辑:按照@Len Greski 在 cmets 中提出的建议,我将所有文件放在一个列表中,同时为我的数据提供标题
lista <- list.files(pattern = ".csv", full.names=TRUE)
myfiles <- lapply(lista,function(x) {
y <- read.csv(x,stringsAsFactors=FALSE, header = FALSE, sep = ',',
col.names = c("well_1", "well_2", "well_3", "well_4", "well_5",
"well_6", "well_7", "well_8"))
y$filename <- x
y
})
然后在大数据框中
data <- do.call(rbind,myfiles)
# A tibble: 6 x 9
well_1 well_2 well_3 well_4 well_5 well_6 well_7 well_8 filename
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 0.158 0.218 0.152 0.189 0.205 0.190 0.181 0.153 ./1_s1_control.csv
2 0.158 0.218 0.152 0.189 0.205 0.190 0.181 0.153 ./1_s1_control.csv
3 0.158 0.218 0.152 0.189 0.204 0.190 0.181 0.153 ./1_s1_control.csv
4 0.158 0.218 0.152 0.189 0.204 0.190 0.181 0.153 ./1_s1_control.csv
5 0.159 0.218 0.151 0.189 0.204 0.190 0.181 0.153 ./1_s1_control.csv
6 0.159 0.218 0.151 0.189 0.204 0.190 0.181 0.153 ./1_s1_control.csv
然后我尝试了以下方法来计算均值和 sd,但出现错误
# summarise by file (filename)
data2 <- data %>%
group_by(filename) %>%
summarise(., across(c(well_1, well_2, well_3, well_4, well_5, well_6, well_7, well_8)),
list(mean = mean, sd = sd), .names = "{col}.{fn}")
Error: `across()` must only be used inside dplyr verbs.
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning message:
In names(cols)[missing_names] <- names[missing_names] :
number of items to replace is not a multiple of replacement length
欢迎任何建议或更正! :)
【问题讨论】:
-
您的
lapply尝试有什么问题?你到底尝试了什么?因为这是正确的策略。这里的例子可能会有所帮助:stackoverflow.com/questions/30790114/…
标签: r csv plot data-analysis