Home > Software engineering >  Concatenate data from multiple rows
Concatenate data from multiple rows

Time:11-30

I have a dataframe that looks like this:

ks_id direction time text
1a down 1
1a up 3 h
1b down 4
1b up 7 e

I would like to transform this to a dataframe where the different directions are in one line, e.g.:

ks_id down_time up_time text
1a 1 3 h
1b 4 7 e

I've tried grouping by ks_id and then mutating, but have not had success with this. How can I convert the df as above?

CodePudding user response:

A possible solution:

library(tidyverse)

df <- data.frame(
  stringsAsFactors = FALSE,
  ks_id = c("1a", "1a", "1b", "1b"),
  direction = c("down", "up", "down", "up"),
  time = c(1L, 3L, 4L, 7L),
  text = c(NA, "h", NA, "e")
)

df %>% 
  pivot_wider(ks_id, names_from = "direction", values_from = c(time, text)) %>%
  unite(text, c("text_up", "text_down"), na.rm = T) 

#> # A tibble: 2 × 4
#>   ks_id time_down time_up text 
#>   <chr>     <int>   <int> <chr>
#> 1 1a            1       3 h    
#> 2 1b            4       7 e

CodePudding user response:

Just `reshape it.

reshape(dat, idvar='ks_id', timevar='direction', direction='wide')[-3]
#   ks_id time.down time.up text.up
# 1    1a         1       3       h
# 3    1b         4       7       e

Data:

dat <- structure(list(ks_id = c("1a", "1a", "1b", "1b"), direction = c("down", 
"up", "down", "up"), time = c(1L, 3L, 4L, 7L), text = c(NA, "h", 
NA, "e")), class = "data.frame", row.names = c(NA, -4L))
  •  Tags:  
  • r
  • Related