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

Description Usage Arguments Value See Also Examples

View source: R/split_by.R

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

1
2
3

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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")

Example output

  Ozone Solar.R Wind Temp Month Day Ozone_zscore
1    41     190  7.4   67     5   1    0.7822293
2    36     118  8.0   72     5   2    0.5572518
3    12     149 12.6   74     5   3   -0.5226399
4    18     313 11.5   62     5   4   -0.2526670
5    NA      NA 14.3   56     5   5           NA
6    28      NA 14.9   66     5   6    0.1972879
                                         .groups                variable
1         Transmission|Automatic|Engine|V-engine             (Intercept)
2         Transmission|Automatic|Engine|V-engine      `Gross horsepower`
3         Transmission|Automatic|Engine|V-engine `Displacement (cu.in.)`
4         Transmission|Automatic|Engine|V-engine                  Weight
5  Transmission|Automatic|Engine|Straight engine             (Intercept)
6  Transmission|Automatic|Engine|Straight engine      `Gross horsepower`
7  Transmission|Automatic|Engine|Straight engine `Displacement (cu.in.)`
8  Transmission|Automatic|Engine|Straight engine                  Weight
9            Transmission|Manual|Engine|V-engine             (Intercept)
10           Transmission|Manual|Engine|V-engine      `Gross horsepower`
11           Transmission|Manual|Engine|V-engine `Displacement (cu.in.)`
12           Transmission|Manual|Engine|V-engine                  Weight
13    Transmission|Manual|Engine|Straight engine             (Intercept)
14    Transmission|Manual|Engine|Straight engine      `Gross horsepower`
15    Transmission|Manual|Engine|Straight engine `Displacement (cu.in.)`
16    Transmission|Manual|Engine|Straight engine                  Weight
          Coef.        2.5 %      97.5 %
1  27.412226336  16.88039150 37.94406117
2  -0.022134087  -0.07692174  0.03265357
3   0.002893784  -0.03364582  0.03943339
4  -2.217155261  -5.56350823  1.12919771
5  34.735574392  12.83587564 56.63527314
6  -0.091480087  -0.21096157  0.02800140
7   0.001205846  -0.05445419  0.05686588
8  -1.521417007  -9.39932076  6.35648675
9  39.071818572  11.32215133 66.82148581
10 -0.001919940  -0.08034876  0.07650888
11 -0.010462370  -0.07243167  0.05150693
12 -5.885254399 -19.22668610  7.45617730
13 49.958434260  35.42191613 64.49495238
14  0.148546925  -0.13026125  0.42735510
15 -0.411071662  -0.87943241  0.05728909
16  1.655876968  -9.78455017 13.09630411

expss documentation built on Jan. 8, 2021, 5:38 p.m.