Nested for loop through different sets of data in R -


how can create nested loop uses different data sets (each consisting of several data files) input , save results variable- specific?

i have written loop subsets different climate data files 1 country , sums values temperature.

the data looks , given every day in every region of both countries (one file=one region)

date      |prec |temperature ----------|-----|-----------                                           13-01-1992| 1   |   1 14-01-1992| 0   |   1.5 15-01-1993| 0.8 |   -0.4 16-01-1993| 0   |   -2.2 17-01-1994| 0   |   -2.35 13-01-1994| 0.3 |   -2.95 14-01-1995| 1   |   -8.95 15-01-1995| 2   |   -7.25 16-01-1996| 1.5 |   -6 17-01-1996| 0   |   -8.3 13-02-1997| 1   |   -0.3 14-02-1997| 0.1 |   -0.15 15-02-1998| 0   |   -2.5 16-02-1998| 0.2 |   -3.4 17-02-1999| 0.9 |   -0.4 16-03-1999| 2.6 |   8.4 17-03-2000| 1.7 |   11 18-03-2000| 4.7 |   4.65 19-03-2001| 1   |   2.95 20-03-2001| 0.6 |   4.7 13-08-2002| 2   |   22.35 14-08-2002| 1   |   20 15-08-2003| 1.7 |   21.4 16-08-2003| 0.5 |   21.55 17-08-2004| 0.4 |   21.5 17-02-2004| 0.3 |   -0.6 18-02-2005| 0.8 |   -3.4 19-02-2005| 1.2 |   -3 20-02-2006| 0.8 |   2 21-02-2006| 6   |   1.2 

now want run on data sets of 2 different countries. different number of data-files belongs each country. tried this:

temperature<-matrix(1995:2006,12,1) country_a<-c("1.csv","2.csv","3.csv") country_b<-c("4.csv","5.csv") country<-c(country_a, country_b) country_names<-c("country_a "," country_b ")  for(j in 1:2) {for(i in country[j]) { name <- country_names[j]   data<-read.csv(i, header=true, sep = ",") data$dates<-as.date(data$date, "%d-%m-%y") data95<-subset(data, dates>="1995-01-01") data$years<- as.numeric(format(data$dates, "%y")) temperature<-cbind(temperature, aggregate(data95$column1, by= list(data95$years),fun=sum))}} 

instead of looping through 1 country after each other, files 1 , 2 addressed. think problem country<-c(country_a, country_b)

i assume array solution address countries separately , maybe save temperature results country specific. unfortunately quite new r , therefore don’t know how set up. happy help!

temperature<-matrix(1995:2006,12,1)  # below understanding. country_a represents names of files in directory     # country_a<-c("1.csv","2.csv","3.csv") # country_b<-c("4.csv","5.csv")  la=list.files(path = "countrya_pathname", pattern= ".csv") lb=list.files(path = "countryb_pathname", pattern= ".csv")  l1a = paste0("countrya_pathname", la) # l1a = c("countrya_pathname/1.csv", "countrya_pathname/2.csv", "countrya_pathname/3.csv") l1b = paste0("countryb_pathname", lb) # l1b = c("countryb_pathname/4.csv", "countryb_pathname/5.csv")  abc <- function(path) {   data = read.csv(path)   data$date<-as.date(data$date, "%d-%m-%y")   data$years<- as.numeric(format(data$date, "%y"))   data95 = subset(data , date >="1995-01-01")   temperature <- ddply(data95, "years", function(x) sum(x$temperature))[-1]#just extracts sum column   temperature }  la = lapply(l1a, abc) lb = lapply(l1b, abc)  da = cbind(temperature, as.data.frame(la)) colnames(da) <- c("temperature", la) db = cbind(temperature, as.data.frame(lb)) colnames(db) <- c("temperature", lb) 

hope works


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -