In R, I have a character vector v, whose values are all elements of colnames(rain) for a dataframe rain. I want to use v to create a new vector chosen whose values satisfy chosen[i] == rain[i, v[i]] without resorting to a loop and preferably using a tidyverse approach.
For example, if I have:
library(tidyverse)
rain <- tibble(ceres = c(0, 1, 0, 1.5, 3),
mond = c(0, 0, 0.5, 0, 0),
els = c(1, 2, 1, 0, 1))
v <- c("els", "ceres", "els", "mond", "ceres")
I would want to have returned in chosen:
> chosen
# els ceres els mond ceres
# 1 1 1 0 3