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

给你一囗甜甜゛ 提交于 2019-12-20 02:59:14

问题


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 answer.

This answer Apply paste over a list of vectors to get a list of strings is about combining rows within lists, not combining columns to obtain a vector.

This answer explains how to select columns but not paste them together using lapply on a list of dataframes

This page explains how to access columns in lists, but not how to combine them together https://www.datacamp.com/community/tutorials/r-tutorial-apply-family

This answer is the closest, but I can't use transform on text Performing an operation on multiple columns in a list of data

The code below is an example which successfully creates the output, but does not use lapply. I will be applying this function over many different dataframes, so need to use lapply. Presumably I could use a for i loop, but lapply is probably cleaner. I need to use lapply, rather than sapply, as the dataframes will have different lengths.

I want to replace the three lines that start output1, output2 and output with an lapply function that will work across the two dataframes in this example. I think it should be something like

output <-lapply(mylist, paste($Artist,$Song))

But that doesn't work. Thanks in advance.

Artist <- c("Drake", "Ed Sheeran", "Bruno Mars", "Camilla Cabello", "BlocBoy")    
Song <- c("Gods Plan", "Perfect", "Finesse", "Havana", "Look Alive")
Current <- data.frame(cbind(Artist,Song))
Artist <- c("Gucci Mane", "Migos", "Daft Punk", "Chainsmokers")
Song <- c("Black Beatles", "Bad and Boujee", "Starboy", "Closer")
Past <- data.frame(cbind(Artist,Song))

mylist <- list(Current,Past)

output1  <- paste(mylist[[1]]$Artist,mylist[[1]]$Song)
output2  <- paste(mylist[[2]]$Artist,mylist[[2]]$Song)
output <-list(output1,output2)
print(output)

回答1:


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" 



回答2:


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"     


来源:https://stackoverflow.com/questions/49056049/how-to-use-lapply-and-paste-on-multiple-dataframes-in-a-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!