R:从函数向数据框添加一个新列 [英] R: add a new column to dataframes from a function

查看:99
本文介绍了R:从函数向数据框添加一个新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  dftest_tw<  -  structure(list(text = c(RT @ BitMEXdotcom:创历史新高:过去24小时内营业额达到5亿美元,其中80%以上在XBTUSD上。祝贺该团队并感谢您对我们的...,
RT @Crowd_indicator:谢谢这个不错的视频,@Nicholas_Merten,
RT @Crowd_indicator:评论#Cindicator by DataDash:t.co/D0da3u5y3V
),Tweet.id = c(896858423521837057,896858275689398272, 896858135314538497
),created.date = structure(c(17391,17391,17391),class =Date),
created.week = c(33,33,33))。 names = c(text,Tweet.id,
created.date,created.week),row.names = c(NA,-3L),class = c(tbl_df ,
tbl,data.frame))

另一个:

  dftest2_tw<  -  dftest_tw 

我有这个df列表:

  myUserList<  -  ls(, pattern =_tw) 

我想要做的是:

< 1-添加一个名为 Twitter.name

的新列

2-用df name ,所有这些都在一个函数中。以下代码适用于每个df一个接一个:

  dftest_tw%>%rowwise()%>%mutate( Twitter.name = myUserList [1])$ ​​b $ b  

期望的结果是:

  MyRes<  -  structure(list(text = c(RT @BitMEXdotcom:创新高:过去24小时超过5亿美元的营业额,超过
RT @Crowd_indicator:感谢你这个漂亮的视频,@Nicholas_Merten,
RT @Crowd_indicator:感谢你给我的团队带来的感谢,通过DataDash评论#Cindicator:t.co/D0da3u5y3V
),Tweet.id = c(896858423521837057,896858275689398272,896858135314538497
),created.date = structure(c( (33,33,33),retweet = c(0,0,0),custom = c(0,$ b(0,0,0)),class =Date),
created.week = $ b 0,0),Twitter.name = c(dftest_tw,dftest_tw,dftest_tw
)),.Names = c(text,Tweet.id,created.date ,created.week,
retweet,custom,Twitte row.names = c(NA,-3L))
tbl_df,tbl,data.frame / code>

当编写一个函数被应用到我所有的df(超过100)时,我不能实现这一目标。我们可以使用 tidyverse 选项。

使用 mget 获取多个字符串对象的值,然后使用 purrr map2 获取多个字符串对象的值c $ c>,在 list 的每个数据集中创建新的列'Twitter.name',并使用相应的'myUserList'字符串元素

  library(tidyverse)
lst < - mget(myUserList)%>%
map2(myUserList,〜mutate(.data = .x ,Twitter.name = .y))

如果我们需要修改全局环境中的对象,使用 list2env

  list2env(lst,envir = .GlobalEnv)


I have many tibbles similar to this:

dftest_tw <- structure(list(text = c("RT @BitMEXdotcom: A new high: US$500M turnover in the last 24 hours, over 80% of it on $XBTUSD. Congrats to the team and thank you to our u…", 
"RT @Crowd_indicator: Thank you for this nice video, @Nicholas_Merten", 
"RT @Crowd_indicator: Review of #Cindicator by DataDash: t.co/D0da3u5y3V"
), Tweet.id = c("896858423521837057", "896858275689398272", "896858135314538497"
), created.date = structure(c(17391, 17391, 17391), class = "Date"), 
    created.week = c(33, 33, 33)), .Names = c("text", "Tweet.id", 
"created.date", "created.week"), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"))

For testing, we add another one:

dftest2_tw <- dftest_tw

I have this list of my df:

myUserList <- ls(,pattern = "_tw")

What I am looking to do is:

1- add a new column named Twitter.name

2- fill the column with the df name, all this in a function. The following code works for each df taken one by one:

dftest_tw %>% rowwise() %>% mutate(Twitter.name = myUserList[1])

The desired result is this:

MyRes <- structure(list(text = c("RT @BitMEXdotcom: A new high: US$500M turnover in the last 24 hours, over 80% of it on $XBTUSD. Congrats to the team and thank you to our u…", 
"RT @Crowd_indicator: Thank you for this nice video, @Nicholas_Merten", 
"RT @Crowd_indicator: Review of #Cindicator by DataDash: t.co/D0da3u5y3V"
), Tweet.id = c("896858423521837057", "896858275689398272", "896858135314538497"
), created.date = structure(c(17391, 17391, 17391), class = "Date"), 
    created.week = c(33, 33, 33), retweet = c(0, 0, 0), custom = c(0, 
    0, 0), Twitter.name = c("dftest_tw", "dftest_tw", "dftest_tw"
    )), .Names = c("text", "Tweet.id", "created.date", "created.week", 
"retweet", "custom", "Twitter.name"), class = c("rowwise_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -3L))

When it comes to write a function to be thereafter been applied to all my df (more than 100), I can't achieve it. Any help would be appreciated.

解决方案

We can use tidyverse options. Get the value of multiple string objects with mget, then with map2 from purrr, create the new column 'Twitter.name in each dataset of the list with corresponding string element of 'myUserList`

library(tidyverse) 
lst <- mget(myUserList) %>% 
          map2(myUserList,  ~mutate(.data = .x, Twitter.name = .y))

If we need to modify the objects in the global environment, use list2env

list2env(lst, envir = .GlobalEnv)

这篇关于R:从函数向数据框添加一个新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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