Good morning,
I have 2 dataframes : (25000,66) and a thershold table (10,2) containing 10 groups and the last id of each group.
In the big dataset I have one variable called id. It's just id = row_number()
id
1
2
3
4
5
...
25000
EDIT : Lots of answers, thank you for all your ideas. Reading I realized that I forgot an important step in my data description and I apologized.
I'm using synthetic sampling on the original bigdataset to genereate new points. So after sampling the id column looks like this :
id
1
2
2.1
3
3.8
4.74
5.12
6
...
25000
This is why I used the between clause with the last_id to reassign the id to their group.
Threshold table :
last_id group_name
50 grp1
1500 grp2
8900 grp3
...
25000 grp10
I would like to add new column to the big dataset in order to have the id and the group name, based only on the condition that the id falls in the group specified range by the threshold table.
For now I wrote this :
df <- df %>%
dplyr::mutate(group_name = case_when(id < last_id[1,1] ~ last_id[1,2],
between(id, last_id[1,1], last_id[2,1]) ~ last_id[2,2],
between(id, last_id[2,1], last_id[3,1]) ~ last_id[3,2],
between(id, last_id[3,1], last_id[4,1]) ~ last_id[4,2],
between(id, last_id[4,1], last_id[5,1]) ~ last_id[5,2],
between(id, last_id[5,1], last_id[6,1]) ~ last_id[6,2],
between(id, last_id[6,1], last_id[7,1]) ~ last_id[7,2],
between(id, last_id[7,1], last_id[8,1]) ~ last_id[8,2],
between(id, last_id[8,1], last_id[9,1]) ~ last_id[9,2],
id > last_id[9,1] ~ last_id[10,2]))
)
But it doesn't work, I get this error :
Error in FUN(left, right) : comparaison (5) ony possible for types list and atomic
Moreover this code looks terrible, there must be another way using apply or another dplyr function?
Thank you for reading.