I want to add a new column to a data.frame
based on the last entry in a string so that I can do regression analysis. In particular, I am trying to extract information from the Fertiliser
variable which contains information on the amount of three different types of fertiliser, separated by hyphens. For instance: 0-0-0
or 30-10-2700
are viable. The last portion of the string is what I need to capture e.g. x-y-z
I need z
.
I tried but was not able to adapt the answer here How to create new column in dataframe based on partial string matching other column in R to this case.
More complete example of the data:
Fertiliser millet_biomass millet_yield
1: 0-0-0 2659.608 710.6942
2: 0-0-100 2701.044 718.1154
3: 0-0-2700 3415.879 804.0360
4: 0-0-300 2781.639 730.5943
5: 0-0-900 2997.173 760.0136
6: 12-4-0 3703.255 772.1719
7: 12-4-100 3720.247 773.1759
8: 12-4-2700 3950.189 788.6133
9: 12-4-300 3751.400 775.1368
10: 12-4-900 3826.693 780.2623
11: 30-10-0 4180.323 798.2134
12: 30-10-100 4184.229 798.4918
13: 30-10-2700 4217.044 800.9312
14: 30-10-300 4187.014 798.6570
15: 30-10-900 4194.873 799.2085
16: 6-2-0 3296.274 765.8496
17: 6-2-100 3326.844 767.6693
18: 6-2-2700 3772.058 785.4535
19: 6-2-300 3381.152 760.7330
20: 6-2-900 3517.515 768.3018
21: 90-30-0 4542.924 831.2832
22: 90-30-100 4543.036 831.3983
23: 90-30-2700 4545.037 831.3227
24: 90-30-300 4543.240 831.3921
25: 90-30-900 4543.733 831.3727
Thus, there are five patterns -0$
, -100$
, -300$
, -900$
, 2700$
, which need to be replaced by 0
, 100
, 300
, 900
, 2700