Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change column name according to another dataframe in R?

Tags:

r

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?

like image 772
kittygirl Avatar asked Aug 16 '19 04:08

kittygirl


People also ask

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.

How do I rename a column in a Dataframe in R?

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.

How do I rename a specific column in a data frame?

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.

How do I change multiple column names in a Dataframe in R?

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.


1 Answers

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)[.])

Update

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)
like image 86
akrun Avatar answered Nov 15 '22 10:11

akrun