gpt4 book ai didi

r - 根据条件将值从一行复制到另一行

转载 作者:行者123 更新时间:2023-12-01 23:25:10 25 4
gpt4 key购买 nike

我有这个数据集:

# Data
movmnt_id <- c("101", "601", "105", "321")
plant <- c("FF", "FF", "DO", "BO")
loc <- c("MM", "MM", "KB", "RD")
vendor <- c(123, NA,NA,NA)
customer <- c(456, NA,NA,NA)
check <- c(NA, NA, "defined", "defined")

df <- data.frame(movmnt_id, plant, loc, vendor,customer,check)

movmnt_id plant loc vendor customer check
1 101 FF MM 123 456 <NA>
2 601 FF MM NA NA <NA>
3 105 DO KB NA NA defined
4 321 BO RD NA NA defined

我需要得到这个输出(第二行 vendor & customer 是从第一行复制的):

  movmnt_id plant loc vendor customer   check
1 101 FF MM 123 456 <NA>
2 601 FF MM 123 456 <NA>
3 105 DO KB NA NA defined
4 321 BO RD NA NA defined

条件如下:

If in current row `movmnt_id `== 601 
-> take row *WHERE* `plant` & `loc` are the same as in the current row
*AND* `movmnt_id == 101`
*AND* is.na(check)
-> copy from found row `vendor` & `customer` to the current row

我可以考虑一些 for 循环,但对于我的数据集来说它太重了。
我想知道是否有更优雅且计算成本更低的解决方案。

我试图从这些案例中调整解决方案,但没有成功:

最佳答案

要实现您的条件,您可以尝试以下操作 -

library(dplyr)

df %>%
group_by(plant, loc) %>%
mutate(across(c(vendor, customer),
~ifelse(movmnt_id == '601' & is.na(.),
.[is.na(check) & movmnt_id == 101], .))) %>%
ungroup

# movmnt_id plant loc vendor customer check
# <chr> <chr> <chr> <dbl> <dbl> <chr>
#1 101 FF MM 123 456 NA
#2 601 FF MM 123 456 NA
#3 105 DO KB NA NA defined
#4 321 BO RD NA NA defined

关于r - 根据条件将值从一行复制到另一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67399813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com