优化(简单)循环以重命名列表内部

时间:2012-08-26 22:07:05

标签: r list loops indexing

我正在尝试学习循环,我目前有一长串数据框,我需要进入一堆这些数据框并重命名一些变量。我有一个函数,但我正在努力构建一个通过我的列表循环的智能方法(真正的列表比下面的例子长得多)并同时应用变化的后缀前缀母鸡重命名。

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

任何帮助将不胜感激!

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)

1 个答案:

答案 0 :(得分: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)  ....