我已尽我所能进行搜索,但仍在为我的问题而苦苦挣扎。我正在尝试根据另一个 tibble 的值对 tibble 中的列进行子集化。
更具体地说,我有一些社会经济指标:
cname year ccodealp wdi_lfpr wdi_lfprf
Turkey 2010 TUR 51.611 29.592
Turkey 2011 TUR 52.781 30.995
Turkey 2012 TUR 52.809 31.676
Turkey 2013 TUR 53.874 33.125
Turkey 2014 TUR 54.597 33.446
Turkey 2015 TUR 55.594 34.858
我有一个单独的 tibble (Tibble 2),有两列,指标和 Tibble 1 中该指标的缺失百分比
tibble_2
col value
who_dwtot 100
who_dwrur 100
who_dwurb 100
我想要做的是将 tibble_1 子集化为在 tibble_2 中仅包含满足特定条件的列。即,仅保留缺失率低于 90% 的列(tibble_2 中的“值”列)。我在 tidyverse 中遇到了麻烦。这是我试过的代码:
tibble_1 %>% select(tibble_2, "value" < 90)
Error: Must subset columns with a valid subscript vector.
x Subscript has the wrong type `tbl_df< col : character value: double >`. i
It must be numeric or character. Run `rlang::last_error()` to see where the error occurred.
我知道这可能是一个微不足道的问题,但我不是 tidyverse 方面的专家,也不知道如何解决这个问题。
感谢您的帮助。
最佳答案
我们可以根据 'value' 列过滤
'tibble_2' 并拉
'col' 用于选择
列名在 tibble_1
library(dplyr)
tibble_1 %>%
select({tibble_2 %>%
filter(value < 90) %>%
pull(col)})
或者如果我们使用 base R
subset(tibble_1, select = subset(tibble_2, value < 90, select = col)$col)
关于r - 根据另一个 tibble 中的值子集 tibble 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67287409/