I have a data frame and I want to add a column. For that purpose, I use dplyr::mutate. However, the values of the column I want to add depend of the value of other column. I use case_when() and the problem is solved, but if there are many cases the code is uncomfortable to write (I show an example below), so I want to know if there is another option (maybe a loop) to simplify this.
The following code works:
NewTable <- Table %>%
dplyr::mutate(ColumnB = case_when(
ColumnA=="2000" ~ 0,
ColumnA=="2001" ~ 4,
ColumnA=="2002" ~ 8,
ColumnA=="2003" ~ 12,
ColumnA=="2004" ~ 16,
ColumnA=="2005" ~ 20,
ColumnA=="2006" ~ 24,
ColumnA=="2007" ~ 28,
ColumnA=="2008" ~ 32,
ColumnA=="2009" ~ 36,
ColumnA=="2010" ~ 40,
ColumnA=="2011" ~ 44))
How can I improve it? Thank you very much.