Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Deleting rows based on a value in a column from a large data set in R [duplicate]

I am working in R on data set of 104500 observations. I want to delete rows based on a column name "state" that has values "TX" and "NY".

I am using the following code

customers <- customers[customers$State != "TX"]

I'm getting the following error

Error: Length of logical index vector must be 1 or 11 (the number of rows), not 104541

Can anyone please help me with this?

like image 902
deadpool Avatar asked Oct 04 '17 21:10

deadpool


People also ask

How do I drop a row based on a column value in R?

If we prefer to work with the Tidyverse package, we can use the filter() function to remove (or select) rows based on values in a column (conditionally, that is, and the same as using subset). Furthermore, we can also use the function slice() from dplyr to remove rows based on the index.

How do you conditionally delete a row in R?

To remove rows of data from a dataframe based on multiple conditional statements. We use square brackets [ ] with the dataframe and put multiple conditional statements along with AND or OR operator inside it. This slices the dataframe and removes all the rows that do not satisfy the given conditions.

How do I remove duplicate rows in a column in R?

Use the unique() function to remove duplicates from the selected columns of the R data frame.

How do I remove specific rows and columns in R?

To remove the row(s) and column(s) of a current matrix in R, we use the c() function.


1 Answers

I think you missed a comma at the end.

customers <- customers[customers$State != "TX", ]
                                              ^

So you select rows based on your filter, and all columns.

HTH

please provide a reproducible example the next time.

like image 138
sluedtke Avatar answered Sep 18 '22 15:09

sluedtke