Alex Fort Alex Fort - 1 month ago 15
R Question

Counting and reorganizing data in a R matrix

I have the following matrix, with the results of operations A,B,C,D,E

Name result freq
A ok 3
A nok 4
B ok 5
B nok 6
C ok 7
D nok 8
E ok 8
E nok 9


I need to create a new table having the following format:

Name freok Frenok
A 3 4
B 5 6
C 7 0
D 0 8
E 8 9


counting for each operation the good and bad results, and if the ok or notok for an operation is missing in the first matrix, then i need to put a Zero in the processed matrix.

I need to process a matrix of about 16 millions of rows

What's the fastest solution?

Answer

Using tidyr (part of the tidyverse in R and working with dplyr)

# Just for importing your dataset
df <- readr::read_delim("Name result freq
A ok 3
A nok 4
B ok 5
B nok 6
C ok 7
D nok 8
E ok 8
E nok 9", delim = " ")


library(tidyr)
spread(df, result, freq, fill = 0)
#> # A tibble: 5 x 3
#>    Name   nok    ok
#> * <chr> <dbl> <dbl>
#> 1     A     4     3
#> 2     B     6     5
#> 3     C     0     7
#> 4     D     8     0
#> 5     E     9     8
Comments