Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In dplyr, how to delete and rename columns that don't exist, manipulate all names, and name a new variable using a string?

Tags:

dataframe

r

dplyr

How can I simplify or perform the following operations using dplyr:

  1. Run a function on all data.frame names, like mutate_each(funs()) for values, e.g.

    names(iris) <- make.names(names(iris))
    
  2. Delete columns that do NOT exist (i.e. delete nothing), e.g.

    iris %>% select(-matches("Width")) # ok
    iris %>% select(-matches("X"))     # returns empty data.frame, why?
    
  3. Add a new column by name (string), e.g.

    iris %>% mutate_("newcol" = 0) # ok
    
    x <- "newcol"
    iris %>% mutate_(x = 0) # adds a column with name "x" instead of "newcol"
    
  4. Rename a data.frame colname that does not exist

    names(iris)[names(iris)=="X"] <- "Y"
    
    iris %>% rename(sl=Sepal.Length) # ok
    iris %>% rename(Y=X)             # error, instead of no change
    
like image 994
ckluss Avatar asked Jan 09 '15 17:01

ckluss


People also ask

How do I remove columns from dplyr in R?

dplyr select() function is used to select the column and by using negation of this to remove columns. All verbs in dplyr package take data.

How do I rename a column 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 I rename multiple columns in dplyr?

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.

How do I rename a variable in dplyr?

To rename a column in R, you can use the rename() function from dplyr. For example, if you want to rename the column “A” to “B” again, you can run the following code: rename(dataframe, B = A) .


1 Answers

Answer for the question n.2:

You can use the function any_of if you want to give explicitly the full names of the columns.

iris %>% 
    select(-any_of(c("X", "Sepal.Width","Petal.Width")))

This will not remove the non-existing column X and will remove the other two listed.

Otherwise, you are good with the solution with matches or a combination of any_of and matches.

  iris %>% 
    select(-any_of("X")) %>% 
    select(-matches("Width"))

This will remove explicitly X and the matches. Multiple matches are also possible.

iris %>% 
    select(-any_of("X")) %>%
    select(-matches(c("Width", "Spec"))) # use c for multiple matches
like image 174
Domenico Spidy Tamburro Avatar answered Sep 21 '22 20:09

Domenico Spidy Tamburro