Home > Software design >  How to create a list of elements in columns based on another in R dataframe
How to create a list of elements in columns based on another in R dataframe

Time:10-16

I have a table like this:

col1 col2 col3
x 1 4
x 2 5
x 3 6
y 1 4
y 2 5
y 3 6

I want to combine the elements in the 2nd and 3rd columns as a list based off of what they corresponded to in the first column like this:

col1 col2 col3
x [1, 2, 3] [4, 5, 6]
y [1, 2, 3] [4, 5, 6]

How do I go about doing this? Thanks

CodePudding user response:

Does this work:

library(dplyr)
library(stringr)

df %>% group_by(col1) %>% summarise(col2 = str_c('[',toString(col2),']'))
# A tibble: 2 x 2
  col1  col2     
  <chr> <chr>    
1 x     [1, 2, 3]
2 y     [1, 2, 3]

CodePudding user response:

This solution using dplyr returns lists for the values (not sure if you wanted character strings or lists but the brackets in your example led me to think you wanted lists...)

library(dplyr)
df1 %>% 
  group_by(col1) %>% 
  summarise(across(, list))

CodePudding user response:

You can do this with tidyr::pivot_wider

library(tidyr)

df %>% 
  pivot_wider(id_cols = col1,
              values_from = -col1,
              values_fn = list)

The default values_fn is list so it's technically not needed here, but to suppress the warning message I made it explicit.

CodePudding user response:

Using aggregate from base R

aggregate(.~ col1, df1, list)
  col1    col2    col3
1    x 1, 2, 3 4, 5, 6
2    y 1, 2, 3 4, 5, 6

data

df1 <- structure(list(col1 = c("x", "x", "x", "y", "y", "y"), col2 = c(1L, 
2L, 3L, 1L, 2L, 3L), col3 = c(4L, 5L, 6L, 4L, 5L, 6L)),
 class = "data.frame", row.names = c(NA, 
-6L))
  • Related