I have a tibble/dataframe df
with six columns (a,b,c,d,e,f
)
I randomly choose one of these six letters and assign it to an extra column:
df %>% mutate(ran=sample(c("a","b","c","d","e","f"),n(), replace=T))
So for each row I have a randomly picked column name. How can I get a value of the corresponding randomly picked column and put it to a new column?
CodePudding user response:
Here is a tidyverse solution relying on tidyr::gather
.
First I create a sample tibble.
library(tidyverse)
set.seed(123)
df <-
tibble(a = 1:5, b = a 10, c = b 10, d = c 10, e = d 10, f = e 10) %>%
mutate(
ran=sample(c("a","b","c","d","e","f"),n(), replace=T),
row = 1:n()
)
Then I pivot this dataframe and then use filter
to select the right value.
df %>%
gather(val, col, -ran, -row) %>%
filter(ran==val) %>%
select(row, col) %>%
left_join(df, ., by = "row")
# A tibble: 5 x 9
a b c d e f ran row col
<int> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <int> <dbl>
1 1 11 21 31 41 51 c 1 21
2 2 12 22 32 42 52 f 2 52
3 3 13 23 33 43 53 c 3 23
4 4 14 24 34 44 54 b 4 14
5 5 15 25 35 45 55 b 5 15