Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a column from a data frame by name [duplicate]

Tags:

dataframe

r

Possible Duplicate:
remove an entire column from a data.frame in R

Is there a better way to remove a column by name from a data frame than the following?

Orange[colnames(Orange) != "Age"]

I've tried the following and I get errors:

> Orange[-"Age"]
Error in -"age" : invalid argument to unary operator
> Orange[,-"Age"]
Error in -"age" : invalid argument to unary operator
> Orange[[,-"Age"]]
Error in -"age" : invalid argument to unary operator
like image 848
Blue Magister Avatar asked Jul 19 '12 16:07

Blue Magister


People also ask

How do I find duplicates in a column in a data frame?

To find duplicate columns we need to iterate through all columns of a DataFrame and for each and every column it will search if any other column exists in DataFrame with the same contents already. If yes then that column name will be stored in the duplicate column set.


2 Answers

You can set the column to NULL

> dat <- data.frame(a = 1, b = 1, c = 1)
> dat
  a b c
1 1 1 1
> dat$a <- NULL
> dat
  b c
1 1 1
> dat["b"] <- NULL
> dat
  c
1 1

Someone will come along and point out that data.frame will makes lots of copies of the data to do this simple task. When data gets big (millions of rows), this will take a lot of time and may not work due to memory constraints. If that's going to be an issue, use data.table and the := operator:

library(data.table)
> dt <- data.table(a = 1, b = 1, c = 1)
> dt[,a:=NULL]
     b c
[1,] 1 1
like image 158
Chase Avatar answered Nov 14 '22 23:11

Chase


Try:

Orange[-match("age",names(Orange))]
   Tree circumference
1     1            30
2     1            58
3     1            87
4     1           115
...
like image 33
James Avatar answered Nov 14 '22 23:11

James