我想在我的数据中计算一些汇总值。数据帧中有趣的列被命名为exppw...._1003和pw...._1003,其中....代表例如0506、0607等。列exppw...._1003中的数据来自实验,而列pw...._1003中的数据来自我们的“标准过程”。
我想比较两个相应列的汇总值,例如pw0708_1003和exppw0708_1003。
rt2020_2 %>%
select(id_intern, persGew, matches("exppw...._1003"), matches("pw...._1003")) %>%
summarise(across(starts_with("pw"),
list(
gewSum = ~ sum(persGew)/1e5,
uWHK = ~ n_distinct(id_intern[.x > 0]),
uWHKExp = ~ n_distinct(id_intern[(paste0("exp", cur_column()))>0])
)
))我希望你能意识到我打算在这里做什么。我采用以"pw“开头的列,并对值>0的不同情况进行计数。我现在的问题是,我在术语id_intern[(paste0("exp", cur_column()))>0]中的条件并不像我希望的那样工作,也就是说,它总是产生true。我尝试取消对paste0("exppw", cur_column())的引用,使其显示为:
rt2020_2 %>%
select(id_intern, persGew, ges, matches("exppw...._1003"), matches("pw...._1003")) %>%
summarise(across(starts_with("pw"),
list(
gewSum = ~ sum(persGew)/1e5,
uWHK = ~ n_distinct(id_intern[.x > 0]),
uWHKExp = ~ n_distinct(id_intern[!!(paste0("exp", cur_column()))>0])
)
))但这会导致一个错误:
Error: `cur_column()` must only be used inside `across()`.你能帮帮我吗?还是有更好的方法来解决我的问题?谢谢你的帮助。
发布于 2020-08-20 14:19:33
有几个问题。现在还不清楚你想在gewSum中得到什么。是sum(persGew)还是以pw开头所有列的总和?uWHK等也是如此。它是pw列还是id_intern列中的内容的不同计数?
此外,在摘要中使用[ ]也不是一个好主意。也许更好的做法是使用mutate并创建几个列来检查您的条件。
https://stackoverflow.com/questions/63416128
复制相似问题