-2

在此处输入图像描述![在此处输入图像描述][2]我有一个数据集,其中包含以随机模式出现的 8 个单词(例如“klein”、“warm”)的列表。我需要创建一个变量,它向我显示在单词的特定出现和最后一次出现之间有多少次试验。例如,如果一个单词连续出现两次,这个新变量应该是 0,如果中间有另一个单词,这个变量应该是 1,以此类推。谁能帮助我并给我一个提示?先感谢您!ps你可以看到图片是在SPSS中是怎么做的

在此处输入图像描述

ps 输入()

structure(list(ExperimentName = c("Habit_Experiment", "Habit_Experiment", 
"Habit_Experiment", "Habit_Experiment", "Habit_Experiment", "Habit_Experiment", 
"Habit_Experiment", "Habit_Experiment", "Habit_Experiment", "Habit_Experiment", 
"Habit_Experiment", "Habit_Experiment", "Habit_Experiment", "Habit_Experiment", 
"Habit_Experiment", "Habit_Experiment", "Habit_Experiment", "Habit_Experiment", 
"Habit_Experiment", "Habit_Experiment", "Habit_Experiment", "Habit_Experiment", 
"Habit_Experiment", "Habit_Experiment", "Habit_Experiment", "Habit_Experiment", 
"Habit_Experiment", "Habit_Experiment", "Habit_Experiment", "Habit_Experiment"
), Subject = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), AccP = c(90.38461, 90.38461, 90.38461, 90.38461, 90.38461, 
90.38461, 90.38461, 90.38461, 90.38461, 90.38461, 90.38461, 90.38461, 
90.38461, 90.38461, 90.38461, 90.38461, 90.38461, 90.38461, 90.38461, 
90.38461, 90.38461, 90.38461, 90.38461, 90.38461, 90.38461, 90.38461, 
90.38461, 90.38461, 90.38461, 90.38461), Age = c(28L, 28L, 28L, 
28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 
28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 
28L), Handedness = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("links", "rechts"), class = "factor"), 
    PracFail.RT = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L), Sex = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("divers", 
    "männlich", "weiblich"), class = "factor"), Block = 1:30, 
    Colour = c("YELLOW", "RED", "YELLOW", "YELLOW", "YELLOW", 
    "RED", "YELLOW", "YELLOW", "YELLOW", "YELLOW", "RED", "RED", 
    "RED", "RED", "YELLOW", "RED", "RED", "YELLOW", "RED", "RED", 
    "YELLOW", "RED", "YELLOW", "RED", "RED", "YELLOW", "DODGERBLUE", 
    "LIME", "DODGERBLUE", "LIME"), contingency.RESP = c("", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", ""), contWord = c("", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", ""), 
    Correct = c("l", "d", "l", "l", "l", "d", "l", "l", "l", 
    "l", "d", "d", "d", "d", "l", "d", "d", "l", "d", "d", "l", 
    "d", "l", "d", "d", "l", "l", "d", "l", "d"), Data = 1:30, 
    Data.Sample = 1:30, Rare_C = c("", "", "True", "False", "False", 
    "False", "True", "False", "False", "False", "False", "False", 
    "False", "True", "True", "True", "True", "False", "True", 
    "False", "False", "False", "False", "False", "False", "True", 
    "", "", "True", "False"), Stim.ACC = c(0L, 1L, 0L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L), Stim.CRESP = c("l", 
    "d", "l", "l", "l", "d", "l", "l", "l", "l", "d", "d", "d", 
    "d", "l", "d", "d", "l", "d", "d", "l", "d", "l", "d", "d", 
    "l", "l", "d", "l", "d"), Stim.RESP = c("d", "d", "d", "l", 
    "l", "d", "l", "l", "l", "l", "d", "d", "d", "d", "l", "d", 
    "d", "l", "d", "d", "d", "d", "l", "d", "d", "l", "d", "d", 
    "l", "d"), Stim.RT = c(NA, 808L, NA, 691L, 462L, 884L, 443L, 
    466L, 444L, 385L, 474L, 441L, 399L, 347L, 398L, 418L, 383L, 
    451L, 304L, 389L, NA, 467L, 395L, 338L, 333L, 327L, NA, 562L, 
    460L, 374L), Word = c("XXXX", "XXXX", "warm", "leicht", "leicht", 
    "warm", "klein", "ganz", "leicht", "leicht", "klein", "klein", 
    "warm", "ganz", "warm", "leicht", "leicht", "ganz", "ganz", 
    "klein", "ganz", "warm", "ganz", "warm", "klein", "klein", 
    "XXXX", "XXXX", "weich", "klar")), row.names = c(NA, 30L), class = "data.frame")
4

1 回答 1

1

您可能不需要循环来执行此操作。这是使用该dplyr软件包的可能解决方案。

假设你的数据框被调用df,这里我先用select删除其他列进行演示。您可以删除此行以将其他列保留在数据框中。

接下来,为每个受试者的数据中的每一行添加试验编号。然后,如果您使用group_byfor each Subjectand Word,您可以计算出last_occurrence哪个是试用值的差异Word

第一次出现的单词将是NA

library(dplyr)

df %>%
  select(Subject, Word) %>%
  group_by(Subject) %>%
  mutate(trial = row_number()) %>%
  group_by(Subject, Word) %>%
  mutate(last_occurrence = trial - lag(trial) - 1)

输出

   Subject Word   trial last_occurrence
     <int> <chr>  <int>           <dbl>
 1       1 XXXX       1              NA
 2       1 XXXX       2               0
 3       1 warm       3              NA
 4       1 leicht     4              NA
 5       1 leicht     5               0
 6       1 warm       6               2
 7       1 klein      7              NA
 8       1 ganz       8              NA
 9       1 leicht     9               3
10       1 leicht    10               0
11       1 klein     11               3
12       1 klein     12               0
13       1 warm      13               6
14       1 ganz      14               5
15       1 warm      15               1
16       1 leicht    16               5
17       1 leicht    17               0
18       1 ganz      18               3
19       1 ganz      19               0
20       1 klein     20               7
21       1 ganz      21               1
22       1 warm      22               6
23       1 ganz      23               1
24       1 warm      24               1
25       1 klein     25               4
26       1 klein     26               0
27       1 XXXX      27              24
28       1 XXXX      28               0
29       1 weich     29              NA
30       1 klar      30              NA
于 2021-01-17T00:59:49.873 回答