vendredi 5 mars 2021

How to add a column based on partial information from another column in R [duplicate]

I have an example data frame like the one below.

ID File
1 11_213
2 13_256
3 11_223
4 12_389
5 14_456
6 12_345

And I want to add another column based on the number before the underscore in the "file" column to get a data frame that looks something like this.

ID File Group
1 11_213 GRP 11
2 13_256 GRP 13
3 11_223 GRP 11
4 12_389 GRP 12
5 14_456 GRP 14
6 12_345 GRP 12

I have tried using substrings within a series of if else statements like this:

df <- df %>%
mutate("Group" = ifelse(substring(File, 1,2) == "11", "GRP 11",
 ifelse(substring(File, 1,2) == "12", "GRP 12",
ifelse(substring(File, 1,2) == "13", "GRP 13", "GRP 14")))

And this works, but I realize this is a really inefficient way of doing this. Also, there are only so many ifelse statements you can do before you get an overflow error.

Is there a better way to do this? I think it might involve the grep or grepl functions, but I'm new to R and am not familiar with that. Any help would be great, thanks!

Aucun commentaire:

Enregistrer un commentaire