Apply a function over rows of a data.frame
-like object and concatenate the results back to tibble
or data.frame
.
Source: R/utils.R
lapply_rows.Rd
Inside a FUN
, a row of df
will be available as a list or scdrake_list()
(default).
The FUN
must return a named list for a proper concatenation of the results.
The named list can also contain elements of a length other than one, which are then wrapped in list()
.
Arguments
- df
A
data.frame
-like object.- as_scdrake_list
A logical scalar: if
TRUE
, each row-list inFUN
will be converted toscdrake_list()
.- return_tibble
If
TRUE
, atibble
with concatenated results is returned. Otherwise results are coerced todata.frame
and original rownames are set.- FUN
A function to apply over
df
rows.- ...
Additional arguments passed to
FUN
.
Examples
library(magrittr)
fn <- function(row_list) {
row_list$cyl_2 <- row_list$cyl**2
row_list$colors <- c("red", "green", "blue")
row_list$sublist <- mtcars[1:5, 1:5]
return(row_list)
}
df <- lapply_rows(mtcars, FUN = fn)
head(df)
#> # A tibble: 6 × 14
#> mpg cyl disp hp drat wt qsec vs am gear carb cyl_2 colors
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <list>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4 36 <chr>
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4 36 <chr>
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 16 <chr>
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 36 <chr>
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 64 <chr>
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1 36 <chr>
#> # … with 1 more variable: sublist <list>
df2 <- lapply_rows(mtcars, return_tibble = FALSE, FUN = fn)
head(df2)
#> mpg cyl disp hp drat wt qsec vs am gear carb cyl_2
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 36
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 36
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 16
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 36
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 64
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 36
#> colors
#> Mazda RX4 red, green, blue
#> Mazda RX4 Wag red, green, blue
#> Datsun 710 red, green, blue
#> Hornet 4 Drive red, green, blue
#> Hornet Sportabout red, green, blue
#> Valiant red, green, blue
#> sublist
#> Mazda RX4 21.00, 21.00, 22.80, 21.40, 18.70, 6.00, 6.00, 4.00, 6.00, 8.00, 160.00, 160.00, 108.00, 258.00, 360.00, 110.00, 110.00, 93.00, 110.00, 175.00, 3.90, 3.90, 3.85, 3.08, 3.15
#> Mazda RX4 Wag 21.00, 21.00, 22.80, 21.40, 18.70, 6.00, 6.00, 4.00, 6.00, 8.00, 160.00, 160.00, 108.00, 258.00, 360.00, 110.00, 110.00, 93.00, 110.00, 175.00, 3.90, 3.90, 3.85, 3.08, 3.15
#> Datsun 710 21.00, 21.00, 22.80, 21.40, 18.70, 6.00, 6.00, 4.00, 6.00, 8.00, 160.00, 160.00, 108.00, 258.00, 360.00, 110.00, 110.00, 93.00, 110.00, 175.00, 3.90, 3.90, 3.85, 3.08, 3.15
#> Hornet 4 Drive 21.00, 21.00, 22.80, 21.40, 18.70, 6.00, 6.00, 4.00, 6.00, 8.00, 160.00, 160.00, 108.00, 258.00, 360.00, 110.00, 110.00, 93.00, 110.00, 175.00, 3.90, 3.90, 3.85, 3.08, 3.15
#> Hornet Sportabout 21.00, 21.00, 22.80, 21.40, 18.70, 6.00, 6.00, 4.00, 6.00, 8.00, 160.00, 160.00, 108.00, 258.00, 360.00, 110.00, 110.00, 93.00, 110.00, 175.00, 3.90, 3.90, 3.85, 3.08, 3.15
#> Valiant 21.00, 21.00, 22.80, 21.40, 18.70, 6.00, 6.00, 4.00, 6.00, 8.00, 160.00, 160.00, 108.00, 258.00, 360.00, 110.00, 110.00, 93.00, 110.00, 175.00, 3.90, 3.90, 3.85, 3.08, 3.15