# The order of my `class` variable is:
complete.cases <- c("Class_0_1","Class_1_3","Class_3_9", "Class_9_25","Class_25_50")
# In my ds:
nest <- c(2,2,2,2,2,3,3,3,3)
TA <- c(2.3,5.9,8.7,11.8,14.9,11.9,8.8,5.7,2.4)
class <- c("Class_0_1","Class_1_3","Class_3_9","Class_3_9","Class_9_25","Class_1_3","Class_1_3","Class_1_3","Class_3_9")
my.ds <- data.frame(nest,TA,class)
my.ds
nest TA class
1 2 2.3 Class_0_1
2 2 5.9 Class_1_3
3 2 8.7 Class_3_9
4 2 11.8 Class_3_9
5 2 14.9 Class_9_25
6 3 11.9 Class_1_3
7 3 8.8 Class_1_3
8 3 5.7 Class_1_3
9 3 2.4 Class_3_9
# About ds information:
nest <- c(2,3)
class_max <- c("Class_3_9","Class_1_3")
info.ds <- data.frame(nest,class_max)
Now I'd like to remove the rows based in the rule, my.ds$class
up than info.ds$class_max
by nest remove it, if not is OK.
My final dataframe: must to be:
# new.ds
# nest TA class
# 1 2 2.3 Class_0_1
# 2 2 5.9 Class_1_3
# 3 2 8.7 Class_3_9
# 4 2 11.8 Class_3_9
# 5 3 11.9 Class_1_3
# 6 3 8.8 Class_1_3
# 7 3 5.7 Class_1_3
Please, any help with it?
CodePudding user response:
You could convert class
to an ordered factor object and define a custom order for it with ordered(x)
or factor(x, ordered = TRUE)
.
library(dplyr)
my.ds %>%
mutate(class = ordered(class, levels = complete.cases)) %>%
left_join(info.ds, by = 'nest') %>%
filter(class <= class_max) %>%
select(-class_max)
# nest TA class
# 1 2 2.3 Class_0_1
# 2 2 5.9 Class_1_3
# 3 2 8.7 Class_3_9
# 4 2 11.8 Class_3_9
# 5 3 11.9 Class_1_3
# 6 3 8.8 Class_1_3
# 7 3 5.7 Class_1_3
CodePudding user response:
Here is one option - join the two datasets and then do a group by slice
library(dplyr)
my.ds %>%
arrange(nest, factor(class, levels = complete.cases)) %>%
left_join(info.ds, by = 'nest') %>%
group_by(nest) %>%
slice(seq_len(last(which(class == class_max)))) %>%
ungroup %>%
select(-class_max)
-output
# A tibble: 7 × 3
nest TA class
<dbl> <dbl> <chr>
1 2 2.3 Class_0_1
2 2 5.9 Class_1_3
3 2 8.7 Class_3_9
4 2 11.8 Class_3_9
5 3 11.9 Class_1_3
6 3 8.8 Class_1_3
7 3 5.7 Class_1_3