2

Already made some research about loops in R and found that most of them focus only on how to change file or variable name with loops like Change variable name in for loop using R could find a good solution from other articles of bloggers....

The following is what I want to do with my original data(s1P...s9P) to get new data(s1Pm...s9Pm) by calculate their means according to the Datum column in (s1P...s9P).

Running the following lines one by one is okay, however, it seems like there should be a possibility to use loops to make it tidy.

Any suggestions would be appreciated. Have a nice weekend!

s1Pm = aggregate(s1P, list(s1P$Datum), mean)
s2Pm = aggregate(s2P, list(s2P$Datum), mean)
s3Pm = aggregate(s3P, list(s3P$Datum), mean)
s4Pm = aggregate(s4P, list(s4P$Datum), mean)
s5Pm = aggregate(s5P, list(s5P$Datum), mean)
s6Pm = aggregate(s6P, list(s6P$Datum), mean)
s7Pm = aggregate(s7P, list(s7P$Datum), mean)
s8Pm = aggregate(s8P, list(s8P$Datum), mean)
s9Pm = aggregate(s9P, list(s9P$Datum), mean)
chew
  • 21
  • 3
  • sorry for the unfinished title...it should be "Apply functions in different csv files in R with regularity of filename " – chew Mar 16 '19 at 18:34
  • another typo..could "not" find a good solution from other articles of bloggers "either".... a little bit tired now... – chew Mar 16 '19 at 18:38

1 Answers1

2

We can load all the objects in a list with mget and then apply the aggregate by looping through the list

outLst <- lapply(mget(paste0("s", 1:9, "P")), 
             function(x) aggregate(x, list(x$Datum), mean))
names(outLst) <- paste0(names(outLst), "m")

It is better to keep the output in a list rather than creating multiple objects. But, it can be done as well

list2env(outLst, envir = .GlobalEnv)

though not recommended

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you @akrun, but I need to deal with them separately for several next steps before listing them together... – chew Mar 16 '19 at 18:42
  • 1
    @chew Okay, then read the datasets into a list. and do the steps. i.e. `files <- list.files(pattern = "\\.csv"); lapply(files, function(x) {x1 <- read.csv(x); do steps on x1; aggregate(....)})` – akrun Mar 16 '19 at 18:44
  • there are two more steps before I can list them and combined with another index file by cbind...and each step has the same filename name regularity that should be able to be dealt with loops which I have no ideas for now... – chew Mar 16 '19 at 18:53
  • @chew Okay, anyway I answered the question that was posted here. For your other stuff, I guess we both can agree that I don't have access to all your functions – akrun Mar 16 '19 at 18:55
  • many thanks for your friendly answers, but if them were listed now, then the data order will be messed up...I will try to find the way to deal with it succinctly, thank you for your instant help : ) – chew Mar 16 '19 at 19:06
  • in the end, I use matlab to print out the coding lines mentioned above as follows, to deal with data from individual subjects :P, for sub=1:20 if sub <10 disp(['s0',num2str(sub),'Pm = aggregate(s0',num2str(sub),'P, list(s0',num2str(session),'P$Datum), mean)']) else disp(['s',num2str(sub),'Pm = aggregate(s',num2str(sub),'P, list(s',num2str(session),'P$Datum), mean)']) end end – chew May 22 '19 at 22:24