代码之家  ›  专栏  ›  技术社区  ›  Matt Parker

纵向比较R…中的值

  •  2
  • Matt Parker  · 技术社区  · 16 年前

    dat <- structure(list(Participant_ID = c("A", "A", "A", "A", "B", "B", 
    "B", "B", "C", "C", "C", "C"), phase = structure(c(1L, 2L, 3L, 
    4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("base", "sixmos", 
    "twelvemos", "eighteenmos"), class = "factor"), result = c("Negative", 
    "Negative", "Negative", "Negative", "Negative", "Positive", "Negative", 
    NA, "Positive", "Indeterminate", "Negative", "Negative")), .Names = c("Participant_ID", 
    "phase", "result"), row.names = c(1L, 2L, 3L, 4L, 97L, 98L, 99L, 
    100L, 9L, 10L, 11L, 12L), class = c("cast_df", "data.frame"))
    

    看起来像:

        Participant_ID       phase        result
    1                A        base      Negative
    2                A      sixmos      Negative
    3                A   twelvemos      Negative
    4                A eighteenmos      Negative
    97               B        base      Negative
    98               B      sixmos      Positive
    99               B   twelvemos      Negative
    100              B eighteenmos          <NA>
    9                C        base      Positive
    10               C      sixmos Indeterminate
    11               C   twelvemos      Negative
    12               C eighteenmos      Negative
    

    我想为每个测试添加一个标识符,以说明该测试是从以前的状态(从负到正)的转换,还是恢复(从正到负)或稳定。问题是,我不仅仅是比较基础测试和六个月测试,六个月到十二个月,等等-在像C这样的情况下,六个月测试应该被标记为稳定或不确定(确切的术语是不明确的),而且(更重要的是)十二个月测试应该和基础测试进行比较,并被标记为回归。相反,如果某人有一系列的“否定”、“不确定”、“否定”,那应该是稳定的。

    这是后一部分,我被困在;如果只是对每个参与者进行一系列的比较,我会没事的,但我在思考如何优雅地处理这些可变的比较对时遇到了困难。一如既往,我们非常感谢你的帮助。

    1 回复  |  直到 16 年前
        1
  •  2
  •   Eduardo Leoni    16 年前

    我不认为您概述了在所有可能的情况下应该发生什么(例如,当序列为“不确定,不确定”时的状态是什么?),但这里有一个想法:将“不确定”的情况视为缺失,并使用package zoo的na.locf来“插补”它们,以结转这些值(或者更好,重新实现它来解决您的问题。)

    library(plyr)
    at <- at[with(at, order(Participant_ID, phase)),]
    at <- ddply(at, "Participant_ID", function(x) {
        ## have to figure out what to do with missing data
        result.fix <- na.locf(car::recode(x$result, "'Negative'=0; 'Positive'=1;'Indeterminate'=NA;NA=1000"))
        x$status <- NA
        x$status[-1] <- result.fix[-1]-result.fix[-length(result.fix)]
        x$status <- car::recode(x$status, "-1='reversion'; 1='conversion'; 0='stable'; else=NA")
        x$status[x$result=="Indeterminate"] <- "stable or inconclusive"
        x
    })
    

    不过,我不确定这算不算优雅!