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?
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.
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.
Use the unique() function to remove duplicates from the selected columns of the R data frame.
To remove the row(s) and column(s) of a current matrix in R, we use the c() function.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With