繁体   English   中英

将多个 csv 文件导入到 R 和 plot 数据对中的每个 Z628CB5675FF524F3E719ZFE7AA2E 上的同一图形文件

[英]Import multiple csv files into R and plot pairs of data from each csv file on the same graph

我想导入多个 csv 文件 (12),其中包含时间 t 和压力 P 的数据。每个文件的标题相同 (t,P) 但时间值不常见,因此每个 csv 中的每个 t,P 对文件是唯一的。

我可以毫无问题地导入文件:

filenames <- list.files(path = ("C:/Users/K125763/Documents/Mars"),  
                        # Follows a regular expression that matches:
                        pattern = "mars-[0-12]{2}.csv",
                        full.names = TRUE)
filenames <- filenames[1:12]

但是下面的循环将每个数据对绘制在单独的图表上:

analyze.p <- function(filename) {
dat <- read.csv(file = filename, header = TRUE)

plot(dat$P~dat$t)

}

for (f in filenames) {
  print(f)
  analyze.p(f)
}

如何在同一个图表上获取所有数据对?

我会建议一种函数式编程方法 + ggplot2

library(purrr) # functional programming tools
library(dplyr) # data wrangling
library(ggplot2) # plotting
filenames <- list.files(path = ("C:/Users/K125763/Documents/Mars"),  
                        # Follows a regular expression that matches:
                        pattern = "mars-[0-12]{2}.csv",
                        full.names = TRUE)
filenames <- filenames[1:12]

## Read and concatenate CSV files
data <- filenames %>%
  map_df(~mutate(read.csv(file = .x, header = TRUE), source_file = .x))

## Plot in a single chart
ggplot(data, aes(t, P)) +
  geom_point() +
  facet_wrap('source_file')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM