Quantcast
Channel: Active questions tagged row - Stack Overflow
Viewing all articles
Browse latest Browse all 447

In R, how can I collapse the data of grouped rows into one row?

$
0
0

I have the following sample dataframe:

df <- data.frame(    record_id = c(1, 1, 1, 2, 2, 3, 3, 3),    instance = c(NA, NA, 2, NA, 1, 2, NA, NA),    A = c(10, NA, NA, 20, 25, NA, 30, NA),    B = c(NA, 5, NA, NA, 5, 15, NA, 15),    C = c(NA, NA, 3, NA, 5, 20, NA, 20),    D = c(NA, NA, NA, 25, 25, 30, NA, 30))  record_id instance  A  B  C  D1         1       NA 10 NA NA NA2         1       NA NA  5 NA NA3         1        2 NA NA  3 NA4         2       NA 20 NA NA 255         2        1 25  5  5 256         3        2 NA 15 20 307         3       NA 30 NA NA NA8         3       NA NA 15 20 30

If the instance is NA, I want the rows of the same record_id to be collapsed down into one row. In my dataframe, there will not be two or more values in the same column for the same record_id and NA instance group.

In other words, I would like to get:

  record_id instance  A  B  C  D1         1       NA 10  5 NA NA2         1        2 NA NA  3 NA3         2       NA 20 NA NA 254         2        1 25  5  5 255         3        2 NA 15 20 306         3       NA 30 15 20 30

How can I do this?


Viewing all articles
Browse latest Browse all 447

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>