我有一个需要重新编码的大数据集。数据集的每一行都是按时间顺序(时间)从单独的实验(id)中检测出来的。然后对每一种可能的检测进行手动验证。当进行第一次真检测时,它被标记为(注释)“第一次”,当最后一次真正检测进行时,它被标记为“最后”。如果没有检测,则输入“none”。
我正准备用if的声明重新编码。1)首先,我要选择变量id的所有情况,其中第一次和最后一次都是存在的,然后它需要用'no_comment‘填充第一次和最后一次之间的所有情况,然后用"MVND“填写第一次和最后一次和最后一次之后的所有情况。2)选择只有"none“存在的id情况,并在id大小写的所有行中填充"none”。单独的代码行正在工作,但由于某种原因,当我在蹒跚学步内将它们合并到if语句中时,它们没有一起工作--它们只是返回原始的data.frame。我想,如果我想要做的事情有其他的结构,我就错了。
#approximate data structure for this case:
y <-data.frame(id=c(rep("a",10),rep("b",10),rep("c",10)),time=rep(1:10, 3), Comments=rep(NA,30))
y$Comments[c(2,11,23)]<-"first"
y$Comments[c(9,19,30)]<-"last"
#x=y[y$id=="a",] #testing specific lines
#recursive process to step through the data
ddply(y,.(id), .fUN=function(x){
if(all(unique(na.omit(x$Comments))%in%c("first","last"))){
f<-which(x$Comments == "first")
l<-which(x$Comments == "last")
#Add no comment to all records between first and last
x$Comments[(f+1):(l - 1)]<- "no_comment"
#if 'first' isn't the first record add MVND to all things before 'first'
if(f>1){x$Comments[1:(f-1)]<-"MVND"}
#if 'last' isn't the last record add MVND to all records after 'last'.
if(l<nrow[x]){x$Comments[(l+1):nrow(x)]<-"MVND"}
}else if(unique(na.omit(x$Comments))=="none"){
x$Comments<-"none" #if the only unique comment is "none" set all comments to none
}
}
)如果数据表是一个更好的方式来做到这一点,我是游戏,以了解如何在dt中这样做。
#编辑:上述修改是为了扩大我正在处理的“第一/最后”和“无”两类案件。Jon的解决方案非常有效,因为我最初只在第一个/最后一个案例中发布了示例数据。
发布于 2021-03-27 22:20:33
对于这个任务,我最喜欢的方法是data.table,原因有两个:
在非equi联接中进行更新。
为了涵盖OP提到的所有用例,我们需要创建一个增强的示例数据集。
y <- data.frame(
id = rep(letters[1:5], each = 5L),
time = rep(1:5, 5L),
Comments = rep(NA_character_, 25L))
y$Comments[c(2, 6, 13, 22)] <- "first"
y$Comments[c(4, 9, 15, 23)] <- "last"
y$Comments[c(18)] <- "none"
yid time Comments 1 a 1 <NA> 2 a 2 first 3 a 3 <NA> 4 a 4 last 5 a 5 <NA> 6 b 1 first 7 b 2 <NA> 8 b 3 <NA> 9 b 4 last 10 b 5 <NA> 11 c 1 <NA> 12 c 2 <NA> 13 c 3 first 14 c 4 <NA> 15 c 5 last 16 d 1 <NA> 17 d 2 <NA> 18 d 3 none 19 d 4 <NA> 20 d 5 <NA> 21 e 1 <NA> 22 e 2 first 23 e 3 last 24 e 4 <NA> 25 e 5 <NA>
现在,我们可以插入丢失的Comments
library(data.table)
y <- setDT(copy(y))
# copy "none" to all rows of the id group in case one Comment is "none"
y[, Comments := if (isTRUE(any(Comments == "none"))) "none" , by = id][]
# create look-up table
lut <- dcast(y[which(Comments %in% c("first", "last"))], id ~ Comments, value.var = "time")
# update in non-equi joins
y[lut, on = .(id, time < first), Comments := "MVND"][]
y[lut, on = .(id, time > last), Comments := "MVND"][]
y[lut, on = .(id, time > first, time < last), Comments := "no commments"][]id time Comments 1: a 1 MVND 2: a 2 first 3: a 3 no commments 4: a 4 last 5: a 5 MVND 6: b 1 first 7: b 2 no commments 8: b 3 no commments 9: b 4 last 10: b 5 MVND 11: c 1 MVND 12: c 2 MVND 13: c 3 first 14: c 4 no commments 15: c 5 last 16: d 1 none 17: d 2 none 18: d 3 none 19: d 4 none 20: d 5 none 21: e 1 MVND 22: e 2 first 23: e 3 last 24: e 4 MVND 25: e 5 MVND id time Comments
查找表lut包含发生first和last的时间,resp.:
id first last 1: a 2 4 2: b 1 4 3: c 3 5 4: e 2 3
请注意,我们假设生产数据集“行为良好”,即,
任何"none"
"last".
列中的"first"和"last",在Comments列中,、
"first"总是在"last".之前出现的。
发布于 2021-03-26 16:52:39
不确定是否对您有用,但下面是我如何使用dplyr的方法。由于这是矢量化的,我预计它将比基于循环的方法运行得更快。
library(dplyr)
y %>%
group_by(id) %>%
dplyr::mutate(Comments2 = case_when( # in case `plyr` is loaded
cumsum(coalesce(lag(Comments == "last"), FALSE)) >= 1 ~ "MVND",
cumsum(coalesce(Comments == "first", FALSE)) < 1 ~ "MVND",
is.na(Comments) ~ "no_comment",
TRUE ~ Comments)) %>%
ungroup()这里的棘手部分是MVND书签,在这里,我计算我们是通过了last还是还没有到达first。coalesce将第一个项中的任何NA转换为第二个项中的FALSE值。cumsum在这里将TRUE值相加。
这是我得到的结果,使用datapasta作为一个小块粘贴。据我所知,输出看起来和预期的一样:
tibble::tribble(
~id, ~time, ~Comments, ~Comments2,
"a", 1L, NA, "MVND",
"a", 2L, "first", "first",
"a", 3L, NA, "no_comment",
"a", 4L, NA, "no_comment",
"a", 5L, NA, "no_comment",
"a", 6L, NA, "no_comment",
"a", 7L, NA, "no_comment",
"a", 8L, NA, "no_comment",
"a", 9L, "last", "last",
"a", 10L, NA, "MVND",
"b", 1L, "first", "first",
"b", 2L, NA, "no_comment",
"b", 3L, NA, "no_comment",
"b", 4L, NA, "no_comment",
"b", 5L, NA, "no_comment",
"b", 6L, NA, "no_comment",
"b", 7L, NA, "no_comment",
"b", 8L, NA, "no_comment",
"b", 9L, "last", "last",
"b", 10L, NA, "MVND",
"c", 1L, NA, "MVND",
"c", 2L, NA, "MVND",
"c", 3L, "first", "first",
"c", 4L, NA, "no_comment",
"c", 5L, NA, "no_comment",
"c", 6L, NA, "no_comment",
"c", 7L, NA, "no_comment",
"c", 8L, NA, "no_comment",
"c", 9L, NA, "no_comment",
"c", 10L, "last", "last"
)https://stackoverflow.com/questions/66820418
复制相似问题