In one tibble I have values that are variable names in another tibble. I need to list the values in a specific row in the second tibble of the column names contained in each row of the first tibble in a fast way. How can I do it?
My data has the form:
# Observations in is tibble says which variables of the tibble_b occurred at each case:
tibble_a <- tribble(
~a, ~b, ~c,
4, 2, 5,
4, 3, 1,
2, 5, 5
)
# This tibble contains values for the variables at different times:
tibble_b <- tribble(
~`1`, ~`2`, ~`3`, ~`4`, ~`5`,
100, 112, 98, 107, 85,
71, 76, 97, 80, 15,
2, 61, 54, 37, 42
)
I need to create a tibble that contain the values in tibble_b for the numbers in the first one, for, let's say, the first row, for all the rows in tibble_a. That is, basically, to obtain this tabular data:
| a | b | c |
|---|---|---|
| 107 | 112 | 85 |
| 107 | 98 | 100 |
| 112 | 85 | 85 |
My problem is, tibble_a has hundreds of thousands of rows, and, of course, both has more columns. Since I have to perform tests and comparisons on multiple samples of row indexes of tibble_b, using something like below is very time consuming.
for(current_row in 1:nrow(tibble_a)) {
for(current_col in 1:3) {
tibble_of_results[[current_row, current_col]] <- tibble_b[[1, tibble_a[[current_row, current_col]]]]
}
}
Can someone suggest any efficient way to accomplish this task?
You can use the values of the column to column index
tibble_b. For example, fortibble_a[[1]]:We then simply convert that into a vector using
unlist. Then we can apply this approach across all columns usingacross(everything(), ...).