计算字符串在列中出现的次数

count number of times string appears in a column

你能想出一种直观的方法来计算单词 space 在特定列中出现的次数吗?或者任何其他可行的解决方案。 我基本上想知道 space 键被按下了多少次,但是有些参与者犯了错误并按下了其他键,这也被认为是错误的。所以我想知道我是否应该使用“key_resp.rt”列来代替计算响应次数。如果您知道如何同时使用这两种方法,那就太好了,因为我可能需要同时使用这两种方法。

我使用了下面的代码,结果与数据不符。

 Data %>% group_by(Participant, Session) %>% summarise(false_start = sum(str_count(key_resp.keys, "space")))

这是我的数据片段:

    Participant    RT     Session   key_resp.keys           key_resp.rt
       X        0.431265    1       ["space"]            [2.3173399999941466]
       X        0.217685    1           
       X        0.317435    2       ["space","space"] [0.6671900000001187,2.032510000000002]    2020.1.3    4
       Y        0.252515    1       
       Y        0.05127     2   ["space","space","space","space","space","space","space","space","space"]   [4.917419999999765,6.151149999999689,6.333714999999771,6.638249999999971,6.833514999999338,7.0362499999992,7.217724999999504,7.38576999999988,7.66913999999997]
dput(droplevels(head(Data_PVT)))
structure(list(Interval_stimulus = c(4.157783411, 4.876139922, 
5.67011868, 9.338167417, 9.196342656, 7.62448411), Participant = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = "ADH80254", class = "factor"), 
    RT = c(431.265, 277.99, 253.515, 310.53, 299.165, 539.46), 
    Session = c(1L, 1L, 1L, 1L, 1L, 1L), date = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "2020-06-12_11h11.47.141", class = "factor"), 
    key_resp.keys = structure(c(2L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
    "[\"space\"]"), class = "factor"), key_resp.rt = structure(c(2L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("", "[2.3173399999941466]"
    ), class = "factor"), psychopyVersion = structure(c(1L, 1L, 
    1L, 1L, 1L, 1L), .Label = "2020.1.3", class = "factor"), 
    Trials = 0:5, Reciprocal = c(2.31875992719094, 3.59725169970143, 
    3.94453977082224, 3.22030077609249, 3.3426370063343, 1.85370555740926
    )), row.names = c(NA, 6L), class = "data.frame")

预期输出:

Participant  Session  false_start
   x             1       0
   x             2       1
   y             1       2
   y             2       1
   z             1       10
   z             2       3

我们可以使用 str_count 计算每个 ParticipantSessionsum"space" 值以获得总数。对于all_false_start,我们计算其中的单词数。

library(dplyr)
library(stringr)

df %>%
  group_by(Participant, Session) %>%
  summarise(false_start = sum(str_count(key_resp.keys, '\bspace\b')), 
            all_false_start = sum(str_count(key_resp.keys, '\b\w+\b')))