Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to drop columns by passing variable name with dplyr?

Tags:

r

dplyr

I have a df as follows:

a <- data_frame(keep=c("hello", "world"),drop = c("nice", "work"))
a
Source: local data frame [2 x 2]
   keep  drop
  (chr) (chr)
1 hello  nice
2 world  work

I can use a %>% select(-drop) to drop the column without problem. however, if I want to pass a variable to present drop column, then it returns error.

name <- "drop"
a  %>% select(-(name))

Error in -(name) : invalid argument to unary operator

like image 864
HappyCoding Avatar asked Jan 24 '17 03:01

HappyCoding


People also ask

How do I drop multiple columns in R with dplyr?

Use dplyr to Drop Multiple Columns Using a Function in R As usual, to drop columns, we use the ! operator. In the example, we use a simple custom function to select all columns with more than 10. The code drops these and returns the remaining columns.

How do I drop a variable by name in R?

Method 1: Using subset() This is one of the easiest approaches to drop columns is by using the subset() function with the '-' sign which indicates dropping variables. This function in R Language is used to create subsets of a Data frame and can also be used to drop columns from a data frame.

How do I remove certain columns in R?

The most easiest way to drop columns is by using subset() function. In the code below, we are telling R to drop variables x and z. The '-' sign indicates dropping variables. Make sure the variable names would NOT be specified in quotes when using subset() function.

How do I drop a column in R using dplyr?

In order to drop the column which ends with certain label we will be using select() function along with ends_with() function by passing the column label inside the ends_with() function as shown below. Dropping the column name which ends with “cyl” is accomplished using ends_with() function and select() function.


5 Answers

You can use one_of to find the column positions and then use - to drop it, select(-one_of(name)), if you check ?select, the usage is documented in the Drop variable section in the Examples:

name <- "drop"
a %>% select(-one_of(name))

# A tibble: 2 × 1
#   keep
#  <chr>
#1 hello
#2 world

Or with select_, you need to paste - with the column names to drop them and pass the pasted column names to the .dots parameter if there are more than one column to be dropped:

name <- "drop"
a %>% select_(.dots = paste("-", name))

# A tibble: 2 × 1
#   keep
#  <chr>
#1 hello
#2 world
like image 51
Psidom Avatar answered Oct 17 '22 04:10

Psidom


You can simple use

a <- data_frame(keep=c("hello", "world"),drop = c("nice", "work"))
select(a, -starts_with('drop'))
#   Source: local data frame [2 x 1]
#
#   keep
#   (chr)
# 1 hello
# 2 world

you have to search for some previously written solutions too. Please read the document here Select/rename variables by name.DPLYR

I hope that does the job for you :) @Psidom thanx for updating your answer.. but I will request upvoters for vote for me too as I recently became an active user and still am to get basic privileges on stackoverflow.

like image 33
Mandar Avatar answered Oct 17 '22 03:10

Mandar


We can use select with setdiff

a %>%
    select_(setdiff(names(.), name))
# A tibble: 2 × 1
#   keep
#   <chr>
#1 hello
#2 world
like image 43
akrun Avatar answered Oct 17 '22 04:10

akrun


A few more possibilities:

name <- "drop"
a %>% `[<-`(name, value=NULL)
a %>% magrittr::inset(name,value=NULL)
a %>% purrr::modify_at(name,~NULL)
like image 4
Moody_Mudskipper Avatar answered Oct 17 '22 02:10

Moody_Mudskipper


I could only get these solutions to work by first ungrouping the data using ungroup:

df <- df  %>%  ungroup  %>%  select(-hello)

Notice no quotation marks on the column name you want to drop (hello). Also, to remove multiple columns, just place a , after hello and add the second column.

like image 1
Marty999 Avatar answered Oct 17 '22 03:10

Marty999