Great. Please mark the solution for the benefit of those to follow.
Regarding the %>%
operator, and the tidyverse
it does take some getting use to. Here's a pattern that might provide some insight. (I've used intermediate variables, but often it's
my_df <- my_df %>% ...
suppressPackageStartupMessages(library(dplyr))
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
sepals <- iris %>% select(Sepal.Length, Sepal.Width, Species)
petals <- iris %>% select(Petal.Length, Petal.Width, Species)
sepals1 <- sepals %>% mutate(Sepal.Length = log(Sepal.Length))
petals1 <- petals %>% mutate(Petal.Width = Petal.Width^2)
iris1 <- inner_join(sepals1, petals1, by="Species") %>% rename(Flower = Species) %>% select(Flower, everything())
head(iris1)
#> Flower Sepal.Length Sepal.Width Petal.Length Petal.Width
#> 1 setosa 1.629241 3.5 1.4 0.04
#> 2 setosa 1.629241 3.5 1.4 0.04
#> 3 setosa 1.629241 3.5 1.3 0.04
#> 4 setosa 1.629241 3.5 1.5 0.04
#> 5 setosa 1.629241 3.5 1.4 0.04
#> 6 setosa 1.629241 3.5 1.7 0.16
Created on 2020-01-13 by the reprex package (v0.3.0)