codedancer
codedancer

Reputation: 1634

Using purrr way to convert listed columns to string in R

I tried to convert columns with listed vectors into string. Instead of using pivot_wider to make the dataframe wide, I'd like to unlist the vectors and keep them in the same cell. So the dataframe has the same number of columns. I found a solution using sapply to convert one column.

df$a <- sapply(df$a, paste, collapse=",")

I am wondering if there's a way I could do this in a purrr way and do it to multiple listed columns. I have provided my dput.

structure(list(rowid = c(3995L, 7899L), a = list(c("", "3007766601", 
"1710034", "1036761", "3006260740", "9681413", "3004080548", 
"1018470", "3010966701"), c("3013099020", "", "3005254598", "3007556128", 
"3003600763", "3011347852")), e = list(c("K172483", 
"K992729", "K043249", "K072487", "K033575", "K011925", "K180588", 
"K982399", "K150662", "K110703"), c("K913525", "K880518", "K960198", 
"K141672", "K926056", "K935580", "K953910", "K982706", "K911739", 
"K010762", "K965013", "K120388", "K760429", "K940294", "K980322", 
"K981131", "K896387", "K170946", "K003266", "K992506", "K003837", 
"K121303", "K123603", "K190024"))), row.names = c(3995L, 7899L
), class = "data.frame")

Upvotes: 2

Views: 122

Answers (2)

akrun
akrun

Reputation: 887148

Using map from purrr

library(purrr)
library(stringr)
map_dfr(df, ~ map_chr(.x, str_c, collapse=", "))

-output

# A tibble: 2 x 3
#  rowid a                                                    e                                                                                        
#  <chr> <chr>                                                <chr>                                                                                    
#1 3995  , 3007766601, 1710034, 1036761, 3006260740, 9681413… K172483, K992729, K043249, K072487, K033575, K011925, K180588, K982399, K150662, K110703 
#2 7899  3013099020, , 3005254598, 3007556128, 3003600763, 3… K913525, K880518, K960198, K141672, K926056, K935580, K953910, K982706, K911739, K010762… 

Upvotes: 1

GKi
GKi

Reputation: 39667

You can use two sapply:

sapply(df, function(x) sapply(x, paste, collapse=","))
#     rowid  a                                                                              e                                                                                                                                                                                                
#[1,] "3995" ",3007766601,1710034,1036761,3006260740,9681413,3004080548,1018470,3010966701" "K172483,K992729,K043249,K072487,K033575,K011925,K180588,K982399,K150662,K110703"                                                                                                                
#[2,] "7899" "3013099020,,3005254598,3007556128,3003600763,3011347852"                      "K913525,K880518,K960198,K141672,K926056,K935580,K953910,K982706,K911739,K010762,K965013,K120388,K760429,K940294,K980322,K981131,K896387,K170946,K003266,K992506,K003837,K121303,K123603,K190024"

Upvotes: 2

Related Questions