【问题标题】:Ifelse statement for multiple, alternating conditions多个交替条件的 ifelse 语句
【发布时间】:2016-05-08 07:13:03
【问题描述】:

我的数据涉及在多个条件下收集的一个因变量 (DV) 的实验,其结构如下:

AnalysedData <- data.frame(Participant = c("Bill", "Bill", "Bill", "Harry", "Harry", "Harry", "Paul", "Paul", "Paul"),
                           Code = c(1, 1, 1, 2, 2, 2, 1, 1, 1),
                           Condition = c("Con", "Expr", "Plac", "Con", "Expr", "Plac", "Con", "Expr", "Plac"),
                           DV = c(26.07, 26.06, 26.05, 26.09, 26.04, 26.65, 26.64, 26.62, 26.63))

所有参与者都接受了相同的条件,但以随机顺序完成了其中的两个条件 - 控制条件总是首先进行。那些在第二次试验中完成实验 (Expr) 条件的人在Code 中获得 1,而在第二次试验中完成安慰剂 (Plac) 的人在Code 中获得 2。

我希望添加一个新列AnalysedData$Trial,其中包括每个参与者的试验顺序。我的首选输出是:

AnalysedData <- data.frame(Participant = c("Bill", "Bill", "Bill", "Harry", "Harry", "Harry", "Paul", "Paul", "Paul"),
                           Code = c(1, 1, 1, 2, 2, 2, 1, 1, 1),
                           Condition = c("Con", "Expr", "Plac", "Con", "Expr", "Plac", "Con", "Expr", "Plac"),
                           DV = c(26.07, 26.06, 26.05, 26.09, 26.04, 26.65, 26.64, 26.62, 26.63),
                           Trial = c(1, 2, 3, 1, 3, 2, 1, 2, 3))

哈利完成安慰剂条件第二和实验条件第三的首选输出地址,给他c(1, 3, 2)。相比之下,Bill 和 Paul 都完成了第二个实验条件和第三个安慰剂条件,给出了c(1, 2, 3)。如上所述,此备用序列由 Code 表示,对于 Bill 和 Paul,等于 1,对于 Harry,等于 2。

我知道我可以使用以下内容添加 1 来指定第一个试验已完成(Con 所有参与者):

AnalysedData$Trial <- ifelse(AnalysedData$Condition == "Con", 1, 0)

以上多个条件如何完成?

【问题讨论】:

    标签: r


    【解决方案1】:

    结合ifelseas.integer(或as.numeric)和factor,您可以利用因子变量存储为带标签的整数这一事实:

    AnalysedData$Trial <- ifelse(AnalysedData$Code==1, 
                                 as.integer(factor(AnalysedData$Condition, 
                                                   levels = c("Con", "Expr", "Plac"))),
                                 as.integer(factor(AnalysedData$Condition, 
                                                   levels = c("Con", "Plac", "Expr"))))
    

    你得到:

    > AnalysedData
      Participant Code Condition    DV Trial
    1        Bill    1       Con 26.07     1
    2        Bill    1      Expr 26.06     2
    3        Bill    1      Plac 26.05     3
    4       Harry    2       Con 26.09     1
    5       Harry    2      Expr 26.04     3
    6       Harry    2      Plac 26.65     2
    7        Paul    1       Con 26.64     1
    8        Paul    1      Expr 26.62     2
    9        Paul    1      Plac 26.63     3
    

    【讨论】:

    • 谢谢,我意识到我在示例中有Code,在预期的输出中有Order。我已经修改,以便Code 出现在两者中。很抱歉给您带来困惑,并感谢您的回答!
    【解决方案2】:

    这应该可行:

    AnalysedData$Trial <- ifelse(AnalysedData$Condition == "Con", 1, 0)
    
    index<-AnalysedData$Condition == "Plac"
    
    AnalysedData$Trial[index]<-rep(2,sum(index))
    

    【讨论】:

    • 谢谢,但结果是:dput(AnalysedData) structure(list(Participant = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("Bill", "Harry", "Paul"), class= "factor"), Code = c(1, 1, 1, 2, 2, 2, 1, 1, 1), Condition = structure( c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Con", "Expr", "Plac"), class= "因子"), DV = c( 26.07, 26.06, 26.05, 26.09, 26.04, 26.65, 26.64, 26.62, 26.63), Trial = c(1, 0, 2, 1, 0, 2, 1, 0, 2)), .Names = c("参与者", "代码", "条件", "DV", "试用"), row.names = c(NA, -9L), class= "data.frame")
    • 我不确定我是否已经清楚地解释了我的问题,因为我在 AnalysedData$Trial
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多