How can i get the following
df | df$nsp class1 | 1 | class2 | 2 | class3 | 3 | class1 | 1
class3 3
How can i get the following
df | df$nsp class1 | 1 | class2 | 2 | class3 | 3 | class1 | 1
class3 3
If I understood, your labels are in string format: 'class1', 'class2', ... Like:
df = data.frame(nsp = c('class1','class2','class3','class4'))
And you want to use only the number to use RF. You can get the numbers using gsub to extract only the numbers and as.numeric to convert string to int. Here, I'm using tidyverse library but it is not relevant.
library(tidyverse)
df %>% mutate(class= as.numeric(gsub(".*?([0-9]+).*", "\\1", nsp)))
nsp class
1 class1 1
2 class2 2
3 class3 3
4 class4 4
With base R you can use:
df$class = as.numeric(gsub(".*?([0-9]+).*", "\\1", df$nsp))