How to use lapply and paste on multiple dataframes in a list

前端 未结 2 1851
Happy的楠姐
Happy的楠姐 2021-01-21 16:50

I can\'t combine the use of lapply and paste to combine two columns for multiple dataframes contained within a list. I\'ve looked through multiple sources, but can\'t find the a

相关标签:
2条回答
  • 2021-01-21 16:54

    I think this is what you're going for:

    lapply(mylist, function(x) paste(x$Artist, x$Song, sep=" "))
    

    We just need to define the little anonymous function to concatenate the columns.

    [[1]]
    [1] "Drake Gods Plan"        "Ed Sheeran Perfect"     "Bruno Mars Finesse"    
    [4] "Camilla Cabello Havana" "BlocBoy Look Alive"    
    
    [[2]]
    [1] "Gucci Mane Black Beatles" "Migos Bad and Boujee"    
    [3] "Daft Punk Starboy"        "Chainsmokers Closer" 
    
    0 讨论(0)
  • 2021-01-21 17:17

    A solution using purrr::map from the tidyverse

    library(tidyverse)
    
    map(list(Current, Past), ~ paste(.$Artist, .$Song))
    
    [[1]]
    [1] "Drake Gods Plan"        "Ed Sheeran Perfect"     "Bruno Mars Finesse"    
    [4] "Camilla Cabello Havana" "BlocBoy Look Alive"    
    
    [[2]]
    [1] "Gucci Mane Black Beatles" "Migos Bad and Boujee"     "Daft Punk Starboy"       
    [4] "Chainsmokers Closer"     
    
    0 讨论(0)
提交回复
热议问题