Deleting Rows in R Based on Values Over Multiple Columns

Deleting rows in R based on values over multiple columns

You can use dplyr::filter_all() to accomplish this:

library(dplyr)

df <- data.frame(column.1 = c('a', 'b', 'NONE', 'b', 'b'),
column.2 = c('a', 'b', '', 'b', 'b'),
column.3 = rep('', 5),
column.4 = rep('', 5),
column.5 = rep('', 5))

df %>%
filter_all(any_vars(. != 'NONE' & . != ''))

Delete rows with multiple conditions in R

Try

a <- df[!( (df$Station == 7 & df$Depth == 1 ) | (df$Station == 7  & df$Depth == 2 )),] 
a

or more compact one

a <- df[!( df$Station == 7 & (df$Depth == 1  |  df$Depth == 2 )),] 
a

R, remove rows based on the values from multiple column

Use apply to look for duplicates in each row. (Note that this internally converts your data to a matrix for the comparison. If you are doing a lot of row-wise operations I would recommend either keeping it as a matrix or converting it to a long format as in Jack Brookes's answer.)

# sample data
set.seed(47)
dd = data.frame(matrix(sample(1:5000, size = 100^2, replace = TRUE), nrow = 100))

# remove rows with duplicate entries
result = dd[apply(dd, MARGIN = 1, FUN = function(x) !any(duplicated(x))), ]

Removing rows in a data frame based on multiple criteria in R with loop function

I assume you're using filter from the dplyr package. To be sure, use dplyr::filter() instead of just filter(). Something I notice is that you have a period after some of your variable names. These don't appear in the screenshot of your data, so maybe that is the problem. Also, when using filter, you don't need feb_raw$ with each variable.

So, potentially corrected code might look like the following:

dplyr::filter(feb_raw, Company=="STL" & Product=="N24" & MRP<=1360 & MRP>=1150)

How to remove rows based multiple conditions

You can remove rows where 'Death' occurs on row number 1 in each group.

library(dplyr)

df %>%
group_by(id) %>%
filter(!(row_number() == 1 & ConditionII == 'Death'))

# id ConditionI ConditionII
# <chr> <chr> <chr>
#1 B 2018-01-01 Alive
#2 B 2018-01-15 Alive
#3 B 2018-01-20 Death
#4 C 2018-02-01 Alive
#5 C 2018-02-1 Alive
#6 E 2018-04-01 Alive
#7 E 2018-04-10 Death

Same logic using data.table :

library(data.table)
setDT(df)[, .SD[!(seq_len(.N) == 1 & ConditionII == 'Death')], id]

Trying to remove rows based on values in two columns

Another way to do this:

# get the names that has 1 rem
# then identify names not in that subset and
# use it to subset the df
df[!(df$name %in% df$name[df$rem == 1]), ]

Removing rows where multiple columns equal an exact number R

If you want an efficient base R solution I would simply use rowSums, e.g.

cols <- paste0("x", 1:2) 
df[rowSums(df[cols] == 9) == length(cols), ]
# id x1 x2
# 1 1 9 9
# 2 2 9 9

If you want a data.table solution, I would use a binary join, e.g.

library(data.table)
setDT(df)[as.list(rep(9, length(cols))), on = cols]
# id x1 x2
# 1: 1 9 9
# 2: 2 9 9

Data

df <- data.frame(id = 1:3, x1 = c(9, 9, 4), x2 = c(9, 9, 4))


Related Topics



Leave a reply



Submit