If your data frame names are inconsistent, you can still use the ls()
entry if you don't have a vector or list of names. Just remove anything else in the environment.
purrr::map()
isn't problematic—it works as intended. It's difficult for me because I've never developed a mental model that makes it practical to use. I might do better if I didn't find myself trying to use it in {tidyverse}
mode. For me, trying to an all-tidy script swamps my syntax cache and every other function involves parsing the signature. Although base style makes more use of delimiters, which brings back bad memories of school algebra equations, I find it ultimately simpler and more intuitive.
On the other hand, everyone's R
experience differs. I started using it in 2007, along with Python and SQL, after a long time away from my brief experience with FORTRAN in 1965 and C in the 80s. At first, I was happy with the tidyverse tools, in part due to the halo effect of {ggplot2}
, which is really more of a domain specific language rather than specifically tidy. Ten years in I started working with data that asked for more in the way of preprocessing and I began to see advantages in using {base}
tools.
This came about, I think, by realizing that putting numeric data in a vector or matrix makes subsetting and numeric operations far easier. For example, compare the datasets iris
and iris3
, a data frame and a matrix, respectively. If I wanted to double each of the numeric values, what I'd usually do in dplyr
(don't claim it's the best way to do in tidy, just the easiest conceptually for me) compared to how I'd do it with linear algebra
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
data(iris)
doubled <- iris |>
mutate(
Sepal.Length = Sepal.Length * 2,
Sepal.Width = Sepal.Width * 2,
Petal.Length = Petal.Length * 2,
Petal.Width = Petal.Width * 2
)
head(doubled)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 10.2 7.0 2.8 0.4 setosa
#> 2 9.8 6.0 2.8 0.4 setosa
#> 3 9.4 6.4 2.6 0.4 setosa
#> 4 9.2 6.2 3.0 0.4 setosa
#> 5 10.0 7.2 2.8 0.4 setosa
#> 6 10.8 7.8 3.4 0.8 setosa
data(iris3)
doubled2 <- iris3*2
head(doubled2)
#> , , Setosa
#>
#> Sepal L. Sepal W. Petal L. Petal W.
#> [1,] 10.2 7.0 2.8 0.4
#> [2,] 9.8 6.0 2.8 0.4
#> [3,] 9.4 6.4 2.6 0.4
#> [4,] 9.2 6.2 3.0 0.4
#> [5,] 10.0 7.2 2.8 0.4
#> [6,] 10.8 7.8 3.4 0.8
#>
#> , , Versicolor
#>
#> Sepal L. Sepal W. Petal L. Petal W.
#> [1,] 14.0 6.4 9.4 2.8
#> [2,] 12.8 6.4 9.0 3.0
#> [3,] 13.8 6.2 9.8 3.0
#> [4,] 11.0 4.6 8.0 2.6
#> [5,] 13.0 5.6 9.2 3.0
#> [6,] 11.4 5.6 9.0 2.6
#>
#> , , Virginica
#>
#> Sepal L. Sepal W. Petal L. Petal W.
#> [1,] 12.6 6.6 12.0 5.0
#> [2,] 11.6 5.4 10.2 3.8
#> [3,] 14.2 6.0 11.8 4.2
#> [4,] 12.6 5.8 11.2 3.6
#> [5,] 13.0 6.0 11.6 4.4
#> [6,] 15.2 6.0 13.2 4.2
Created on 2023-09-12 with reprex v2.0.2