Manipulate data with dplyr

The dplyr package is based on a data manipulation ‘grammar’. This grammar provides a consistent set of ‘verbs’ that solve the most common data manipulation tasks. I illustrate five of these ‘verbs’: filter()arrange()select()mutate(), and summarise(). Please refer to the dplyr documentation for details.

First of all, install and load the dplyr package in R:

install.packages('dplyr')
library(dplyr)

Data

The functions are illustrated with a data set from Fox and Jacewicz (2009). The authors compare the spectral change of five vowels in Western North Carolina, Central Ohio, and Southern Wisconsin. The corpus consists of 1920 utterances by 48 female informants. The authors find variation in formant dynamics as a function of phonetic factors. They also find that, for each vowel and for each measure employed, dialect is a strong source of variation in vowel-inherent spectral change.

Load the data as follows:

vow.dur <- read.table("https://bit.ly/2Iw7kn7", header=TRUE, sep="\t")

Once loaded as a data frame, here is what the data look like:

filter rows with filter()

The filter() function subsets a data frame, retaining all rows that meet one or several conditions. You express the condition(s) by means of the following logical operators:

  • == (equal to), != (not equal to), > (greater than), >= (greater than or equal to), etc
  • & (and), | (or), ! (not), xor() (exclusive or)
  • is.na() (checks whether a value is NA)
  • etc.

filtering with one condition

Keep only the vowels that occur in a voiceless context:

filter(vow.dur, context == "voiceless")

The same can be achieved with the tidyverse syntax:

vow.dur %>% 
     filter(context == "voiceless")

Let us now keep only the vowels whose duration is greater than 187:

filter(vow.dur, Vow_dur_ms > 187)

And now, let us keep only the vowels whose duration is greater than the mean duration:

filter(vow.dur, Vow_dur_ms > mean(Vow_dur_ms, na.rm = TRUE))

filtering with multiple conditions

To filter with multiple conditions, separate each condition with &. The code below keeps only the vowels that occur in voiceless and consonantal contexts:

filter(vow.dur, context == "voiceless" & position == "Ccontext")

Keep only the vowels that occur in a voiceless context AND whose duration is greater than 187:

filter(vow.dur, context == "voiceless" & Vow_dur_ms > 187)

arrange rows with arrange()

With arrange(), you can order the rows of a data frame by the values of selected columns.

arrange(vow.dur, US_state) # order by US state
arrange(vow.dur, US_state, Vow_dur_ms) # order by US state and vowel duration
arrange(vow.dur, US_state, desc(Vow_dur_ms)) # order by US state and vowel duration in decreasing order

select columns with select()

select() accesses the variables (columns) in a data frame based on their names. Selection can be made with the following base-R logical operators:

  • : for selecting a range of consecutive variables
  • ! for taking the complement of a set of variables (e.g. !variable1 = all variables except variable1)
  • & and | for selecting the intersection or the union of two sets of variables
  • c() for combining selections

With tidyverse-specific operators, you can

  • match patterns in variable names:
    • starts_with(): the variable name starts with a prefix
    • ends_with(): the variable name ends with a suffix
    • contains(): the variable name contains a literal string
    • matches(): the variable name matches a regular expression
    • num_range(): the variable name matches a numerical range like x01x02x03.
  • select variables from a character vector:
    • all_of(): matches variable names in a character vector
    • any_of(): same as all_of(), except that no error is thrown for names that don’t exist.
  • select variables with a function:
    • where(): applies a function to all variables and selects those for which the function returns TRUE

Suppose we want to fetch US_state. With select(), we can do it it in several ways, including highly irrelevant ones.

vow.dur %>% select(starts_with("US"))
vow.dur %>% select(ends_with("te"))

The most obvious way consists in using the plain variable name, without quotes.

vow.dur %>% select(US_state)

Suppose we now want to fetch US_state and Vow_dur_ms. Both variable names have the underscore in common. Let us use this to select them.

vow.dur %>% select(contains("_"))

If you are familiar with regular expressions, write your regex as an argument of matches():

vow.dur %>% 
   select(matches("(\\w+_)+"))
vow.dur %>% select(matches("\\w+_\\w+_\\w+""))

add new variables/colums with mutate()

With mutate(), you can add new variables and preserve existing ones. A close equivalent, transmute() adds new variables but drops existing ones.

mutate() is often used with group_by() to calculate sums or means over grouped values.

vow.dur %>% 
   group_by(US_state) %>%
   mutate(mean_vow_dur = mean(Vow_dur_ms, na.rm = TRUE))

With transmute(), the variable Vow_dur_ms is dropped:

vow.dur %>%
   select(US_state, context, Vow_dur_ms) %>%
   group_by(US_state) %>%
   transmute(mean_vow_dur = mean(Vow_dur_ms, na.rm = TRUE))

rename variable names with rename()

rename() changes the names of individual variables using new_name = old_name syntax.

vow.dur %>%
   rename(vowel_duration = Vow_dur_ms)

make grouped summaries with summarise()

summarise() creates a new data frame based on a source data frame with one row per grouping variable.

Here is how to calculate the mean vowel duration overall:

vow.dur %>%
   summarise(mean = mean(Vow_dur_ms))

the mean vowel duration per US state:

vow.dur %>% 
   group_by(US_state) %>%
   summarise(mean = mean(Vow_dur_ms))

or the mean vowel duration per US state and context:

vow.dur %>%
   group_by(US_state, context) %>%
   summarise(mean = mean(Vow_dur_ms))

There are many functions other than mean()median()sd() (standard deviation), IQR() (interquartile range), min()max()quantile()n() (count), etc.

References

Fox, Robert Allen, and Ewa Jacewicz. 2009. “Cross-Dialectal Variation in Formant Dynamics of American English Vowels.” The Journal of the Acoustical Society of America 126 (5): 2603–18. https://doi.org/10.1121/1.3212921.

Cite this article as: Guillaume Desagulier, "Manipulate data with dplyr," in Around the word, 03/03/2021, https://corpling.hypotheses.org/3461.

Guillaume Desagulier

UMR 7114 MoDyCo — Université Paris 8, CNRS, Université Paris Nanterre, Institut Universitaire de France.

Vous aimerez aussi...

1 réponse

  1. 16/03/2021

    […] Read the complete article at: corpling.hypotheses.org […]

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.

Rechercher dans OpenEdition Search

Vous allez être redirigé vers OpenEdition Search