Add a new variable based on some condition and group rows based on the new variable

add_group_col(data, name, where, value, add = TRUE)

Arguments

data

a data frame

name

the name of the new variable to be created

where

the condition in which the new variable should have a value (NA otherwise)

value

an expression for the content of the new variable when the condition is true

add

when `add = TRUE` (default), the new variable will add to existing groups. Set to `FALSE` to override all existing groups (passed to `dplyr::group_by`)

Examples

lexicon_str <- '\\lx bureau \\sn 1 \\de desk \\sn 2 \\de office \\lx langue \\sn 1 \\de language \\sn 2 \\de tongue ' read_lexicon(file = lexicon_str, regex = "\\\\?([a-z]*)\\s?(.*)", into = c("code", "value")) %>% add_group_col(name = lx_group, where = code == "lx", value = paste0(line, ": ", value)) %>% add_group_col(name = sense_no, where = code == "sn", value = value)
#> # A tibble: 11 x 6 #> # Groups: lx_group, sense_no [6] #> line data code value lx_group sense_no #> <int> <chr> <chr> <chr> <chr> <chr> #> 1 1 "\\lx bureau" lx bureau 1: bureau NA #> 2 2 "\\sn 1" sn 1 1: bureau 1 #> 3 3 "\\de desk" de desk 1: bureau 1 #> 4 4 "\\sn 2" sn 2 1: bureau 2 #> 5 5 "\\de office" de office 1: bureau 2 #> 6 6 "" "" "" 1: bureau 2 #> 7 7 "\\lx langue" lx langue 7: langue NA #> 8 8 "\\sn 1" sn 1 7: langue 1 #> 9 9 "\\de language" de language 7: langue 1 #> 10 10 "\\sn 2" sn 2 7: langue 2 #> 11 11 "\\de tongue" de tongue 7: langue 2