Linked Questions

8 votes
0 answers
28k views

Select all columns except specific (by name) in R? [duplicate]

I have the following dataframe: a b c d e 1 rr dfv 5 0.1 9 t rr1 222 0.44 7 sdf we 1 0.111 I want to select all columns except say d and e. I know I can use subset for this: ...
Dr. Richard Tennen's user avatar
2 votes
0 answers
9k views

How to remove column by name from R matrix? [duplicate]

I know how to remove columns by name from R dataframe, but I can not figure out how to do it in matrix. I want to do something like this: > m <- matrix(1:6, nrow=2, ncol = 3) > colnames(m) &...
Moysey Abramowitz's user avatar
1 vote
1 answer
3k views

Exclude columns in R calling column names [duplicate]

Suppose I have a matrix called mymat. I need to exclude columns key and AMLM12014N-R and make another matrix called newmat. I can simply do this like this: newmat <- mymat[,-c(1,4)], but is there a ...
Achal Neupane's user avatar
1 vote
1 answer
790 views

R referring to dataframe columns by label to delete them [duplicate]

An easy one I suppose though my searches have been pretty fruitless -- given z=data.frame(X.39=rnorm(20),X.40=rnorm(20),X.51=rnorm(20)) the subsetting operation z[,c('X.39','X.51')] works. but z[,...
Tahnoon Pasha's user avatar
1 vote
2 answers
108 views

Why doesn’t mtcars[, -c("mpg"), drop = F] work? [duplicate]

This works: mtcars[, c("mpg"), drop = F] mpg Mazda RX4 21.0 Mazda RX4 Wag 21.0 Datsun 710 22.8 Hornet 4 Drive 21.4 Hornet Sportabout 18....
bird's user avatar
  • 3,364
0 votes
1 answer
96 views

How to drop multiple columns in R using select?I just don't know where to put the “-” [duplicate]

Create the dataframe taxis_smaller that excludes the columns VendorID, rate, and store_and_fwd_flag. taxis_smaller <- taxis %>% select(taxis,-c("VendorID","rate","store_and_fwd_flag")) select(...
Sofia's user avatar
  • 1
0 votes
0 answers
66 views

Very basic subsetting issue [duplicate]

I have a dataframe of about 4000 x 50000, and I have a variable "x" which contains the name of the variables I would like to drop from the dataframe. Here's an example of what my data looks like: df &...
Error404's user avatar
  • 7,161
0 votes
0 answers
66 views

Why subset doesn't work with string column names when removing columns? [duplicate]

Consider the following dataframe: df <- data.frame(x=1:5, y=6:10, z=11:15, a=16:20) I can select a column using just the name: df2 <- subset (df, select = x) Or I can select it passing the ...
robertspierre's user avatar
0 votes
0 answers
28 views

Errors when using subset and select [duplicate]

I am using subset and select to extract columns from a dataframe. My code is simple, however errors occur. names(df7) df7 <- subset(df7, select = -c("Parameter.Code", "Time.Local", "Date.GMT", "...
Yabin Da's user avatar
  • 595
0 votes
0 answers
35 views

Remove columns with -c() [duplicate]

I know that one should be able to delete columns with -c() and the column names. But weirdly it doesn't work in my R. What am I doing wrong? library(data.table) df <- data.frame(x1=c(1,2,3), x2=c(4,...
Michael Bigler's user avatar
0 votes
0 answers
19 views

how can I add a column name as an argument to a function? [duplicate]

Im creating a function to run cor.test for all the columns on a data frame, except the column that is added in the argument. I want the argument (ref) to be the column name, buy when I actually use ...
Paula Carmona's user avatar
1052 votes
25 answers
2.0m views

Drop data frame columns by name

I have a number of columns that I would like to remove from a data frame. I know that we can delete them individually using something like: df$x <- NULL But I was hoping to do this with fewer ...
Btibert3's user avatar
  • 40.4k
449 votes
2 answers
111k views

Why is `[` better than `subset`?

When I need to filter a data.frame, i.e., extract rows that meet certain conditions, I prefer to use the subset function: subset(airquality, Month == 8 & Temp > 90) Rather than the [ function: ...
flodel's user avatar
  • 89.3k
41 votes
4 answers
190k views

How to delete specific rows and columns from a matrix in a smarter way?

Let's say t1 is : t1 <- array(1:20, dim=c(10,10)) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 1 11 1 11 1 11 1 11 [2,] 2 12 2 12 2 ...
psiu's user avatar
  • 615
8 votes
5 answers
51k views

How to select range of columns in a dataframe based on their name and not their indexes?

In a pandas dataframe created like this: import pandas as pd import numpy as np df = pd.DataFrame(np.random.randint(10, size=(6, 6)), columns=['c' + str(i) for i in range(6)], ...
Cleb's user avatar
  • 26.2k

15 30 50 per page