Learn R Programming

expss (version 0.10.7)

split_by: 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, compute, calculate, do_repeat, where

Examples

Run this code
# NOT RUN {
# example from base R 'split'
data(airquality)
airquality2 = airquality %>% 
    split_by(Month) %>% 
    compute({
        Ozone_zscore = scale(Ozone)
    }) %>% 
    split_off() 
    
head(airquality2)

# 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) %>% 
    use_labels({
        res = lm(mpg ~ hp + disp + wt)
        cbind(Coef. = coef(res), confint(res))
    }) %>% 
    split_off(groups = TRUE, rownames = "variable")
# }

Run the code above in your browser using DataLab