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
df %>% setNames(as.character(df[1,]))
– IceCreamToucan(X9:end)
, as well as get the names from the first row? – Calum Youselect
df1 <- df %>% select(X8,X9,X10)%>% setNames(as.character(.[1,X9:max(ncol(.))]))
– Alexander