I'm an R novice (first time posting) trying to create a new column for an existing data.frame based on the last character in an existing column. The data.frame elements look OK but I always receive an error message like:
Error in Plot.ID(endsWith, "D") : could not find function "Plot.ID"
I've tried using mutate in dplyr and transform in RStudio but always receive a similar error. Below is the code I tried using with dplyr.
mature_trees_rev <- mature_trees %>%
mutate(plot_type = case_when(Plot.ID(ends_with("D"))~'Decidious',
Plot.ID(ends_with("E"))~'Evergreen',TRUE~"NA"))
mature_trees <- transform(mature_trees, sample_type= ifelse(Plot.ID (endsWith,"D"),"Deciduous", "Evergreen"))
str(mature_trees)
'data.frame': 229 obs. of 8 variables:
$ Plot.ID : chr "0181E" "0181E" "0181E" "0181E" ...
$ Tag.number: int 1 2 3 4 5 6 7 8 9 10 ...
$ Species : chr "PIAB" "ACRU" "PIAB" "PIAB" ...
$ Year : int 2017 2017 2017 2017 2017 2017 2017 2017 2017 2017 ...
$ D_2017 : num 15.2 25 8.5 11.8 24.5 15 15.7 10 9.3 NA ...
$ D_2018 : num 15.8 25.9 8.5 11.9 24.7 15.1 15.8 9.9 9.7 12.2 ...
$ D_2019 : num 15.6 25.9 9.5 12 24.8 15 16.2 10 9.5 15.3 ...
I've tried this with the Plot.ID element as both a factor and character string (with a lot of syntax variations), but always get a similar result. I was able to create a new column using mutate, but not when using any of the existing data.frame conditions. I guess I'm really trying to figure out why the data.frame element (Plot.ID) is being treated as a function? Thanks.