0

I have a table (df):

Class    Subclass
A          C
A          D
B          E
B          F

And the results of the calculations of "C" "D" "E" "F" in separated data.frames

C (a data.frame,1 column)

D (a data.frame,1 column)

E (a data.frame,1 column)
F (a data.frame,1 column)

And I want to bind the data.frames "C" "D" to "A" and "E" "F" to B, for the sum of the group A (C,D) and B (E,F). I need to do a loop because is number of class have 70 and the subclass 20000 variables, and make this manually is too hard. I have a list with the name of "A" and the names of the subclass "C" "D" trying to bind in a data.frame but the script doesn't work

Thanks.

fv123
  • 1
  • 1
  • Just do a split to split the dataset into a `list` of datasets `lst <- split(df1['Subclass'], df1$Class)` – akrun Jan 05 '18 at 11:23
  • I don't need to split, I make a split to do the calculations of C,D...I need something like unsplit, to grouping the results of C, D in A... I do that you say, but didn't work. Thanks for your attention – fv123 Jan 05 '18 at 11:30
  • Ok, then it is not clear. – akrun Jan 05 '18 at 11:35
  • Yes, that work to make the list, i will try to do the loop. thanks. – fv123 Jan 05 '18 at 11:36
  • And with this list, how can I make a loop, for bind the data.frames with my results of subclass in a data.frame by the name of class?? for (d in names (lst)) { total <- bind_cols(match(lst=='subclass')) } – fv123 Jan 05 '18 at 11:46

1 Answers1

0

(If I understand you correctly), if you pull your classes as a vector you could use tapply as in the following example which sums subsets of the numbers 1:100 and assigns them to a class from A to Z:

z <- tapply(1:100, base::sample(LETTERS, 100, replace = T), FUN = sum)
Valeri Voev
  • 1,982
  • 9
  • 25
  • the variables are no LETTERS, are chemical compounds...I have the data.frames with the results of each LETTER, in the enviroment, I want bind the LETTERs that you say to the class. Thank you. – fv123 Jan 08 '18 at 09:33
  • Sorry, I think I didn't make myself clear, I mean that if the calculations that create the data frames C, D, E, F, etc, are not too complicated then one can use `tapply` with an (anonymous) function that makes these calculations and aggregates them on the higher (Class) level, circumventing the need to crate the individual Subclass data frames – Valeri Voev Jan 08 '18 at 09:46
  • The calculations are around 300 lines to each compound with too much loops( inside loops). So, I need group, C, D, E, F which are in the global environment, and take these results, and make a data.frame with the results of each class. For example, group the results by the names of class in df (see up). Thus, i group the results of the subclass by class, and then I can make the last calculation, the sum of each class. I really appreciate your help!!!Thank you so much – fv123 Jan 09 '18 at 09:53
  • On this link https://stackoverflow.com/questions/14954399/put-multiple-data-frames-into-list-smart-way there is a discussion about how to get many data frames into a list. If you manage that (say you create a list of all your C, D, E, etc and call it `ldf`), and the data frames have the same structure (number of columns and column names) you can then `rbind` them into a single data frame by `do.call(rbind, ldf)`. That might bring you closer ... – Valeri Voev Jan 09 '18 at 12:42
  • Hi Valeri!I have df (with class and subclass), the list of subclass, and one data.frame by subclass (I also have one data.frame with all the results subclass "C D E F" in (each results is only a column) ), but what I want is find some comand, for say to R; "Ey! Do you see the names in the global environment? So make me a group (by class) with the results of each subclass (that I have in the global environment and in .csv), and if you want take the relationship of df (to know wich subclass put in each data.frame of class). My english is so bad, sorry. Thank you for your attention and patience – fv123 Jan 09 '18 at 17:22