Reputation: 4645
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
Upvotes: 8
Views: 4452
Reputation: 226587
Since I don't see this here and it seems to be simpler/more tidyverse-ish than other options: set_names(slice(.,1))
(to name by the first row; explicit coercion to character isn't necessary) followed by slice(-1)
(to discard the first row since you don't need it any more) ...
library(tidyverse)
df1 <- (df
%>% select(X8:X10)
%>% set_names(slice(.,1))
%>% slice(-1)
)
(set_names
taken from @cropgen's answer)
Upvotes: 0
Reputation: 2022
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)
Upvotes: 2
Reputation: 1439
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,]
Upvotes: 2
Reputation: 73377
set.seed(502)
df <- data.frame(replicate(10, sample(100, 2, rep=TRUE)))
> df
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1 79 6 82 23 36 58 95 30 60 42
2 89 77 9 13 79 97 1 10 48 66
In base R we can do
df1 <- "colnames<-"(df[2 , x <- paste0("X", 8:10)], df[1, x])
> df1
30 60 42
2 10 48 66
Upvotes: 0