As mom objects are tibbles, they probably look more than familiar to you. And as they are tibbles, the entire tidyverse comes for free.

This vignette illustrates how to use the most common tidyverse functions for the most common data manipulation tasks in MomX. Things shown here will “work” on any data.frame and can be found easily from the tidyverse homepage: https://www.tidyverse.org/.

The pipe operators

First thing first: if you’re not familiar with these %>% everywhere in my code, examples, etc. let me just show you some “base R”:

bot_c    <- coo_center(bot)
bot_cs   <- coo_scale(bot_c)
bot_csa  <- coo_align(bot_cs)
bot_csa1 <- coo_slidedirection(bot_csa)
bot_eft  <- efourier(bot_csa1)

And now some “piped R”:

bot %>%
  coo_center %>%
  coo_scale %>%
  coo_align %>%
  coo_slidedirection("right") %>%
  efourier()

Unless you’re an ultra-rthodox or something1, you will likely vote for the second one. We should not reduce the pipe-operator to a matter of taste, it is also:

  • safer (it reduces typos)
  • easier and more comfy to read (thus to understand and debug)
  • easier and faster to write (use RStudio shortcut!)
  • a guarantee that functions consistently use data as first argument

Read the Introduction to magrittr

Everything is a tibble (thus a data.frame)

To be perfectly clear, <mom> are tibbles that are themselves data.frame that are themselves list:

tibble::is_tibble(bot)
is.data.frame(bot)
is.list(bot)

In other words you do not need tidyverse verbs (they are just really really desirable) to handles <mom> objects, ie all of this works:

# a mom is a data.frame
bot[1, ]

# a coo column is a list
bot$coo[[5]]

lapply(bot$coo[1:5], coo_center)

# because of lists nature of data.frames
lapply(bot, summary)

dplyr: common data manipulation

If you’re not familiar with dplyr, the Introduction to dplyr will definitely change your life.

By our side, we will play with olive stones:

library(tidyverse)
library(Momocs2)
olea

Filtering

You can filter individuals based on logical conditions:

olea %>% filter(view == "VL")

And you can group conditions:

olea %>% filter(view == "VL", var != "Aglan", ind %in% c("O10", "O11"))

Select rows

Except in “inspection” case, it’s generally not perfectly safe to subset positionnally but that’s the job of slice

olea %>% slice(1:5)

olea %>% slice(-(1:5))

  1. And if you’re a post-modeRnist you will appreciate the nakedpipe too↩︎