Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all column except for one in R [duplicate]

Tags:

dataframe

r

Possible Duplicate:
Drop Columns R Data frame

Let's say I have a dataframe with column c1, c2, c3.

I want to list just c1 and c2. How do I do that?

I've tried:

head(data[column!="c3"]) head(data)[,2] head(data[!"c3"]) 
like image 512
mythicalprogrammer Avatar asked Oct 12 '12 23:10

mythicalprogrammer


People also ask

How do I take all columns except one in R?

To pick out single or multiple columns use the select() function. The select() function expects a dataframe as it's first input ('argument', in R language), followed by the names of the columns you want to extract with a comma between each name.

How do I exclude a single column 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 select only certain columns in R?

To select a column in R you can use brackets e.g., YourDataFrame['Column'] will take the column named “Column”. Furthermore, we can also use dplyr and the select() function to get columns by name or index. For instance, select(YourDataFrame, c('A', 'B') will take the columns named “A” and “B” from the dataframe.


2 Answers

If you are looking for negative indexing by name (in addition to tcash21's numeric indexing), here's a few ways I know, some riskier than others:

mtcars[, -which(names(mtcars) == "carb")]  #only works on a single column mtcars[, names(mtcars) != "carb"]          #only works on a single column mtcars[, !names(mtcars) %in% c("carb", "mpg")]  mtcars[, -match(c("carb", "mpg"), names(mtcars))]  mtcars2 <- mtcars; mtcars2$hp <- NULL         #lost column (risky)   library(gdata)  remove.vars(mtcars2, names=c("mpg", "carb"), info=TRUE)  

Generally I use:

mtcars[, !names(mtcars) %in% c("carb", "mpg")]  

because I feel it's safe and efficient.

like image 95
Tyler Rinker Avatar answered Oct 04 '22 02:10

Tyler Rinker


You can index and use a negative sign to drop the 3rd column:

data[,-3] 

Or you can list only the first 2 columns:

data[,c("c1", "c2")] data[,1:2] 

Don't forget the comma and referencing data frames works like this: data[row,column]

like image 37
tcash21 Avatar answered Oct 04 '22 00:10

tcash21