3
votes

I have a tibble named X of multiple columns (over 500) which are named in format of "X"+integer. The tibble looks like this.

# A tibble: 7,352 x 561
      X1       X2     X3     X4     X5     X6        
    <dbl>    <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  
 1 0.289 -0.0203  -0.133 -0.995 -0.983 -0.914 
 2 0.278 -0.0164  -0.124 -0.998 -0.975 -0.960 

The txt file didn't contain column names, but they are in another txt file which I have read into another tibble. This tibble is size of 561x1.

What I wanted to do is to rename all of the column names of tibble x by using row values (=converting the tibble to character vector named y).

I have tried dplyr function rename_all without a result.

Here's an example which I believe is quite close to actually working, but I don't quite understand how to work with function list

> rename_all(x,list(paste0(y)))

The above command in RStudio command line produces following error message:

Error in get(.x, .env, mode = "function") : 
  object 'tBodyAcc-mean()-X' of mode 'function' was not found

The tBodyAcc-mean()-X is the value in the first row of character vector y.

I have tried to googling the error message, but so far I have no idea what is causing that and how should I modify the rename_all command to get it working.

Any help is much appreciated!

2
Why not just names(df) <- y?Sotos

2 Answers

4
votes

You could use :

library(dplyr)

x %>% rename_all(~y %>% pull(col))
#     a       b      c      d      e      f
#1 0.289 -0.0203 -0.133 -0.995 -0.983 -0.914
#2 0.278 -0.0164 -0.124 -0.998 -0.975 -0.960

Or simply in base R :

names(x) <- y$col

where col is the column name in y dataframe.

data

x <- structure(list(X1 = c(0.289, 0.278), X2 = c(-0.0203, -0.0164), 
X3 = c(-0.133, -0.124), X4 = c(-0.995, -0.998), X5 = c(-0.983, 
-0.975), X6 = c(-0.914, -0.96)), class = "data.frame", row.names = c("1", "2"))
y <- tibble(col = letters[1:6])
0
votes

Using rename_at, you may write the following code:

x1 <- x %>% 
        rename_at(., names(x), funs(c(letters[1:6])))