split_by: Splits data.frame into list of data.frames that can be...

View source: R/split_by.R

split_byR Documentation

Splits data.frame into list of data.frames that can be analyzed separately

Description

Splits data.frame into list of data.frames that can be analyzed separately. These data.frames are sets of cases that have the same values for the specified split variables. Any missing values in split variables are dropped together with the corresponding values of data. split_off works with lists of data.frames or objects that can be coerced to data.frame and assumed to have compatible structure. Resulting rows will be sorted in order of the split variables.

Usage

split_by(data, ..., drop = TRUE)

split_off(data, groups = NULL, rownames = NULL)

Arguments

data

data.frame for split_by/list for split_off

...

unquoted variables names (see keep) by which data will be split into list.

drop

should we drop combination of levels with zero observation? TRUE by default.

groups

character If it is not NULL then we add list names as variable to result of split_off with the name specified by groups. If it is TRUE then name will be .groups.

rownames

character If it is not NULL then we add data.frames rownames as variable to result of split_off with the name specified by rownames. If it is TRUE then name will be .rownames.

Value

split_by returns list of data.frames/split_off returns data.frame

See Also

split

Examples


# usage of 'groups', 'rownames'
data(mtcars)
# add labels to dataset
mtcars %>% 
    apply_labels(mpg = "Miles/(US) gallon",
                 disp = "Displacement (cu.in.)",
                 wt = "Weight",
                 hp = "Gross horsepower",
                 vs = "Engine",
                 vs = num_lab(" 
                                   0 V-engine
                                   1 Straight engine
                                   "),
                 
                 am = "Transmission",
                 am = num_lab(" 
                                   0 Automatic
                                   1 Manual
                                   ")
    ) %>% 
    split_by(am, vs) %>% 
    to_list({
        res = lm(mpg ~ hp + disp + wt, data = .x)
        cbind(Coef. = coef(res), confint(res))
    }) %>% 
    split_off(groups = TRUE, rownames = "variable")

expss documentation built on Nov. 10, 2022, 6:14 p.m.