Dinho
Dinho

Reputation: 724

How to merge multiple rows into a single row for a single column?

I have a dataframe 'df':

# A tibble: 16 x 10
   Injury   Parameter                    mean    sdv     se median CoeffofVar UpperQuant LowerQuant     n
   <fct>    <chr>                       <dbl>  <dbl>  <dbl>  <dbl>      <dbl>      <dbl>      <dbl> <int>
 1 Severe   BL BBB                     21     0      0       21        0          21         21         5
 2 Severe   BL Time Delay (sec)         0.386 0.0385 0.0172   0.4      0.0997      0.4        0.39      5
 3 Severe   Acute BBB                   2.4   1.14   0.510    2        0.475       3          2         5
 4 Severe   Acute Time Delay (sec)      1.54  0.0829 0.0371   1.55     0.0539      1.59       1.47      5
 5 Severe   Acute Area Deficit (mm2)    3.20  0.120  0.0539   3.24     0.0376      3.27       3.12      5
 6 Severe   Chronic BBB                11.6   1.82   0.812   12        0.157      12         11         5
 7 Severe   Chronic Time Delay (sec)    0.802 0.0807 0.0361   0.8      0.101       0.86       0.75      5
 8 Severe   Chronic Area Deficit (mm2)  0.370 0.0466 0.0208   0.37     0.126       0.4        0.36      5
 9 Moderate BL BBB                     21     0      0       21        0          21         21         7
10 Moderate BL Time Delay (sec)         0.407 0.0435 0.0164   0.41     0.107       0.435      0.375     7
11 Moderate Acute BBB                   9.29  1.50   0.565    9        0.161      10.5        8.5       7
12 Moderate Acute Time Delay (sec)      0.974 0.0796 0.0301   0.97     0.0817      1.02       0.93      7
13 Moderate Acute Area Deficit (mm2)    1.73  0.113  0.0428   1.77     0.0654      1.79       1.70      7
14 Moderate Chronic BBB                16.6   1.51   0.571   17        0.0912     17.5       16         7
15 Moderate Chronic Time Delay (sec)    0.666 0.0806 0.0305   0.65     0.121       0.685      0.625     7
16 Moderate Chronic Area Deficit (mm2)  0.139 0.0752 0.0284   0.15     0.542       0.17       0.12      7

I would like to take columns "Injury" and "n" and reduce the repeated values to a single row while still keeping all other rows the same.

I tried using lapply but did not work:

df[, lapply(.SD, paste0, collapse=""), by=Injury]

Upvotes: 1

Views: 3569

Answers (1)

akrun
akrun

Reputation: 887881

As it is a tibble, we can make use of tidyverse functions (in the newer version of dplyr , we can use across with summarise)

library(dplyr)
library(stringr)
df %>% 
   group_by(Injury) %>% 
   summarise(across(everything(), str_c, collapse="")) 

Or with summarise_at

df %>% 
   group_by(Injury) %>% 
   summarise_at(vars(-group_cols()),  str_c, collapse="")

Upvotes: 4

Related Questions