如何使用lapply并将其粘贴到列表中的多个数据框上 [英] How to use lapply and paste on multiple dataframes in a list

查看:108
本文介绍了如何使用lapply并将其粘贴到列表中的多个数据框上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能结合使用lapply和paste来组合两个列,以用于列表中包含的多个数据框.我已经查看了多个来源,但找不到答案.

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.

此答案说明了如何选择列而不将它们粘贴在一起在上面使用lapply数据框列表

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

下面的代码是一个示例,该示例成功创建了输出,但未使用lapply.我将在许多不同的数据帧上应用此功能,因此需要使用lapply.大概我可以使用for for循环,但是lapply可能更干净.我需要使用lapply而不是sapply,因为数据帧的长度将不同.

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.

我想用一个lapply函数替换开始output1,output2和output的三行,该函数将在此示例中跨两个数据帧工作.我认为应该是

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)

推荐答案

我认为这是您要使用的:

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" 

这篇关于如何使用lapply并将其粘贴到列表中的多个数据框上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆