Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change col names with the first row of a data in dplyr chain

Tags:

r

dplyr

I try to rename the column names with the first row of the data.

use first row data as column names in r

use %>% with replacement functions like colnames()<-

The problem that I counter is that doing this process without breaking the dplyr pipeline as I would like to continue doing some other stuff after renaming the columns.

There is comment in this post about rename function dplyr::rename may be more convenient if you are only (re)naming a few out of many columns (it requires writing both the old and the new name; see @Richard Scriven's answer)

However, in my real data the number of columns is not fixed and so I need to use something like to select the columns select(X9:max(ncol(.)))

df <- data.frame(replicate(10,sample(100,2,rep=TRUE)))


  X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1 77 40 45 98 75 17  5 33 53  94
2 43 67 82 42 63 90 14 65  4  98

library(dplyr)
df1 <- df %>%
  select(X8,X9,X10)%>%
  ....

the expected output after selection and renaming the columns

  33 53  94
1 65  4  98
like image 985
Alexander Avatar asked May 30 '18 17:05

Alexander


People also ask

How do I change column names in R with dplyr?

rename() function from dplyr takes a syntax rename(new_column_name = old_column_name) to change the column from old to a new name. The following example renames the column from id to c1 . The operator – %>% is used to load the renamed column names to the data frame.

How do you make the first row of a dataset the column names in R?

To promote the first row to column headers, select Home > Use First Row As Headers.

How do you reassign column names in R?

How to change column headers of a data-frame in R? colnames () function can be used to change the column names of a data-frame column in R. colnames () function can be used for changing one column name at a time, also all the column names can be changed at once.


2 Answers

You can easily do it by naming columns as first row and then remove the first row.

library(dplyr)
df <- df %>%
  select(X8,X9,X10)

names(df) <- df[1,]
df <- df[-1,]

like image 155
Macosso Avatar answered Nov 15 '22 05:11

Macosso


You could do something like this

library(tidyverse)
df <- data.frame(replicate(10,sample(100,2,rep=TRUE)))
df
#>   X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
#> 1 22 64 23 11 36 46 87 57 90  96
#> 2 62 46 15  9 77 84 70 32 71   8

cols_2_select <- c('X8','X9','X10')

df %>%
  select(all_of(cols_2_select)) %>% 
  set_names(df %>% select(all_of(cols_2_select)) %>% slice(1) %>% as.character()) %>% 
  slice(-1)
#>   57 90 96
#> 1 32 71  8

Created on 2021-04-16 by the reprex package (v1.0.0)

like image 33
cropgen Avatar answered Nov 15 '22 04:11

cropgen