Reputation: 2409
Given the following vectors to build a dataframe :
set.seed(1)
x <- sample( LETTERS[1:4], 100, replace=TRUE)
y <- runif(100,0,100)
df <- data.frame(x,y)
I would like to have if possible, a clean code with a loop or apply
or any other method to subset the data.frame
by different conditionals for every level of factor x
. For example:
level A y >30 | y <20
level B y >21 | y <12
level C y >42 | y <21
level D y >58 | y <13
Upvotes: 1
Views: 53
Reputation: 31454
using library(data.table)
we can do
lower = c(20, 12, 21, 13)
upper = c(30, 21, 42, 58)
setDT(df)[!between(y, lower[x], upper[x]), .SD, keyby=x]
# x y
# 1: A 63.349326
# 2: A 59.876097
# 3: A 97.617069
# 4: A 73.179251
# 5: A 49.559358
# 6: A 17.344233
# 7: A 51.116978
# ...
Upvotes: 0
Reputation: 26373
A split apply combine approach where we use Map
to iterate over the subsets and the conditions in parallel.
do.call(rbind,
Map(function(data, left, right) {
subset(x = data, subset = y > left | y < right)
},
data = split(df, df$x),
left = c(30, 21, 42, 58),
right = c(20, 12, 21, 13)
))
# x y
#A.5 A 63.349326
#A.10 A 59.876097
#A.11 A 97.617069
#A.12 A 73.179251
#A.22 A 49.559358
#A.24 A 17.344233
# ...
We split your data by x
, subset each according to your conditions and combine the list to a single dataframe.
Upvotes: 1
Reputation: 5281
What about something like this
df[df$x == 'A' & (df$y > 30 | df$y < 20),]
# x y
# 2 A 71.117606
# 3 A 44.438057
# 6 A 63.244699
# 7 A 54.185802
# 11 A 39.577617
# 13 A 8.681545
# 29 A 94.437431
# ...
# or depending on what you mean by '&'
df[df$x == 'A' & df$y > 30,]
# x y
# 2 A 71.11761
# 3 A 44.43806
# 6 A 63.24470
# 7 A 54.18580
# 11 A 39.57762
# 29 A 94.43743
# 31 A 54.17604
# ...
# and then accordingly for the other cases
Upvotes: 0