df1 <- data.frame(
cola = c('1',NA,'c','1','1','e','1',NA,'c','d'),
colb = c("A",NA,"C","D",'a','b','c','d','c','d'),
colc = c('a',NA,'c','d','a',NA,'c',NA,'c','d'),stringsAsFactors = TRUE)
df2<-data.frame(name=c('cola','colc','colb'),
altname=c('a','c','b'))
df1 %>% table %>% data.frame(.)
Result of above codes as:
cola colb colc Freq
1 1 a a 1
2 c a a 0
I want to change columns name of result based on df2
(for example,change colb
to b
),the expected result as:
a b c Freq
1 1 a a 1
2 c a a 0
How to do it?
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.
Rename Column in R Dataframe using rename() rename() is the method available in the dplyr package, which is used to change the particular column name present in the data frame. The operator – %>% is used to load the renamed column names to the data frame. At a time we can change single or multiple column names.
We can use pandas DataFrame rename() function to rename columns and indexes. It supports the following parameters. mapper: dictionary or a function to apply on the columns and indexes.
To change multiple column names by name and by index use rename() function of the dplyr package and to rename by just name use setnames() from data. table . From R base functionality, we have colnames() and names() functions that can be used to rename a data frame column by a single index or name.
We can just remove the substring with rename_at
library(stringr)
libraryr(dplyr)
df1 %>%
table %>%
data.frame(.) %>%
rename_at(1:3, ~ str_remove(., "col"))
Or if it needs to be from 'df2'
df1 %>%
table %>%
data.frame(.) %>%
rename_at(1:3, ~ setNames(as.character(df2$altname), df2$name)[.])
If all the column names in 'df1' are not in key/val columns of 'df2', an option is
df1 %>%
table %>%
data.frame(.) %>%
rename_at(1:3, ~ coalesce(setNames(as.character(df2$altname), df2$name)[.], .))
Or using base R
out <- df1 %>% table %>% data.frame(.)
names(out) <- sub("col", "", names(out))
if it needs to be based on a second dataset
name(out)[-4] <- df2$altname[match(names(out)[-4], df2$name)]
Or with substr
names(out) <- substring(names(out), 4)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With