I managed to get the inner for loop to work, but I have not figured out to get the outer for loop to run. My objective of the for loop is to condense some data. My original data is in a format such that there are columns with data from a particular temperature. Each column is a different trial. There are various columns with data from the same temperature. I want the for loop to aggregate all of the same temperatures into one data frame for each temperature range. The title of each column is the temperature.trial#. So for example: cols 1-3 are Temp_30.1, Temp_30.2, Temp_30.3.
I know there is a way to condense this down, I just haven't gotten the code to work. I had to manually run the outer for loop. But here is the code:
Temprange<-c("30", "32", "34", "36", "38")
t<-grep(Temprange[1], names(data), value=TRUE)
t1<-data.frame(data[t[1]])
colnames(t1)<-paste("Temp_",Temprange[1], sep= "")
t30<-data.frame(rep("NA", times = count(t1)))
for (i in 1:length(t)){
t30[,i+1]<-data[t[i]]
colnames(t2)<-rep(paste("Temp_", Temprange[1], sep=""),times=i+1)
#cat(i,"\n")
}
#Temp 32 C
Temprange<-c("30", "32", "34", "36", "38")
t<-grep(Temprange[2], names(data), value=TRUE)
t1<-data.frame(data[t[2]])
colnames(t1)<-paste("Temp_",Temprange[2], sep= "")
t32<-data.frame(rep("NA", times = count(t1)))
for (i in 1:length(t)){
t32[,i+1]<-data[t[i]]
colnames(t32)<-rep(paste("Temp_", Temprange[2], sep=""),times=i+1)
#cat(i,"\n")
}
#Temp 34 C
Temprange<-c("30", "32", "34", "36", "38")
t<-grep(Temprange[3], names(data), value=TRUE)
t1<-data.frame(data[t[3]])
colnames(t1)<-paste("Temp_",Temprange[3], sep= "")
t34<-data.frame(rep("NA", times = count(t1)))
for (i in 1:length(t)){
t34[,i+1]<-data[t[i]]
colnames(t34)<-rep(paste("Temp_", Temprange[3], sep=""),times=i+1)
#cat(i,"\n")
}
#Temp 36 C
Temprange<-c("30", "32", "34", "36", "38")
t<-grep(Temprange[4], names(data), value=TRUE)
t1<-data.frame(data[t[4]])
colnames(t1)<-paste("Temp_",Temprange[4], sep= "")
t36<-data.frame(rep("NA", times = count(t1)))
for (i in 1:length(t)){
t36[,i+1]<-data[t[i]]
colnames(t36)<-rep(paste("Temp_", Temprange[4], sep=""),times=i+1)
#cat(i,"\n")
}
#Temp 38 C
Temprange<-c("30", "32", "34", "36", "38")
t<-grep(Temprange[5], names(data), value=TRUE)
t1<-data.frame(data[t[5]])
colnames(t1)<-paste("Temp_",Temprange[5], sep= "")
t38<-data.frame(rep("NA", times = count(t1)))
for (i in 1:length(t)){
t38[,i+1]<-data[t[i]]
colnames(t38)<-rep(paste("Temp_", Temprange[5], sep=""),times=i+1)
#cat(i,"\n")
}
The main issue I'm having is not knowing how to create a new data frame for each of my new temperatures in the for loop named according to the temperature. Any help would be greatly appreciated!
What I have tried that doesn't work is this:
Temprange<-c("30", "32", "34", "36", "38")
for (jj in 1:5){
t<-grep(Temprange[jj], names(data), value=TRUE)
t1<-data.frame(data[t[jj]])
colnames(t1)<-paste("Temp_",Temprange[jj], sep= "")
for (i in 1:length(t)){
t30[,i+1]<-data[t[i]]
colnames(t2)<-rep(paste("Temp_", Temprange[jj], sep=""),times=i+1)
}
}