Combining rows by index in R [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-13 07:48:59

问题


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


来源:https://stackoverflow.com/questions/50055417/combining-rows-by-index-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!