0

I'm relatively new to programming, and have found the following link incredibly helpful in trying to create new variables from a large dataset using a loop: Change variable name in for loop using R i.e.

for (i in 1:13){
assign(paste("conD",i, sep=""), (subset(con,day==i)))
 }

This is to produce a set of 13 variables from a dataset of control biological samples, with each subset containing data from each day in the timeseries day1-day13 i.e. "conD1" would contain data from controls analysed on day 1. (the dimensions of the df are generally 3x4000). This worked perfectly. However, I now wish to create 13 more variables in a loop, with the median of each column calculated, i.e. "medconD1" - "medconD13" within a loop, each one 1x4000, by recalling the created variables conD1-conD13. I have tried the following code, but this doesnt work:

for (i in 1:13){
assign(paste("medconD",i, sep=""), (apply(conD[i][8:n], 2, FUN = median)))
 }

Can anyone help or point out where I am going wrong here? How do I recall the conD[i] variables in this second part of code? This will save me weeks of work with large datasets! Thank you

Community
  • 1
  • 1
bmbetu
  • 11
  • 2
  • Why not do it in the same loop? Is there processing between these two steps? – Elin Feb 10 '17 at 11:13
  • No, no more processing. I'm trying to do this now by using the original control data set with all days in, instead of subsetting. So far com up with this, but still having problems in that "medconD1" etc dont exist: `for (i in 1:13) { if ("day" == i) assign(paste("medconD",i, sep=""), (apply(con[,8:n], 2, FUN = median))) }` If you have any suggestions I'd appreciate it. Thanks – bmbetu Feb 10 '17 at 11:56
  • In same loop: `for (i in 1:13){ assign(paste("conD",i, sep=""), (subset(con,day==i))) assign(paste("medconD",i, sep=""), (apply(conD[i][8:n], 2, FUN = median))) } ` but still dont know how to use conD[i], or conDi, or conD.i etc. Thanks – bmbetu Feb 10 '17 at 11:58
  • All sorted out now (thanks helpers!) - answer was to add them to a list as I made them, then recall them from a list: `conlist=c() for (i in 1:13){ x<-assign(paste("conD",i, sep=""), (subset(con,day==i))) conlist=c(conlist,list(x)) } medconlist=c() for (i in 1:13){ x<-assign(paste("medconD",i, sep=""), (apply(conlist[[i]][,8:n], 2, FUN = median))) medconlist=c(medconlist,list(x)) }` – bmbetu Feb 11 '17 at 13:32
  • You should write an answer and accept it. It was good question. – Elin Feb 11 '17 at 17:01
  • Oh OK! Thanks a lot, will do – bmbetu Feb 12 '17 at 17:12

1 Answers1

0

All sorted out now (thanks helpers!) - answer was to add them to a list as I made them, then recall them from a list:

conlist=c()
    for (i in 1:13){
    x<-assign(paste("conD",i, sep=""), (subset(con,day==i)))
    conlist=c(conlist,list(x))
    }
    medconlist=c()
    for (i in 1:13){
    x<-assign(paste("medconD",i, sep=""), (apply(conlist[[i]][,8:n], 2, FUN = median)))
    medconlist=c(medconlist,list(x))
    }
bmbetu
  • 11
  • 2