In R: How to compare nested lists in data.frame

Viewed 34

My data frame looks like this for one person (ID):

# Groups:   ID, count_b2_attention_seq [12]
      ID count_b2_attention_seq solution                    solution_resp                
   <int>                  <int> <chr>                       <chr>                        
 1   111                      0 [[-1, 1, 1, 1, -1, -1, 1, ~ [[-1, 1, 1, 1, -1, -1, 1, 1]~
 2   111                      1 [[1, -1, 1, 1, -1, 1, 1, -~ [[1, -1, 1, 1, -1, 1, 1, -1]~
 3   111                      2 [[1, 1, -1, -1, 1, 1, -1, ~ [[1, 1, -1, -1, 1, 1, -1, 1]~
 4   111                      3 [[-1, 1, 1, 1, -1, -1, 1, ~ [[-1, 1, 1, 1, -1, -1, 1, 1]~
 5   111                      4 [[1, -1, 1, 1, -1, 1, 1, -~ [[1, -1, 1, 1, -1, 1, 1, -1]~
 6   111                      5 [[1, 1, -1, -1, 1, 1, -1, ~ [[1, 1, -1, -1, 1, 1, -1, 1]~
 7   111                      6 [[-1, 1, 1, 1, -1, -1, 1, ~ [[-1, 1, 1, 1, -1, -1, 1, 1]~
 8   111                      7 [[1, -1, 1, 1, -1, 1, 1, -~ [[1, -1, 1, 1, -1, 1, 1, -1]~
 9   111                      8 [[1, 1, -1, -1, 1, 1, -1, ~ [[1, 1, -1, -1, 1, 1, -1, 1]~
10   111                      9 [[-1, 1, 1, 1, -1, -1, 1, ~ [[-1, 1, 1, 1, -1, -1, 1, 1]~
11   111                     10 [[1, -1, 1, 1, -1, 1, 1, -~ [[1, 1, 1, 1, -1, 1, 1, -1],~
12   111                     11 [[1, 1, -1, -1, 1, 1, -1, ~ [[1, 1, -1, -1, 1, 1, -1, 1]~

The nested lists involve five sublists of eight element each. Ich would like to make severeal comparisons between solution and solution_resp.

  1. the number of elements that differ in both for each line.
  2. I believe a more difficult comparison: Dropping all that comes after the last -1 in solution_resp (and correspondingly in solution). Then counting the number of missing -1 in solution_resp.
  3. the number of correctly identified items, i.e., -1 in solution_resp that corresponds to -1 in solution
  4. the number of incorrectly identified items, i.e., -1 in solution_resp that does not correspond to -1 in solution (probably easy if i managed 3.)
  5. the number of -1 in solution_resp

I wondered whether it is necessary or helpful to take out the nested list in rows and create a new counting variable? Or is there an easier way for direct comparisons? Dplyr solutions are favored, but others are welcome too.

I have now created a simpler version of the data.frame:

ID <- c(2,2,2,2,5,5,5,5)
Rounds <- c(0,1,2,3,0,1,2,3)
solution <- as.character('[[-1,1,1,-1],[1,-1,1,-1]]')
resp <- as.character('[[-1,1,1,1],[1,1,-1,1]]')

dt <- data.frame(ID,Rounds,solution,resp)

Desired output: To simplify the example line is equal.

  1. number of elements that differ: 4
  2. number of left outs: 2
  3. correctly identified: 1
  4. incorrectly identified: 1
  5. marked: 2
0 Answers
Related