在一个Tibble中,我的值是另一个Tibble中的变量名. 我需要快速列出第一个Tibble的每一行中包含的列名的第二个Tibble中特定行中的值.我该怎么做呢?
我的数据格式如下:
# 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
)
我需要创建一个Tibble,它包含第一个数字的值tibble_b
,假设是第一行,tibble_a
中的所有行.也就是说,基本上,要获得这个表格数据:
a | b | c |
---|---|---|
107 | 112 | 85 |
107 | 98 | 100 |
112 | 85 | 85 |
我的问题是,tibble_a
有数十万行,当然,两者都有更多的列.由于我必须对tibble_b
的行索引的多个样本执行测试和比较,因此使用如下内容非常耗时.
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]]]]
}
}
有没有人能建议一些有效的方法来完成这项任务?