R:向值等于数据框名称的数据框添加新变量 [英] R: Add a new variable to dataframes whose value is equal to the name of the dataframes

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

问题描述

我想在全局环境中向所有数据框添加一个变量,并使新添加的列的值等于数据框名称。

I want to add a variable to ALL dataframes in my global environment and make the value of the newly added column equal to the dataframe name.

Product=c("A","A","A","A","A","A","A","A","A","A","A","A","B","B","B","C","C","C")
Day=c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Monday","Tuesday","Wednesday","Saturday","Sunday" ,"Monday")

data1=data.frame(Product, Day)

Product2=c("Z","Z","Z","Z","Z","Z","Z","Z","Z","Z","Z","Z","Y","Y","Y","X","X","X")
Day2=c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Monday","Tuesday","Wednesday","Saturday","Sunday" ,"Monday")

data2=data.frame(Product2, Day2)

我想在两个数据框中添加一列值等于数据框名称,即data1的newvar = data1和newvar = data2 f或数据2。我的实际数据框列表比这更长。

I want to add a column in both dataframes whose value is equal to the dataframe name, i.e newvar="data1" for data1 and newvar="data2" for data2. My actual data frame list is much longer than this.

任何帮助都将不胜感激。

Any help is greatly appreciated.

谢谢!

推荐答案

这是一个函数,您可以在其中传递任意数量的具名data.frames,它将返回具名data.frames列表,并添加请求的列。使用 list2env 函数(如@akrun的回答),您可以将它们放在所需的任何环境中。 (您也可以修改函数以自动产生该副作用。)

Here's a function, where you can pass any arbitrary number of named data.frames, and it will return a list of named data.frames back with the requested column added. Using the list2env function (as in @akrun's answer) you can then put these in whatever environment you want. (You could also modify the function to produce that side-effect automatically.)

f <- function(...) {
    objnames <- as.character(substitute(c(...)))[-1]
    obj <- list(...)
    out <- mapply(function(x, col) {
        x[, col] <- col
        x
    }, obj, objnames, SIMPLIFY = FALSE)
    setNames(out, objnames)
}

使用方法如下:

list2env(f(data1,data2), .GlobalEnv)
# <environment: R_GlobalEnv>
str(data1)
# 'data.frame':   18 obs. of  3 variables:
#  $ Product: Factor w/ 3 levels "A","B","C": 1 1 1 1 1 1 1 1 1 1 ...
#  $ Day    : Factor w/ 7 levels "Friday","Monday",..: 2 6 7 5 1 3 2 6 7 5 ...
#  $ data1  : chr  "data1" "data1" "data1" "data1" ...
str(data2)
# 'data.frame':   18 obs. of  3 variables:
#  $ Product2: Factor w/ 3 levels "X","Y","Z": 3 3 3 3 3 3 3 3 3 3 ...
#  $ Day2    : Factor w/ 7 levels "Friday","Monday",..: 2 6 7 5 1 3 2 6 7 5 ...
#  $ data2   : chr  "data2" "data2" "data2" "data2" ...

如果您要传递大量的命名对象而没有明确列出它们在 f()中,您可以执行以下操作:

If you had a large number of named objects that you wanted to pass without listing them explicitly in f(), you could do something like:

list2env(do.call(f, sapply(ls(pattern = "data"), as.name)), .GlobalEnv)

具有相同的结果。

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

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