I'm quite new to R and am currently stuck with my data.frame. I have a character column with different groups varying in numbers. For example the first seven rows being "A", the next five rows being "B" and so on. Now I have a vector with a length being equal to the total number of groups. My goal is to create a new column, where all "A" rows get the first vector value, all "B" rows the second value and so on.
I already tried:
values <- c("G", "H", "J", "K")
dat$col2 <- values[dat$col1]
from an earlier entry (Create new column based on 4 values in another column) and it worked. But after updating R it somehow doesn't work anymore. Though it creates the new column "col2", the values are now all NA and not corresponding the vector.
Can anyone help me out with that?
edit: example as reproducible code:
first_column <- c(rep("value_1", 6),rep("value_2",7))
df <- data.frame(first_column)
df$second_column <- c("A","B")[df$first_column]