我想根据 id 和 id1 确定在特定日期内测量的长度。测量长度记录在 d1...d5 变量中, d_day 定义我要查找的测量长度。例如,在第一种情况下,我想要的是等于 30 的 d3 测量长度。我尝试了 which 函数,但肯定有更快的解决方案。
id
id1
d1...d5
d_day
30
d3
which
期望输出
数据结构:
Sample data: structure(list(id = c(101, 101), id_1 = c(1, 2), d1 = c(15, 0 ), d2 = c(30, 15), d3 = c(25, 30), d4 = c(15, 30), d5 = c(15, 60), d_day = c("d3", "d1")), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -2L), spec = structure(list( cols = list(id = structure(list(), class = c("collector_double", "collector")), id_1 = structure(list(), class = c("collector_double", "collector")), d1 = structure(list(), class = c("collector_double", "collector")), d2 = structure(list(), class = c("collector_double", "collector")), d3 = structure(list(), class = c("collector_double", "collector")), d4 = structure(list(), class = c("collector_double", "collector")), d5 = structure(list(), class = c("collector_double", "collector")), d_day = structure(list(), class = c("collector_character", "collector"))), default = structure(list(), class = c("collector_guess", "collector")), skip = 1L), class = "col_spec"))
618夏日盛惠
2核2G云服务器首年95元,GPU云服务器低至9.93元/天,还有更多云产品低至0.1折…
这样做是可行的:
library(dplyr) library(tidyr) df %>% pivot_longer(-c(id, id_1, d_day)) %>% filter(d_day == name) %>%