Combining rows by index in R [duplicate]

2019-09-20 12:54发布

问题:

This question already has an answer here:

  • Combining pivoted rows in R by common value 4 answers

EDIT: I am aware there is a similar question that has been answered, but it does not work for me on the dataset I have provided below. The above dataframe is the result of me using the spread function. I am still not sure how to consolidate it.

EDIT2: I realized that the group_by function, which I had previously used on the data, is what was preventing the spread function from working in the way I wanted it to work originally. After using ungroup, I was able to go straight from the original dataset (not pictured below) to the 2nd dataframe pictured below.


I have a dataframe that looks like the following. I am trying to make it so that there is only 1 row for each id number.

id  init_cont  family  1  2  3
1   I          C       1  NA NA
1   I          C       NA 4  NA
1   I          C       NA NA 3
2   I          D       2  NA NA
2   I          D       NA 1  NA
2   I          D       NA NA 4
3   K          C       3  NA NA
3   K          C       NA 4  NA
3   K          C       NA NA 1

I would like the resulting dataframe to look like this.

id  init_cont  family  1  2  3
1   I          C       1  4  3
2   I          D       2  1  4
3   K          C       3  4  1

回答1:

We cangroup_by the 'd', 'init_cont', 'family' and then do a summarise_all to remove all the NA elements in the columns 1:3

library(dplyr)
df1 %>%
   group_by_at(names(.)[1:3]) %>%
   summarise_all(na.omit)
   #Or
   #summarise_all(funs(.[!is.na(.)]))
# A tibble: 3 x 6
# Groups: d, init_cont [?]
#      d init_cont family   `1`   `2`   `3`
#   <int> <chr>     <chr>  <int> <int> <int>
#1     1 I         C          1     4     3
#2     2 I         D          2     1     4
#3     3 K         C          3     4     1