I have a data frame like this:
I would like this result:
I wrote this code:
test$statetwentyeighteen = test$state[test$year=="2018"]
but I get this wrong result:
Can you please help see how to revise the code?
update:
I am having a new issue with this matter. when the original table is updated to this:

this code no longer works
test %>% group_by(name) %>% mutate(state_twentyeighteen = state[year == 2018])
instead I get this error message:
Error: Column `state_twentyeighteen` must be length 3 (the group size) or one, not 2
Can you please see what revision should be done to the code?


