[英]in R function, assign data frame object as a name? loop through function
My question is two fold. 我的问题是双重的。 First, given these three data frames:
首先,给出这三个数据框:
df1 <- data.frame(k1 = runif(6, min=0, max=100),
k2 = runif(6, min=0, max=100),
k3 = runif(6, min=0, max=100),
k4 = runif(6, min=0, max=100))
df2 <- data.frame(k1 = runif(6, min=0, max=100),
k2 = runif(6, min=0, max=100),
k3 = runif(6, min=0, max=100),
k4 = runif(6, min=0, max=100))
df3 <- data.frame(k1 = runif(6, min=0, max=100),
k2 = runif(6, min=0, max=100),
k3 = runif(6, min=0, max=100),
k4 = runif(6, min=0, max=100))
I would like to reformat and rename part of each data frame using this function: 我想使用此函数重新格式化并重命名每个数据框的一部分:
samplelist<-c("k2", "k4")
draft_fxn<-function(x, obj_name){
x.selected<-x[,c(samplelist)] #select columns of choice
colnames(x.selected)[1:2]<-paste(obj_name, colnames(x.selected), sep="_") #rename columns so they include original data frame name
return(x.selected)
}
#Example run and output:
df2_final<-draft_fxn(df2, "df2")
#output from:
head(df2_final[1:2],)
> df2_k2 df2_k4
>1 5.240274 53.03423
>2 5.042926 34.78974
First question: How can I change my function so I don't have to type in ' df2, "df2" '. 第一个问题:如何更改我的功能,所以我不必输入'df2,'df2“'。 In my draft_fxn code, I want to replace "obj_name" with whatever the name of the input data frame is.
在我的draft_fxn代码中,我想用输入数据框的名称替换“obj_name”。 In my example it is "df2".
在我的例子中它是“df2”。
Second question: How can I loop through all of my data frames? 第二个问题:我如何遍历所有数据框? Perhaps, similar to this for loop?
也许,类似于这个for循环? objs<-c(df1, df2, df3)
objs <-c(df1,df2,df3)
for (file in objs){
out<-draft_fxn(file); return(out)
} #this doesn't work though.
To answer your first question: you can obtain the name of an object x
using deparse(substitute(x))
. 要回答您的第一个问题:您可以使用
deparse(substitute(x))
获取对象x
的名称。 So to eliminate the argument obj_name
from your function, you could use 因此,要从函数中消除
obj_name
参数,可以使用
draft_fxn <- function(x){
obj_name <- deparse(substitute(x))
x.selected<-x[,c(samplelist)]
colnames(x.selected)[1:2]<-paste(obj_name, colnames(x.selected), sep="_") #rename columns so they include original data frame name
return(x.selected)
}
As to your second question, if you wanted to perform such an operation for multiple data frames, you would usually put them in a list and then lapply
the function. 至于你的第二个问题,如果你想对多个数据帧执行这样的操作,你通常会将它们放在一个列表中然后
lapply
函数提供资源。 In this case, however, it does not work, because the object name changes if you put the data frames into a list, ie deparse(substitute(x))
returns X[[i]]_
instead of the name of the individual data frame. 但是,在这种情况下,它不起作用,因为如果将数据帧放入列表中对象名称会更改,即
deparse(substitute(x))
返回X[[i]]_
而不是单个数据的名称帧。 If you wanted to do it in a loop I would suggest a different approach where you pass a vector of the names of the data frames: 如果您想在循环中执行此操作,我建议您使用不同的方法传递数据帧名称的向量:
## Names of the relevant data frames:
objNames <- c("df1", "df2", "df3")
## Function to rename the specified columns:
renameFun <- function(xString){
x <- get(xString)[,c(samplelist)]
colnames(x) <- paste(xString, samplelist, sep = "_")
x
}
## Apply function to all data frames specifed by objNames:
lapply(objNames, renameFun)
# [[1]]
# df1_k2 df1_k4
# 1 54.232123 2.178375
# 2 16.816784 23.586760
# 3 6.612874 16.509340
# 4 92.399588 71.133637
# 5 22.917838 8.127079
# 6 43.563411 21.118758
#
# ...
So your function is not well-specified because you're defining samplelist
outside of the function and then calling it inside. 所以你的函数没有明确指定,因为你在
samplelist
之外定义samplelist
然后在里面调用它。 The problem with that is that if you don't have samplelist
defined, the function will return an error, ie it's not self-contained. 问题是如果你没有定义
samplelist
,该函数将返回一个错误,即它不是自包含的。
Here's an alternative: 这是另一种选择:
draft_fxn<-function(x, cols =...){
x.selected<-data.frame(x[, cols]) #select columns of choice
colnames(x.selected)<-paste(deparse(substitute(x)), colnames(x.selected), sep="_") #rename columns so they include original data frame name
return(x.selected)
}
Note that the cols
argument can vary (as long as it's positive and not larger than the number of columns in your data frame). 请注意,
cols
参数可以变化(只要它是正数且不大于数据框中的列数)。
This returns: 返回:
> df2_final<- draft_fxn(df2, cols = c("k2", "k4"))
> head(df2_final)[1:2,]
df2_k2 df2_k4
1 21.62533 2.256182
2 64.83556 67.705705
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.