在R函数中,将数据帧对象指定为名称?环通函数

在R函数中,将数据帧对象指定为名称?环通函数,r,function,loops,dataframe,R,Function,Loops,Dataframe,我的问题有两个方面。首先,考虑到这三个数据帧: 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=

我的问题有两个方面。首先,考虑到这三个数据帧:

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))
## 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
# 
# ...

df1因此您的函数没有很好地指定,因为您在函数外部定义
samplelist
,然后在内部调用它。问题是如果没有定义
samplelist
,函数将返回一个错误,即它不是自包含的

这里有一个替代方案:

  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)
}

回答第一个问题:可以使用
deparse(替换(x))
获取对象的名称
x
。因此,要从函数中删除参数
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)
}  

从一开始,您可能会比在变量名本身中有一堆松散相关的变量和数据更好。
> 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
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)
}  
## 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
# 
# ...