0

我有一个看起来像这样的数据框:

> 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 中的对应单元格Data1A$Block等于“Block 4”或“Block 5”并且对应的单元格Block 2in等于如果 in 对应的单元格等于或,则对应的单元格 in将等于else 如果对应的单元格 in等于or并且对应的行等于OR,如果对应的单元格 in等于 or 并且对应的单元格等于Block 3Data1A$CorrectAnswerWordData1A$CorrectAnswerPseudoWordTrialTypeRegularData1A$BlockBlock 1Block 6Data1A$BlockBlock 2Block3Data1A$CorrectAnswerpseudoWordData1A$BlockBlock 4Block 5Data1A$CorrectAnswerWord那么对应的单元格Data1A$TrialType等于Random

我希望这是可以理解的。如果有人怀疑我对伪代码的意图,我将进行检查。

4

1 回答 1

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
于 2013-01-18T13:34:00.033 回答