I have a very similar task to the original poster in this thread: Create new column in dataframe based on partial string matching other column
however have 10 different conditions under TEST. There was a suggestion in the original thread how to code for >3 conditions however I wasn’t able to understand how to apply it to my data.
I want to create a column called DISTANCE that extracts the distance from the test. So for any test that includes "0.10m" in the name, I want to be able to have "0-10m" in the distance column. If it is "0.20m" in the name, I want it to be "0-20m" in the DISTANCE column and so on.
PLAYER SEX TEST VALUE
Player 1 Female ICE_0.10m 2.100000
Player 1 Female ICE_0.20m 3.475000
Player 1 Female ICE_10.20m 1.375000
Player 1 Female ICE_20.30m 1.246000
Player 1 Female ICE_0.30m 4.721000
Player 1 Female ICE_Vel_0.10m 4.761905
Player 1 Female ICE_Vel_0.20m 5.755396
Player 1 Female ICE_Vel_10.20m 7.272727
Player 1 Female ICE_Vel_20.30m 8.025682
Player 1 Female ICE_Vel_0.30m 6.354586
Player 1 Female OFF_0.10m 1.983000
Player 1 Female OFF_0.20m 3.380000
Player 1 Female OFF_10.20m 1.397000
Player 1 Female OFF_20.30m 1.380000
Player 1 Female OFF_0.30m 4.760000
Player 1 Female OFF_Vel_0.10m 5.042864
Player 1 Female OFF_Vel_0.20m 5.917160
Player 1 Female OFF_Vel_10.20m 7.158196
Player 1 Female OFF_Vel_20.30m 7.246377
Player 1 Female OFF_Vel_0.30m 6.302521
I tried this but it didn't work:
SpeedLong$Distance <- ifelse(grepl("0.10m", SpeedLong$Tag, ignore.case = T), "0-10m",
ifelse(grepl("0.20m", SpeedLong$Tag, ignore.case = T), "0-20m",
ifelse(grepl("0.30m", SpeedLong$Tag, ignore.case = T), "0-30m",
ifelse(grepl("0.10m", SpeedLong$Tag, ignore.case = T), "0-10m", "20-30m"))
With that code I don't get an error message but it shows the code in the console finishing with a + sign I guess meaning the code is incomplete? I don't know if if else and grepl are the best ways to go about this so alternative suggestions are welcome!