Skip to content

multiple plots and looping assistance requested (revised codes)

2 messages · Irucka Embry, arun

#
HI Irucka,

The situation is slightly different here.? I was under the assumption that the list elements sometimes had one two columns or three columns.? Here, all the columns are present, but some with entire rows of missing values.? Also, there was a mistake in the code when I updated the code.
If the real data is similar to the one posted here, I guess it should work.

temp<- structure(list(`:Bostoncitydata` = structure(list(Month = c(1L,
2L, 3L, NA), Data1 = c(1.5, 12.3, 11.4, NA), Data2 = c(9.1342,
12.31, 3.5, NA)), .Names = c("Month", "Data1", "Data2"), class = "data.frame", row.names = c(NA,
-4L)), `:Chicagocitydata` = structure(list(Month = c(1L, 2L,
3L, 4L, 5L, NA), Data1 = c(1.52, 12.63, 20.34, 12.83, 3.34, NA
), Data2 = c(19.41342, 13.031, 0.80021, 12.63104, 19.20021, NA
)), .Names = c("Month", "Data1", "Data2"), class = "data.frame", row.names = c(NA,
-6L)), `:NewYorkcitydata` = structure(list(Month = c(1L, 2L,
3L, 4L, 5L, 6L, 7L, NA), Data1 = c(NA, NA, NA, NA, NA, NA, NA,
NA), Data2 = c(3.1342, 1.31, 13.5, 1.31, 2.40021, 0.25, 26.3,
NA)), .Names = c("Month", "Data1", "Data2"), class = "data.frame", row.names = c(NA,
-8L)), `:Philadelphiacitydata` = structure(list(Month = c(1L,
2L, 3L, 4L, 5L, 6L, NA), Data1 = c(1.65, 11.63, 5.0434, 13.73,
3.0234, 34.209, NA), Data2 = c(2.61342, 16.331, 19.040021, 17.831,
10.1010021, 3.80742, NA)), .Names = c("Month", "Data1", "Data2"
), class = "data.frame", row.names = c(NA, -7L)), `:Atlantacitydata` = structure(list(
Month = c(1L, 2L, 3L, NA), Data1 = c(NA, NA, NA, NA), Data2 = c(NA,
NA, NA, NA)), .Names = c("Month", "Data1", "Data2"), class = "data.frame", row.names = c(NA,
-4L)), `:Baltimorecitydata` = structure(list(Month = 1:2, Data1 = c(11.325,
32.433), Data2 = c(49.71342, 52.4031)), .Names = c("Month", "Data1",
"Data2"), class = "data.frame", row.names = c(NA, -2L))), .Names = c(":Bostoncitydata",
":Chicagocitydata", ":NewYorkcitydata", ":Philadelphiacitydata",
":Atlantacitydata", ":Baltimorecitydata"))
temp1<-lapply(temp,function(x) {x1<-x[,colSums(is.na(x))!=nrow(x)]; if(is.data.frame(x1)) x1[complete.cases(x1),] else x1[complete.cases(x1)] })
# temp1<-lapply(temp1,function(x) x[is.data.frame(x)])
#3 columns subset
temp3<-temp1[lapply(temp1,length)==3]
?temp3New<-lapply(temp3,function(x) lapply(names(x)[-1], function(i) {x1<-cbind(x[,1],x[,i]);colnames(x1)<- c("Month",i);x1} ))
pdf("Irucka3.pdf") 
par(mfrow=c(1,2)) 
lapply(names(temp3New),function(i) lapply(temp3New[[i]],function(x) {plot(x[,1],x[,2],main="Seasonal Flux Sum",sub=paste(i,colnames(x)[2],sep="_"),xlab="Calendar Year Timesteps",ylab="Total Flux (kg/season)");lines(x[,1],x[,2])})) 
dev.off()

#2 columns subset
temp2<-temp1[lapply(temp1,length)==2] 
temp2New<-lapply(temp2,function(x) lapply(names(x)[-1], function(i) {x1<-cbind(x[,1],x[,i]);colnames(x1)<- c("Month",i);x1} )) 
?
pdf("Irucka2.pdf") 
par(mfrow=c(1,1)) 
lapply(names(temp2New),function(i) lapply(temp2New[[i]],function(x) {plot(x[,1],x[,2],main="Seasonal Flux Sum",sub=paste(i,colnames(x)[2],sep="_"),xlab="Calendar Year Timesteps",ylab="Total Flux (kg/season)");lines(x[,1],x[,2])})) 
dev.off()

Please let me know if there are problems.
A.K.