I have a bunch of .csv files that I want to read into a list, then create plots. I've tried the code below, but get an error when trying to cbind. Below is the dput from 2 example files. Each file represents weather data from seperate stations. Ideally I would plot prcp data (column) from each file into one plot window. I don't have much experience working with data in a list.
file1 <- structure(list(mxtmp = c(18.974, 20.767, 21.326, 19.669, 18.609,
21.322), mntmp = c(4.026, 5.935, 8.671, 6.785, 3.493, 6.647),
prcp = c(0.009, 0.046, 0.193, 0.345, 0.113, 0.187)), .Names = c("mxtmp",
"mntmp", "prcp"), row.names = c(NA, 6L), class = "data.frame")
.
file2 <- structure(list(mxtmp = c(18.974, 20.767, 21.326, 19.669, 18.609,
21.322), mntmp = c(4.026, 5.935, 8.671, 6.785, 3.493, 6.647),
prcp = c(0.009, 0.046, 0.193, 0.345, 0.113, 0.187)), .Names = c("mxtmp",
"mntmp", "prcp"), row.names = c(NA, 6L), class = "data.frame")
I read these files from a directory into a list:
myFiles <- list.files(full.names = F, pattern = ".csv")
my.data <- lapply(myFiles, read_csv)
my.data
names(my.data) <- gsub("\\.csv", " ", myFiles)
I get an error on the line below:
my.data <- lapply(my.data, function(x) cbind(x = seq_along(x), y = x))
Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 3, 34333
list.names <- names(my.data)
lns <- sapply(my.data, nrow)
my.data <- as.data.frame(do.call("cbind", my.data))
my.data$group <- rep(list.names, lns)
My plot code:
library(ggplot2)
ggplot(my.data, aes(x = x, y = y, colour = group)) +
theme_bw() +
geom_line(linetype = "dotted")