在R中实例化条件语句

时间:2013-01-18 12:40:35

标签: r if-statement dataframe pseudocode

我有一个如下所示的数据框:

> head(Data1A)
        Prime Target CorrectAnswer Accuracy   RT   Block TrialType
1     bezaaid barkme    PseudoWord        1 3450 Block 1        NA
2       hazen lemmet          Word        1 1108 Block 1        NA
3  bergtoppen slavin          Word        1 1160 Block 1        NA
4       wekte gareel          Word        1 1250 Block 1        NA
5      zessen studio          Word        1  557 Block 1        NA
6 nauwkeurige hotens    PseudoWord        1  882 Block 1        NA

如您所见,必须填充列TrialType,我需要一个条件语句来完成它。我知道我想要什么,而且我尽可能地用伪代码写下来,但是我能够用正确的语法实现这一点。任何帮助深表感谢。

伪代码:

对于Data1A$TrialType中的每个单元格Data1A$Block中的相应单元格等于Block 2Block 3Data1A$CorrectAnswer中的相应行等于Word {1}}或者,如果Data1A $ Block中的相应单元格等于“Block 4”或“Block 5”且Data1A$CorrectAnswer中的相应单元格等于PseudoWord,则TrialType中的相应单元格如果Regular中的相应单元格等于Data1A$BlockBlock 1Block 6中相应的单元格相等,则1}}将等于Data1A$Block。到Block 2Block3Data1A$CorrectAnswer中的相应行等于pseudoWord或者如果Data1A$Block中的相应单元格等于Block 4Block 5 Data1A$CorrectAnswer中的相应单元格等于WordData1A$TrialType中的相应单元格等于Random

我希望这是可以理解的。如果我对伪代码的意图存在疑问,我会检查。

1 个答案:

答案 0 :(得分:2)

我真的很难在这里简化你的逻辑。

但这应该有用。

我生成了一些数据

dat <- read.table(text =        "Prime Target CorrectAnswer Accuracy   RT   Block TrialType
                  1     bezaaid barkme    PseudoWord        1 3450 'Block 4'        NA
                  2       hazen lemmet          Word        1 1108 'Block 5'        NA
                  3  bergtoppen slavin          Word        1 1160 'Block 4'        NA
                  1     bezaaid barkme    PseudoWord        1 3450 'Block 5'        NA
                  2       hazen lemmet          Word        1 1108 'Block 2'        NA
                  3  bergtoppen slavin          Word        1 1160 'Block 3'        NA
                  1     bezaaid barkme    PseudoWord        1 3450 'Block 2'        NA
                  2       hazen lemmet          Word        1 1108 'Block 3'        NA
                  3  bergtoppen slavin          Word        1 1160 'Block 1'        NA
                  4       wekte gareel          Word        1 1250 'Block 6'        NA
                  5      zessen studio          Word        1  557 'Block 1'        NA
                  6 nauwkeurige hotens    PseudoWord        1  882 'Block 3'        NA",header = T,row.names = NULL)

然后我实现你的逻辑:

transform(dat,
          TrialType= ifelse(Block %in% c('Block 4','Block 5'),
                            ifelse(CorrectAnswer =="Word" ,"Random","Regular"),
                            ifelse(Block  %in% c('Block 2','Block 3'),
                                   ifelse(CorrectAnswer =="Word" ,"Regular","Random"),
                                   'Random')))

结果

  row.names       Prime Target CorrectAnswer Accuracy   RT   Block TrialType
1          1     bezaaid barkme    PseudoWord        1 3450 Block 4   Regular
2          2       hazen lemmet          Word        1 1108 Block 5    Random
3          3  bergtoppen slavin          Word        1 1160 Block 4    Random
4          1     bezaaid barkme    PseudoWord        1 3450 Block 5   Regular
5          2       hazen lemmet          Word        1 1108 Block 2   Regular
6          3  bergtoppen slavin          Word        1 1160 Block 3   Regular
7          1     bezaaid barkme    PseudoWord        1 3450 Block 2    Random
8          2       hazen lemmet          Word        1 1108 Block 3   Regular
9          3  bergtoppen slavin          Word        1 1160 Block 1    Random
10         4       wekte gareel          Word        1 1250 Block 6    Random
11         5      zessen studio          Word        1  557 Block 1    Random
12         6 nauwkeurige hotens    PseudoWord        1  882 Block 3    Random