0
votes

I would like to change the names of a tibble using another tibble which work as a dictionary.

however, I don't know if the order of the table's variables are the same as the dictionary's rows.

The only thing assured is that the first variable of the names table holds all the variable names in the data

examples:

in the simplest case (not this problem) the order of the variables is equal to the ordering of the dictionary's rows

cars <- as_tibble(mtcars)

wanted_names <- tribble(~names, ~new,
                        "mpg","arbitary1",
                        "cyl","arbitary2",
                        "disp","x",
                        "hp","y",
                        "drat","f",
                        "wt","sss",
                        "qsec","ffff",
                        "vs","wvegsb",
                        "am","dhyhjn",
                        "gear","scsssfsf",
                        "carb","arbitaryn")
names(cars)

[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb"

I could just use-

names(cars)<-wanted_names$new

yet I cannot vouch for the order of wanted_names or cars, so it actually looks similar to this:

cars <- as_tibble(mtcars) %>% 
  select(wt,hp,carb,everything())

wanted_names <- tribble(~names, ~new,
                        "wt","sss",
                        "qsec","ffff",
                        "vs","wvegsb",
                        "am","dhyhjn",
                        "disp","x",
                        "hp","y",
                        "drat","f",
                        "cyl","arbitary2",
                        "gear","scsssfsf",
                        "mpg","arbitary1",
                        "carb","arbitaryn")

any method that could assure correct renaming will be appreciated.

1
You can just join your tibbles. carsNew <- dplyr::left_join(cars, wanted_names, by = "names") %>% select(-names)SKyJim
thanks but the problem is that cars is in a wide format, so there is no common variableamann

1 Answers

1
votes

One approach is to use match.

names(cars) <- wanted_names$new[match(names(cars),wanted_names$names)]
cars
#   arbitary1 arbitary2     x     y     f   sss  ffff wvegsb dhyhjn scsssfsf arbitaryn
#       <dbl>     <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>    <dbl>     <dbl>
# 1      21           6  160    110  3.9   2.62  16.5      0      1        4         4
# 2      21           6  160    110  3.9   2.88  17.0      0      1        4         4
# 3      22.8         4  108     93  3.85  2.32  18.6      1      1        4         1
# 4      21.4         6  258    110  3.08  3.22  19.4      1      0        3         1
# 5      18.7         8  360    175  3.15  3.44  17.0      0      0        3         2
# 6      18.1         6  225    105  2.76  3.46  20.2      1      0        3         1
# 7      14.3         8  360    245  3.21  3.57  15.8      0      0        3         4
# 8      24.4         4  147.    62  3.69  3.19  20        1      0        4         2
# 9      22.8         4  141.    95  3.92  3.15  22.9      1      0        4         2
#10      19.2         6  168.   123  3.92  3.44  18.3      1      0        4         4
# … with 22 more rows