0

我正在尝试了解循环,我目前有一长串数据框,我需要进入一堆这些数据框并重命名一些变量。我有一个函数,但我正在努力构建一种智能的方式来遍历我的列表(实际列表比下面的示例长得多),同时在重命名时应用不同的后缀前缀。

希望我下面的工作示例将说明这种情况。我想我可以将最后一部分构建成两个循环,但我似乎无法弄清楚如何在循环内写入列表中的数据框。

任何帮助,将不胜感激!

data(mtcars)

mtcarsList <- list(mtcars1 = mtcars, mtcars2 = mtcars,
                   mtcarsA = mtcars, mtcars = mtcars  )

# function I use to renames a specific number of variables
baRadd <- function(df, vector, suffix){
               names(df) <- ifelse(names(df) %in% vector,names(df), 
                                  paste(suffix, names(df), sep = ".")) 
               return(df)}


foo <- c("mpg", "cyl", "disp")
suffix1 <- "bar"
suffix2 <- "barBAR"
suffix3 <- "barBARbar"

mtcarsList$mtcars1 <- baRadd(mtcarsList$mtcars1, foo, suffix1)
mtcarsList$mtcars2 <- baRadd(mtcarsList$mtcars2, foo, suffix2)
mtcarsList$mtcarsA <- baRadd(mtcarsList$mtcarsA, foo, suffix3) 

 names(mtcarsList$mtcars1)
 # [1] "mpg"      "cyl"      "disp"     "bar.hp"   "bar.drat" "bar.wt"  
 # [7] "bar.qsec" "bar.vs"   "bar.am"   "bar.gear" "bar.carb"
 names(mtcarsList$mtcars2)
 # [1] "mpg"         "cyl"         "disp"        "barBAR.hp"   "barBAR.drat"
 # [6] "barBAR.wt"   "barBAR.qsec" "barBAR.vs"   "barBAR.am"   "barBAR.gear"
 # [11] "barBAR.carb"
 names(mtcarsList$mtcarsA)
 # [1] "mpg"            "cyl"            "disp"           "barBARbar.hp"  
 # [5] "barBARbar.drat" "barBARbar.wt"   "barBARbar.qsec" "barBARbar.vs"  
 # [9] "barBARbar.am"   "barBARbar.gear" "barBARbar.carb"
 names(mtcarsList$mtcars)
 # [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
 # [11] "carb"

更新,

根据下面 DWin 的回复,我编写了解决我的问题的脚本,

# rm(list = ls(all = TRUE)) ## Clear workspace

data(mtcars)

mtcarsList <- list(mtcars1 = mtcars, mtcars2 = mtcars, 
                    mtcarsA = mtcars, mtcars = mtcars)

## function I use to renames a specific number of variables
baRadd <- function(df, vector, suffix){
               names(df) <- ifelse(names(df) %in% vector,names(df), 
                                  paste(suffix, names(df), sep = ".")) 
               return(df)}


suffixes <- c('A', 'B', 'C') # suffixes to be added to the three dfTO
whatNOTtoRename <- c("mpg", "cyl", "disp") 
# variables within the data frame I do not     want to renames
dfTO <- c('mtcars1','mtcars2','mtcarsA') 
# the specific data frames I need to rename


# str(mtcarsList)
mtcarsList[ names( mtcarsList[dfTO]) ] <- 
     mapply(baRadd, df=mtcarsList[dfTO], 
                    suffix= suffixes, 
                    MoreArgs=list(vector=whatNOTtoRename) , SIMPLIFY=FALSE)

str(mtcarsList)
4

1 回答 1

3

看起来好像mapply可以完成这个任务:

> newList <- mapply(baRadd, df=mtcarsList[1:3], suffix= c(suffix1, suffix2, suffix3), MoreArgs=list(vector=foo) , SIMPLIFY=FALSE)
> str(newList)
List of 3
 $ mtcars1:'data.frame':    32 obs. of  11 variables:
  ..$ mpg     : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
  ..$ cyl     : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
  ..$ disp    : num [1:32] 160 160 108 258 360 ...
  ..$ bar.hp  : num [1:32] 110 110 93 110 175 105 245 62 95 123 ...
  ..$ bar.drat: num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
  ..$ bar.wt  : num [1:32] 2.62 2.88 2.32 3.21 3.44 ...
  ..$ bar.qsec: num [1:32] 16.5 17 18.6 19.4 17 ...
  ..$ bar.vs  : num [1:32] 0 0 1 1 0 1 0 1 1 1 ...
  ..$ bar.am  : num [1:32] 1 1 1 0 0 0 0 0 0 0 ...
  ..$ bar.gear: num [1:32] 4 4 4 3 3 3 3 4 4 4 ...
  ..$ bar.carb: num [1:32] 4 4 1 1 2 1 4 2 2 4 ...
 $ mtcars2:'data.frame':    32 obs. of  11 variables:
  ..$ mpg        : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
  ..$ cyl        : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
  ..$ disp       : num [1:32] 160 160 108 258 360 ...
  ..$ barBAR.hp  : num [1:32] 110 110 93 110 175 105 245 62 95 123 ...
  ..$ barBAR.drat: num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
  ..$ barBAR.wt  : num [1:32] 2.62 2.88 2.32 3.21 3.44 ...
  ..$ barBAR.qsec: num [1:32] 16.5 17 18.6 19.4 17 ...
  ..$ barBAR.vs  : num [1:32] 0 0 1 1 0 1 0 1 1 1 ...
  ..$ barBAR.am  : num [1:32] 1 1 1 0 0 0 0 0 0 0 ...
  ..$ barBAR.gear: num [1:32] 4 4 4 3 3 3 3 4 4 4 ...
  ..$ barBAR.carb: num [1:32] 4 4 1 1 2 1 4 2 2 4 ...
 $ mtcarsA:'data.frame':    32 obs. of  11 variables:
  ..$ mpg           : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
  ..$ cyl           : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
  ..$ disp          : num [1:32] 160 160 108 258 360 ...
  ..$ barBARbar.hp  : num [1:32] 110 110 93 110 175 105 245 62 95 123 ...
  ..$ barBARbar.drat: num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
  ..$ barBARbar.wt  : num [1:32] 2.62 2.88 2.32 3.21 3.44 ...
  ..$ barBARbar.qsec: num [1:32] 16.5 17 18.6 19.4 17 ...
  ..$ barBARbar.vs  : num [1:32] 0 0 1 1 0 1 0 1 1 1 ...
  ..$ barBARbar.am  : num [1:32] 1 1 1 0 0 0 0 0 0 0 ...
  ..$ barBARbar.gear: num [1:32] 4 4 4 3 3 3 3 4 4 4 ...
  ..$ barBARbar.carb: num [1:32] 4 4 1 1 2 1 4 2 2 4 ...

如果您想将该结果分配给 mtcarsList[1:3],那也应该是可能的。

对您的评论:这成功了....

mtcarsList[ names( mtcarsList[1:3]) ] <- 
         mapply(baRadd, df=mtcarsList[1:3], 
                        suffix= c(suffix1, suffix2, suffix3), 
                        MoreArgs=list(vector=foo) , SIMPLIFY=FALSE)

 # omitted output of str(mtcarsList)  ....
于 2012-08-26T22:53:34.037 回答