简体   繁体   English

使用 dplyr rename_if 切换列

[英]Switched columns with dplyr rename_if

Trying to rename some data frame columns using dplyr rename_if and lists of old and new patterns, some column names end up switched in the output.尝试使用 dplyr rename_if和新旧模式列表重命名一些数据框列,一些列名最终在 output 中切换。

head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

lst <- list(Old=c("mpg", "cyl", "disp", "carb", "wt", "gear"), New=c("Miles per Gallon", "Cylinder", "Displacement", "Carburator", "Weight", "Gear"))

mtcars %>% 
rename_if(names(.) %in% lst$Old,
          function(x){
             lst$New[which(lst$Old %in% x)]}) %>% 
head()
                  Miles per Gallon Cylinder Displacement  hp drat Carburator
Mazda RX4                     21.0        6          160 110 3.90      2.620
Mazda RX4 Wag                 21.0        6          160 110 3.90      2.875
Datsun 710                    22.8        4          108  93 3.85      2.320
Hornet 4 Drive                21.4        6          258 110 3.08      3.215
Hornet Sportabout             18.7        8          360 175 3.15      3.440
Valiant                       18.1        6          225 105 2.76      3.460
                   qsec vs am Weight Gear
Mazda RX4         16.46  0  1      4    4
Mazda RX4 Wag     17.02  0  1      4    4
Datsun 710        18.61  1  1      4    1
Hornet 4 Drive    19.44  1  0      3    1
Hornet Sportabout 17.02  0  0      3    2
Valiant           20.22  1  0      3    1

We can see that the wt and Carburator columns have been switched.我们可以看到wtCarburator列已经切换。

How to rename columns using rename_if when the reference name lists are not in the same order as the data frame columns?当引用名称列表与数据框列的顺序不同时,如何使用rename_if重命名列?

EDIT The use of rename_at(lst$Old, ~lst$New) does not work when the reference lists contain names not present in the particular data frame columns.编辑当引用列表包含特定数据框列中不存在的名称时,使用rename_at(lst$Old, ~lst$New)不起作用。

For example with:例如:

lst <- list(Old=c("mpg", "cyl", "disp", "carb", "wt", "gear", "xtra"), New=c("Miles per Gallon", "Cylinder", "Displacement", "Carburator", "Weight", "Gear", "ExtraCol"))

(see the xtra and ExtraCol names) (参见xtraExtraCol名称)

Instead of rename_if , try with rename_at since you have names of columns that you want to replace.尝试使用rename_at而不是rename_if ,因为您有要替换的列的名称。

library(dplyr)
head(mtcars) %>% rename_at(lst$Old, ~lst$New)

However, _at / _if / _all variants have been superseded so try with rename_with .但是, _at / _if / _all变体已被取代,因此请尝试使用rename_with

head(mtcars) %>% rename_with(~lst$New, lst$Old)

#                  Miles per Gallon Cylinder Displacement  hp drat Weight  qsec vs am Gear Carburator
#Mazda RX4                     21.0        6          160 110 3.90  2.620 16.46  0  1    4          4
#Mazda RX4 Wag                 21.0        6          160 110 3.90  2.875 17.02  0  1    4          4
#Datsun 710                    22.8        4          108  93 3.85  2.320 18.61  1  1    4          1
#Hornet 4 Drive                21.4        6          258 110 3.08  3.215 19.44  1  0    3          1
#Hornet Sportabout             18.7        8          360 175 3.15  3.440 17.02  0  0    3          2
#Valiant                       18.1        6          225 105 2.76  3.460 20.22  1  0    3          1

In base R, we can do this using match :在 base R 中,我们可以使用match来做到这一点:

names(mtcars)[match(lst$Old, names(mtcars))] <- lst$New

In case, there are values in lst which are not present in names we can first filter them and then use the above method.如果lst中有名称中不存在的值,我们可以先过滤它们,然后使用上述方法。

inds <- lst$Old %in% names(mtcars)
lst <- lapply(lst, `[`, inds)

Replacing which() by match() keeps indexes by order of occurrence and solves the problemmatch()替换which()按出现顺序保留索引并解决问题

mtcars %>% rename_if(names(.) %in% lst$Old, function(x){lst$New[match(x, lst$Old)]})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM