Skip to contents

Code frequencies separate by a variable

Usage

code_freq_by(x, by, codes = ".*", returnTidyDf = FALSE)

Arguments

x

The object with parsed sources.

by

The variables on which to split when computing code frequencies.

codes

A regular expression specifying the codes fo which to compute the code frequencies.

returnTidyDf

When TRUE, return a tidy data frame with the counts in one column, the by variable in another, and the code for which the counts are provided in another column. Otherwise, return a 'wide' data frame with the by variable in one column, the codes in the other columns, and the counts in the cells.

Value

A data frame with the code frequencies

Examples

### Get path to example source
examplePath <-
  system.file("extdata", package="rock");

### Get a path to one example file
exampleFile <-
  file.path(examplePath, "example-1.rock");

### Load example source
loadedExample <- rock::parse_source(exampleFile);

### Show code frequencies
code_freq_by(loadedExample, "nestingLevel");
#>   nestingLevel childCode1 childCode2 childCode3 childCode4 childCode5
#> 1            0          1          0          0          0          1
#> 2            1          1          1          0          0          0
#> 3            2          0          0          1          1          0
#> 4            3          0          0          0          0          0
#>   grandchildCode1 grandchildCode2 grandchildCode3 grandchildCode4
#> 1               1               2               1               1
#> 2               0               1               0               0
#> 3               0               0               0               0
#> 4               0               0               0               0
#>   grandchildCode5 grandchildCode6 grandchildCode7 someOtherCode
#> 1               1               1               1             0
#> 2               0               0               0             0
#> 3               0               0               0             1
#> 4               0               0               0             0