gpt4 book ai didi

r - 如何在转到R中的下一行之前为每一行创建函数返回值?

转载 作者:行者123 更新时间:2023-12-04 12:35:09 25 4
gpt4 key购买 nike

我有以下数据框:

lineups <- tibble::tribble(
~lineupBefore, ~playerOut, ~playerIn,
"Justise Winslow, Bam Adebayo, Meyers Leonard, Tyler Herro, Kendrick Nunn", "Kendrick Nunn", "Goran Dragic",
NA, "Justise Winslow", "Derrick Jones Jr.",
NA, "Meyers Leonard", "Kelly Olynyk",
NA, "Bam Adebayo", "Justise Winslow",
NA, "Tyler Herro", "Duncan Robinson",
NA, "Derrick Jones Jr.", "Bam Adebayo",
NA, "Goran Dragic", "Kendrick Nunn",
NA, "Justise Winslow", "Tyler Herro",
NA, "Kelly Olynyk", "Meyers Leonard",
NA, "Bam Adebayo", "Justise Winslow"
)

然后,我创建一个列:
lineups %>%
mutate(lineupAfter = str_replace(lineupBefore, playerOut, playerIn))

这导致:
tibble::tribble(
~lineupBefore, ~playerOut, ~playerIn, ~lineupAfter,
"Justise Winslow, Bam Adebayo, Meyers Leonard, Tyler Herro, Kendrick Nunn", "Kendrick Nunn", "Goran Dragic", "Justise Winslow, Bam Adebayo, Meyers Leonard, Tyler Herro, Goran Dragic",
NA, "Justise Winslow", "Derrick Jones Jr.", NA,
NA, "Meyers Leonard", "Kelly Olynyk", NA,
NA, "Bam Adebayo", "Justise Winslow", NA,
NA, "Tyler Herro", "Duncan Robinson", NA,
NA, "Derrick Jones Jr.", "Bam Adebayo", NA,
NA, "Goran Dragic", "Kendrick Nunn", NA,
NA, "Justise Winslow", "Tyler Herro", NA,
NA, "Kelly Olynyk", "Meyers Leonard", NA,
NA, "Bam Adebayo", "Justise Winslow", NA
)

现在,我想将 lineupBefore 中的 NA 值设为 lineupAfter 中的前一个值。然后,必须将创建 lineupAfter 列的相同函数应用于 lineupBefore 中的新值。如果我尝试用 mutate 来做,它只会替换第一个 NA 行中的值。所以我需要这个函数在每一行上工作,在进入下一行之前将它变成与 NA 不同的东西。我想我需要使用 purrr 来做到这一点,但我不知道如何。任何帮助,将不胜感激!

编辑:

这是前 5 行预期的样子:
tibble::tribble(
~lineupBefore, ~playerOut, ~playerIn, ~lineupAfter,
"Justise Winslow, Bam Adebayo, Meyers Leonard, Tyler Herro, Kendrick Nunn", "Kendrick Nunn", "Goran Dragic", "Justise Winslow, Bam Adebayo, Meyers Leonard, Tyler Herro, Goran Dragic",
"Justise Winslow, Bam Adebayo, Meyers Leonard, Tyler Herro, Goran Dragic", "Justise Winslow", "Derrick Jones Jr.", "Derrick Jones Jr., Bam Adebayo, Meyers Leonard, Tyler Herro, Goran Dragic",
"Derrick Jones Jr., Bam Adebayo, Meyers Leonard, Tyler Herro, Goran Dragic", "Meyers Leonard", "Kelly Olynyk", "Derrick Jones Jr., Bam Adebayo, Kelly Olynyk, Tyler Herro, Goran Dragic",
"Derrick Jones Jr., Bam Adebayo, Kelly Olynyk, Tyler Herro, Goran Dragic", "Bam Adebayo", "Justise Winslow", "Derrick Jones Jr., Justise Winslow, Kelly Olynyk, Tyler Herro, Goran Dragic",
"Derrick Jones Jr., Justise Winslow, Kelly Olynyk, Tyler Herro, Goran Dragic", "Tyler Herro", "Duncan Robinson", "Derrick Jones Jr., Justise Winslow, Kelly Olynyk, Duncan Robinson, Goran Dragic"
)

如您所见,列 lineupBefore 的第 2 行将等于列 lineupAfter 的第 1 行,列 lineupBefore 的第 3 行将等于列 lineupAfter 的第 2 行,依此类推。
同时,lineupAfter 的第 2 行将是 str_replace(lineupBefore, playerOut, playerIn) 应用于 lineupBefore 的第 2 行的结果,依此类推。

最佳答案

我们可以通过赋值循环更新它

v1 <- c(first(lineups$lineupBefore), rep("", nrow(lineups) -1 ))
for(i in 2:nrow(lineups)) v1[i] <- str_replace(v1[i-1],
lineups$playerOut[i-1], lineups$playerIn[i-1])
lineups$lineupBefore <- v1
library(dplyr)
lineups %>%
mutate(lineupAfter = lead(lineupBefore))
# A tibble: 10 x 4
# lineupBefore playerOut playerIn lineupAfter
# <chr> <chr> <chr> <chr>
# 1 Justise Winslow, Bam Adebayo, Meyers Leonar… Kendrick Nunn Goran Dragic Justise Winslow, Bam Adebayo, Meyers Leon…
# 2 Justise Winslow, Bam Adebayo, Meyers Leonar… Justise Wins… Derrick Jon… Derrick Jones Jr., Bam Adebayo, Meyers Le…
# 3 Derrick Jones Jr., Bam Adebayo, Meyers Leon… Meyers Leona… Kelly Olynyk Derrick Jones Jr., Bam Adebayo, Kelly Oly…
# 4 Derrick Jones Jr., Bam Adebayo, Kelly Olyny… Bam Adebayo Justise Win… Derrick Jones Jr., Justise Winslow, Kelly…
# 5 Derrick Jones Jr., Justise Winslow, Kelly O… Tyler Herro Duncan Robi… Derrick Jones Jr., Justise Winslow, Kelly…
# ...

关于r - 如何在转到R中的下一行之前为每一行创建函数返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59556977/

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