根据来自其他两列的匹配字符串创建第三列

时间:2018-01-14 15:57:04

标签: r

我正在尝试计算并为测试中的得分创建一个新列。 function checkFalse(obj) { for (var i in obj) { for (var j in obj[i]) { if (obj[i][j] == true) return true; } } return false; } var test = {"colors":{"blue":false}, "sizes":{"square":false}}; console.log(checkFalse(test)); // for this you will get false since you do not have true in it. var test = {"colors":{"blue":false}, "sizes":{"square":true}}; console.log(checkFalse(test)); // for this you will get true since you have a true in it. 是一个列,用于指定通过网格坐标选择的测试的正确答案。 Recall.CRESP显示参与者的回应。

这些列看起来像这样:

Recall.RESP

因此,例如在此表的第1行中,参与者得到5/5正确,因为|Recall.CRESP |Recall.RESP | |---------------------------------|---------------------------------| |grid35grid51grid12grid43grid54 |grid35grid51grid12grid43grid54 | |grid11gird42gird22grid51grid32 |grid11gird15gird55grid42grid32 | 的网格坐标与Recall.CRESP匹配。但是在第2行中,参与者只有2/5正确,因为只有第一个和最后一个网格坐标是相同的。坐标的顺序必须匹配才能正确。

我的新列应分别为两行显示5和2。我不确定如何拆分网格坐标,也告诉R命令必须匹配才能正确。

2 个答案:

答案 0 :(得分:1)

处理此问题的一个好方法是使用列表列,其中您可以以易于迭代的方式存储一整套响应或值。在tidyverse语法中,

library(tidyverse)

responses <- data_frame(Recall.CRESP = c("grid35grid51grid12grid43grid54", "grid11gird42gird22grid51grid32"), 
                        Recall.RESP = c("grid35grid51grid12grid43grid54", "grid11gird15gird55grid42grid32"))

scored <- responses %>% 
    mutate_all(~strsplit(.x, '[^^]g[ri]{2}d')) %>%    # split on all but first "grid"/"gird"
    mutate(correct = map2(Recall.CRESP, Recall.RESP, `==`), 
           score = map_int(correct, sum))

scored
#> # A tibble: 2 x 4
#>   Recall.CRESP Recall.RESP correct   score
#>   <list>       <list>      <list>    <int>
#> 1 <chr [5]>    <chr [5]>   <lgl [5]>     5
#> 2 <chr [5]>    <chr [5]>   <lgl [5]>     2

如果您想仔细查看数据,请拉出各列。

答案 1 :(得分:0)

您可以使用简单tidyverse和自定义mapply功能split_grid进行此操作(我假设只有数字相关):

df <- data_frame(Recall.CRESP = c("grid35grid51grid12grid43grid54", "grid11gird42gird22grid51grid32"),
                 Recall.RESP = c("grid35grid51grid12grid43grid54", "grid11gird15gird55grid42grid32"))

split_grid <- function(x) {
    unlist(regmatches(x, gregexpr("[[:digit:]]+", x)))
}

compare <- function(x, y) {
    sum(split_grid(x) == split_grid(y))
}

df$Res <- mapply(compare, df$Recall.CRESP, df$Recall.RESP)

# A tibble: 2 x 3
  Recall.CRESP                   Recall.RESP                      Res
  <chr>                          <chr>                          <int>
1 grid35grid51grid12grid43grid54 grid35grid51grid12grid43grid54     5
2 grid11gird42gird22grid51grid32 grid11gird15gird55grid42grid32     2