0

Hello guys im trying to import multiple data-station from csv documents and then plot their information into a loop. I would like to get my information virtual stored in R to manipulate them. Additionally, I need to find a way to display all my plots in a matrix. And finally another sheet with just 1 plot graphing all my data stations. I hope you can help me.

My data only has 2 columns, Date and Deep as follow:

ldat <- lapply(1:18, function(x) data.table(Date = sample(seq(as.Date('2016/01/01'), as.Date('2016/01/31'), by="day"), 12),
                                        Deep = runif(12, min = 32, max = 100)))

So, to import the documents and plot im doing the next code.

rutas<-list.files()
length(rutas)
par(mfrow=c(length(rutas), 2))
for (i in 1:length(rutas)) {
tabla<-read.csv(rutas[i],header=T,sep=";", as.is = TRUE,stringsAsFactors=FALSE)
as.numeric(tabla$Deep)
with(tabla, dmy_hm(Date))
a<-ggplot(tabla)+
  aes(x = as.Date(Date), y = Deep) + geom_point(colour="blue") +
  scale_x_date(labels=date_format("%Y-%m"),breaks = pretty_breaks(25)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 10))+
  ggtitle(rutas[i]) +
  xlab("Date") + ylab("Deep m")+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
a}

But im having trouble after running the object "a", whenever the loop ends the variable tabla changes into a new file which only has 1 column and pops this error

"Error in: "$<-.data.frame`(*tmp*, "Deep", value = numeric(0)) : 
  replacement has 0 rows, data has 65713"

If there is another way to avoid this error plz let me know.

  • Read all data into 1 dataframe, [see here](https://stackoverflow.com/questions/5186570), then read about [facet_grid](http://ggplot2.tidyverse.org/reference/facet_grid.html) – zx8754 Jul 20 '17 at 07:00
  • I think you need to put your iterator in double square brackets so that it is rutas[[i]]. Try that and see. – meenaparam Jul 20 '17 at 07:03
  • `as.numeric(tabla$Deep)` is not changing your data.frame. For that you need `tabla$Deep <- as.numeric(tabla$Deep)`. Ditto next line. – Richard Telford Jul 20 '17 at 07:16

0 Answers0