gpt4 book ai didi

r - 比较数据框中组内的行

转载 作者:行者123 更新时间:2023-12-02 08:29:11 25 4
gpt4 key购买 nike

我有一个示例数据框

dF <- structure(list(status = structure(c(1L, 1L, 1L, 4L, 1L, 3L, 1L, 
1L, 2L, 4L, 4L, 2L), .Label = c("complete", "go", "no go", "revise"
), class = "factor"), group = structure(c(1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("101", "102", "103"), class =
"factor"),
date = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L), .Label = c("1", "2", "3", "4"), class = "factor")),
.Names = c("status",
"group", "date"), row.names = c(NA, -12L), class = "data.frame")

我想将 dF$status[2]dF$status[1]dF$status[3] 进行比较到 dF$status[2] 等等,在每个组中。我可以使用一个简单的函数和 ddply() 相对轻松地完成此操作:

  state_change_function <- function(x){

tmp <- integer(length = nrow(x))

for(i in 2:nrow(x)){
if(x$statu[i] == x$status[i-1]){
tmp[i] <- "no change"
} else {
tmp[i] <- "state change"
}
}
return(tmp)
}

state_change <- ddply(dF, .(group), state_change_function)

这提供了一个非常简单的输出,然后我可以使用 reshapemelt() 并将其作为新列附加到我的 dF .

> state_change
group V1 V2 V3 V4
1 101 0 no change no change state change
2 102 0 state change state change no change
3 103 0 state change no change state change

我的问题是当我在组中有不同数量的行时。例如,如果 dF 突然丢失了一行 where `dF$group == 102",

dF1 <- structure(list(status = structure(c(1L, 1L, 1L, 4L, 3L, 1L, 1L, 
2L, 4L, 4L, 2L), .Label = c("complete", "go", "no go", "revise"
), class = "factor"), group = structure(c(1L, 1L, 1L, 1L, 2L,
2L, 2L, 3L, 3L, 3L, 3L), .Label = c("101", "102", "103"), class = "factor"),
date = structure(c(1L, 2L, 3L, 4L, 2L, 3L, 4L, 1L, 2L, 3L,
4L), .Label = c("1", "2", "3", "4"), class = "factor")), .Names =
c("status",
"group", "date"), row.names = c(NA, -11L), class = "data.frame")

然后运行相同的函数会导致错误:

state_change <- ddply(dF1, .(group), state_change_function)
Error in list_to_dataframe(res, attr(.data, "split_labels"), .id, id_as_factor) :
Results do not have equal lengths

我在 SO 上找到了一个使用不同函数的部分解决方案:

state_change_function <- function(data){    
output <- integer(length(rrsIdeas)-1)
for(i in seq_along(output)){
output[[i]] <- (data$status[i] == data$status[i+1])
}
return(output)
}

state_change <- ddply(dF1, .(group), state_change_function)

并提供不同的输出:

> state_change
group V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17
1 101 1 1 0 NA NA NA NA NA NA NA NA NA NA NA NA NA NA
2 102 0 1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
3 103 0 1 0 NA NA NA NA NA NA NA NA NA NA NA NA NA NA

我对这个输出的问题是,在没有更多工作的情况下,melt() 和附加到我原来的 dF1 要困难得多,因为组 102 101102 的几个列中有数据。这尤其困难,因为我有超过 1500 个组要应用此函数,它们的 nrow() 可能会随时间变化。

我想要的是一个函数,它将每一行与组中的前一行进行比较,并且——理想情况下——输出一个类似 dataFrame 的函数

group  V1
101 0
101 no change
101 no change
101 state change
102 0
102 state change
102 state change
102 no change
etc...

但是,如果某些组的行数少于其他组,则可以限制该组的 dataFrame 中的行数。

我在这里和其他地方搜索过帮助,但没有找到我要找的东西。我确信这是可能的,我可能会忽略一些非常简单的事情。

感谢您的帮助。

最佳答案

data.table 的解决方案:

library(data.table)

setDT(dF1)[,V1:=c("0",ifelse(head(status,-1)!=status[-1],'change','no change')),group]

# status group date V1
# 1: complete 101 1 0
# 2: complete 101 2 no change
# 3: complete 101 3 no change
# 4: revise 101 4 change
# 5: no go 102 2 0
# 6: complete 102 3 change
# 7: complete 102 4 no change
# 8: go 103 1 0
# 9: revise 103 2 change
#10: revise 103 3 no change
#11: go 103 4 change

关于r - 比较数据框中组内的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29171403/

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