0

I am creating an automated survey analysis tool which has a parent dataframe name "Impact" which has a column "Level.Mapped.to". The idea is to split myData into different dataframes based on factor values of "Level.Mapped.to". I did this using :

LevelMap = split( ImpactDF , ImpactDF$Level.Mapped.to)


for(i in 1:length(LevelMap))
  {
  assign(paste0("LevelMapped", i),  data.frame(LevelMap[i]))
  }

Now this creates 3 different Dataframes LevelMapped1, LevelMapped2 and LevelMapped3.

Now for all these values I want to perform the following functions :

AvgImpactDF[i] = round(sapply(LevelMapped[i], function(x) mean(x,na.rm=TRUE,trim=0) ),2)

Now this AvgImpactDF is to be created for n number of levels in the ImpactDF. Please help me how I can do this.

My LevelMapped1 has 15 columns

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • why are you spliting the data ? can you try `by(ImpactDF , ImpactDF$Level.Mapped.to, function(x) round(mean(x,na.rm=TRUE,trim=0)),2)` – s.brunel Aug 29 '16 at 11:54
  • Please do not use assign. Either try to not split the data, as s.brunel pointed out, or, put the data.frames generated by splitting into a list. This list, you can easily traverse using `lapply` or a `for` loop. – Bernhard Aug 29 '16 at 11:58
  • `split` will return a named list of data.frames, where the names of each list item are the factor level. So levelMap from your first line is a list of data.frames. Use `lapply` on this list to apply your function to each of the data.frames as suggested by bernhard. For more details on working with lists of data.frames, see gregor's answer to [this post](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames). – lmo Aug 29 '16 at 12:18
  • Thanks I was able to solve it – Vikram Aditya Rattan Aug 31 '16 at 07:22

0 Answers0