I have the data as below:
> dput(test_data)
structure(list(`10` = c(0, 0, 0, 0, 0, 1, 0.22123412423, 0.0123915695,
0.0126915225, 0.4312, 1), `34` = c(0, 0, 0, 0, 0, 0.323256312,
0.32423561, 0.44451256, 0.33623498, 0.21341411, 0.321223), `59` = c(0.232,
0.57192, 0, 0, 0, 0.31312334, 0.2775713, 0.1311453, 0.63215713,
0.4423123, 0.132212), `84` = c(1, 1, 1, 0, 1, 0, 1, 1, 1, 1,
0), `110` = c(0.55345123, 0.689875, 0.423415551, 1, 0.444532121,
0, 0.01186404, 0.22132204, 0.21186404, 0, 0), `134` = c(0.234126,
0.33347267, 0.35321234, 0.4123412, 0.333412666, 0, 0, 0, 0.3123,
0, 0), `165` = c(0.1147189, 0.12343, 0.3155, 0.2755712, 0.123,
0, 0, 0, 0, 0, 0), `199` = c(0.1236836, 0.0058933, 0, 0.1344607,
0, 0, 0, 0, 0, 0, 0), Group = c("Train", "Bicycle", "Car", "Bicycle",
"Bicycle", "Car", "Bicycle", "Train", "Car", "Train", "Train"
)), .Names = c("10", "34", "59", "84", "110", "134", "165", "199",
"Group"), row.names = c("Mark_1", "Greg_1", "Tim_1", "Tom_1",
"Sim_1", "Karl_1", "Moham_1", "Teraq_1", "Jake_1", "Sonya_1",
"Monique_1"), class = "data.frame")
I would like to take rows from the same Group
and combine them into one row. The row.names are not important in that case. As a row name the group should be used. Of course some groups have more members (especially in my real data) so the missing values in specific columns should be filled with NA's
.
For example values for Greg
, Tom
, Sim
, Moham
should be in one row under the name Bicycle
.
Expected output;
We have 4 members of the group Bicycle
. Each of the member has 8 values = 8 columns. So as an output we should have a row.name=Bicycle and 32 columns with the values coming from those 4 members. We should do the same with all the groups.
EDIT:
So I expected something like Rechlay
and Wietze314
posted. Is there any way to remove those NA
values and keep the number of columns up to maximal number of members multiplied by 8. I do not care about colnames
and from which/column value came from.