I'm having the following problem with my R-code (which I have fixed by 12 nested if else statements, which is by far not desirable). As I cannot share the full code nor the data, I have given a similar problem. Suppose I have the following column in my dataset, the frequency
> test_df
ID Frequency
1 1 1
2 2 56
3 3 34
4 4 22
5 5 9
6 6 8
7 7 50
8 8 7
Now, I want to mutate a new column based on a tabel, which categorizes the Frequency, namely
htbl
freq_interval category
1 6 A
2 18 B
3 20 C
4 30 D
5 40 E
Now, I want to mutate a new column based on this table, in the following way: if the frequency is less than 6, give the new column the value "A". If the frequency is less than 18, but more than 6, give the new column the value "B". If the frequency is less than 20 but more than 18, give it the value "C" and so on. So, my desired new test_df will be:
ID Frequency mutated_column
1 1 1 A
2 2 56 <NA>
3 3 34 E
4 4 22 D
5 5 9 B
6 6 8 B
7 7 50 <NA>
8 8 7 B
How can I do this cleanly?
Thanks in advance
Aucun commentaire:
Enregistrer un commentaire