Remove an entire column from a data.frame in R

rename column in r
remove columns in r by name
drop specific columns from dataframe in r
how to remove a column from table in r
delete multiple columns in r
how to remove columns in r
add column to dataframe r
remove columns in r dplyr

Does anyone know how to remove an entire column from a data.frame in R? For example if I am given this data.frame:

> head(data)
   chr       genome region
1 chr1 hg19_refGene    CDS
2 chr1 hg19_refGene   exon
3 chr1 hg19_refGene    CDS
4 chr1 hg19_refGene   exon
5 chr1 hg19_refGene    CDS
6 chr1 hg19_refGene   exon

and I want to remove the 2nd column.

You can set it to NULL.

> Data$genome <- NULL
> head(Data)
   chr region
1 chr1    CDS
2 chr1   exon
3 chr1    CDS
4 chr1   exon
5 chr1    CDS
6 chr1   exon

As pointed out in the comments, here are some other possibilities:

Data[2] <- NULL    # Wojciech Sobala
Data[[2]] <- NULL  # same as above
Data <- Data[,-2]  # Ian Fellows
Data <- Data[-2]   # same as above

You can remove multiple columns via:

Data[1:2] <- list(NULL)  # Marek
Data[1:2] <- NULL        # does not work!

Be careful with matrix-subsetting though, as you can end up with a vector:

Data <- Data[,-(2:3)]             # vector
Data <- Data[,-(2:3),drop=FALSE]  # still a data.frame

R : Keep / Drop Columns from Data Frame, R : Delete column by name. Method I : The most easiest way to drop columns is by using subset() function. In the code below, we are telling R to drop variables x​  3. Delete A Column Of A Data Frame By Subsetting. Another approach for us to remove or delete a column of a data frame in R is to leveraging the subsetting of a data frame, for example, to delete the column “eye” which is the 4th column of the above data frame:

To remove one or more columns by name, when the column names are known (as opposed to being determined at run-time), I like the subset() syntax. E.g. for the data-frame

df <- data.frame(a=1:3, d=2:4, c=3:5, b=4:6)

to remove just the a column you could do

Data <- subset( Data, select = -a )

and to remove the b and d columns you could do

Data <- subset( Data, select = -c(d, b ) )

You can remove all columns between d and b with:

Data <- subset( Data, select = -c( d : b )

As I said above, this syntax works only when the column names are known. It won't work when say the column names are determined programmatically (i.e. assigned to a variable). I'll reproduce this Warning from the ?subset documentation:


This is a convenience function intended for use interactively. For programming it is better to use the standard subsetting functions like '[', and in particular the non-standard evaluation of argument 'subset' can have unanticipated consequences.

How to delete a column from a data frame in R? - tools, Hi, I am new to R and I wanted to delete column number 14 from my data frame of 21 variables? I want the column to be deleted, and not set to  # how to remove a column in r / delete column in R # this version will remove column in r by name dataframe$columetoremove <- NULL This approach will set the data frame’s internal pointer to that column to NULL, releasing the space and will remove the column from the R data frame. A simple but efficient way to drop columns.

(For completeness) If you want to remove columns by name, you can do this:

cols.dont.want <- "genome"
cols.dont.want <- c("genome", "region") # if you want to remove multiple columns

data <- data[, ! names(data) %in% cols.dont.want, drop = F]

Including drop = F ensures that the result will still be a data.frame even if only one column remains.

Examples of How To Add and Delete Columns From an R Dataframe, We're going to walk through how to add and delete columns to a data frame using R. This includes creating calculated fields. This article continues the examples  To remove an entire column from a data frame, you can use any one of the following ways. Data[2] <- NULL . Data[[2]] <- NULL . Data <- Data[,-2] Data <- Data[-2] Output: chr region. 1 chr1 CDS. 2 chr1 exon. 3 chr1 CDS. 4 chr1 exon. 5 chr1 CDS. 6 chr1 exon

The posted answers are very good when working with data.frames. However, these tasks can be pretty inefficient from a memory perspective. With large data, removing a column can take an unusually long amount of time and/or fail due to out of memory errors. Package data.table helps address this problem with the := operator:

> dt <- data.table(a = 1, b = 1, c = 1)
> dt[,a:=NULL]
     b c
[1,] 1 1

I should put together a bigger example to show the differences. I'll update this answer at some point with that.

How to Remove a Column in R using dplyr (by name and index), If we, on the other hand and want to install the whole Tidyverse Notice, how we this time removed the first column from the dataframe in R. R : Drop columns by column index numbers It's easier to remove variables by their position number. All you just need to do is to mention the column index number. In the following code, we are telling R to drop variables that are positioned at first column, third and fourth columns. The minus sign is to drop variables. df <- mydata[ -c(1,3:4) ]

There are several options for removing one or more columns with dplyr::select() and some helper functions. The helper functions can be useful because some do not require naming all the specific columns to be dropped. Note that to drop columns using select() you need to use a leading - to negate the column names.

Using the dplyr::starwars sample data for some variety in column names:


starwars %>% 
  select(-height) %>%                  # a specific column name
  select(-one_of('mass', 'films')) %>% # any columns named in one_of()
  select(-(name:hair_color)) %>%       # the range of columns from 'name' to 'hair_color'
  select(-contains('color')) %>%       # any column name that contains 'color'
  select(-starts_with('bi')) %>%       # any column name that starts with 'bi'
  select(-ends_with('er')) %>%         # any column name that ends with 'er'
  select(-matches('^v.+s$')) %>%       # any column name matching the regex pattern
  select_if(~!is.list(.)) %>%          # not by column name but by data type

# A tibble: 2 x 2
homeworld species
  <chr>     <chr>  
1 Tatooine  Human  
2 Tatooine  Droid 

You can also drop by column number:

starwars %>% 
  select(-2, -(4:10)) # column 2 and columns 4 through 10

How to delete variables (columns) in RStudio, Another way of doing it using base R: [code]test <- data.frame(x = c(1,2,3,4), y = c​("a","b","c","d"), z = c("A","B","C","D")) x y z 1 1 a A 2 2 b B 3 3 c C 4 4 d D  In R, one can perform various types of operation on a data frame like accessing rows and columns, selecting the subset of the data frame, editing data frames, delete rows and columns in a data frame, etc. Please refer to DataFrame Operations in R to know about all types of operations that can be performed on a data frame.

Adding and removing columns from a data frame, There are many different ways of adding and removing columns from a data frame. data <- read.table(header=TRUE, text=' id weight 1 20 2 27 3 24 ') # Ways to  If we already have a string that have the objects pasted together, we can use strsplit to split the string and get the values with mget. This will return a list output. Then wrap it with data.frame to convert it to 'data.frame` data.frame(mget(strsplit(str1, ', ')[[1]])) data str1 <- paste0

Drop column in R using Dplyr - drop variables, Dplyr package in R is provided with select() function which is used to select or drop the columns based on Drop 3rd, 4th and 5th columns of the dataframe:  delete.dirt <- function(DF, dart=c('NA')) { dirty_rows <- apply(DF, 1, function(r) !any(r %in% dart)) DF <- DF[dirty_rows, ] } mydata <- delete.dirt(mydata) Above function deletes all the rows from the data frame that has 'NA' in any column and returns the resultant data.

pandas.DataFrame.drop, Remove rows or columns by specifying label names and corresponding axis, or by specifying directly index or column names. When using a multi-index, labels on  If we needed to insert multiple rows into a r data frame, we have several options. First, we can write a loop to append rows to a data frame. This is good if we are doing something like web scraping, where we want to add rows to the data frame after we download each page. We can still use this basic mechanism within a loop, iterating our