I need to process three data frames containing the same subgroups indexed by name. That is, the first data frame df1 looks like this:
Name col1 col2
Car 94.56 1
Car 52.67 2
Bike 421.5 2
Bike 34.56 4
df2 and df3 have the same Name column with the same values, only different columns. I need to process all the rows in the 3 data frames, for each different name. So far I've been using this approach:
results = data.frame(name = factor("dummy"), col1 = 1, col2 = 2)
for( name in df1$Name ) {
new.results = process(name, df1[df1$Name == name, ], df2[df2$Name == name, ], df3[df3$Name == name, ]
results = rbind(results, new.results)
}
return(results)
Here process() returns another data frame with the results of some calculation. The problem with this code is that process() must return the same layout than the 'results' data frame. If I change the contents returned by process() I also have to change 'results'. Also the first row in the results data frame must be removed.
Is there an easier way to do this? by() can group 1 data frame by name and invoke process() for each subgroup, but I can't pass in the df2 and df3 subgroups.