gpt4 book ai didi

r - 如何使用数据框中的当前行值检查前一行值

转载 作者:行者123 更新时间:2023-12-04 23:38:43 24 4
gpt4 key购买 nike

如何通过对特定 ID 进行分组,为数据框的所有列动态检查前一行值和当前行值。

我的数据框:

ID  ITEM1     ITEM2    ITEM3           
1 A A A
2 C B C
1 A B C
1 B A C
2 NA B F
3 A A D
4 R G J
4 H T J

例如:
ID  ITEM1     ITEM2    ITEM3    ITEM1change  ITEM2change   ITEM3change                   
1 A A A 0 0 0
1 A B C 0 1 1
1 B A C 1 1 0
2 C B C 0 0 0
2 NA B F 1 0 1
3 A A D 0 0 0
4 R G J 0 0 0
4 H T J 1 1 0

我的最终输出将是:
  Fiels         modifiedcout   unmodifiedcount Total      
ITEM1change 3 5 8
ITEM2change 3 5 8
ITEM3change 2 6 8

我的数据:
structure(list(ID = c(1, 2, 1, 1, 2, 3, 4, 4), ITEM1 = structure(c(1L, 
3L, 1L, 2L, NA, 1L, 5L, 4L), .Label = c("A", "B", "C", "H", "R"
), class = "factor"), ITEM2 = structure(c(1L, 2L, 2L, 1L, 2L,
1L, 3L, 4L), .Label = c("A", "B", "G", "T"), class = "factor"),
ITEM3 = structure(c(1L, 2L, 2L, 2L, 4L, 3L, 5L, 5L), .Label = c("A",
"C", "D", "F", "J"), class = "factor")), .Names = c("ID",
"ITEM1", "ITEM2", "ITEM3"), row.names = c(NA, -8L), class = "data.frame")

最佳答案

一个可能的解决方案:

library(dplyr)
library(tidyr)

df %>%
gather(item, value, -1) %>%
group_by(ID, item) %>%
mutate(change = lag(value, default = first(value)) != value,
change = replace(change, is.na(change), TRUE)) %>%
group_by(item) %>%
summarise(modified = sum(change, na.rm = TRUE),
unmodified = sum(!change, na.rm = TRUE)) %>%
mutate(total = modified + unmodified)

这使:

# A tibble: 3 x 4
item modified unmodified total
<chr> <int> <int> <int>
1 ITEM1 3 5 8
2 ITEM2 3 5 8
3 ITEM3 2 6 8

关于r - 如何使用数据框中的当前行值检查前一行值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46067803/

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