如何解决如何同时在两个数据集中使用if-R
一个疑问,我需要比较两个数据集的ID号相同,如果相同,则需要将x列中的字段除以y列中的字段。
例如:
tableX <- as_tibble (tableX)
tableY <- as_tibble (tableY)
if (tableX $ id == tableY $ id) {
tableX $ example <- tableY $ px / tableY $ px
}
由于它们是两个独立的数据集,由于每个数据集([i])中的行数,我不知道如何进行此过滤。 如果有人知道任何功能或对我有帮助的任何东西,我将非常感激。
解决方法
联接可能是您想要的。然后,您可以过滤和计算所需的任何内容。
library(dplyr)
# if you only want those rows with matching IDs in both tables
# Inner_join matches up the id values
tableX %>%
innner_join(tableY,by = "id") %>%
mutate(example = ...) # Where ... is the calculation you want to do
# If you want to keep all the rows in X even if there is not a match in Y
tableX %>%
left_join(tableY,by = "id") %>%
mutate(example = ...) # Where ... is the calculation you want to do
# If you want to do the left_join and have different behavior id there is a match or not
tableX %>%
left_join(tableY,by = "id") %>%
mutate(example = case_when(is.na(table.Y.px) ~ ...,# Where ... is the calculation you want to do
TRUE ~ ...) # How you want the rest of the cases to be treated
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。