Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename all column names in tibble by passing a character vector?

Tags:

r

dplyr

tibble

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!

like image 549
BigGiantHead Avatar asked Feb 12 '20 08:02

BigGiantHead


2 Answers

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])
like image 182
Ronak Shah Avatar answered Sep 29 '22 23:09

Ronak Shah


Building upon the rightly suggested base R solutions, I wrote a small pipe-compatible functions that does the job:

set_names <- function(x, colnames) {
  # Do some checks
  if (! "data.frame" %in% class(x)) stop("Argument must be a data.frame")
  if (class(colnames) != "character") stop("New names must be character")
  if (length(names(x)) != length(colnames)) stop("Invalid nr. of new names")
   # Actual replacement of  column names     
  names(x) <- colnames
  return(x)
}

Example usage:

X <- read_xslx(fname,sname) %>%     # Read the data
  select(1:2) %>%                   # Use only first 2 columns
  set_names(c("name","value")) %>%  # set proper name
  filter(value>5)                   # select data of interest
like image 43
p.w.bogaart Avatar answered Sep 29 '22 23:09

p.w.bogaart