Modern object classes and methods for handling data.frame like structures
are provided by the dplyr and data.table packages. The following gives a
short introduction to the usage and functionalities of the dplyr package.
More detailed tutorials on this topic can be found here:
- dplyr: A Grammar of Data Manipulation
- Introduction to
dplyr - Tutorial on
dplyr - Cheatsheet for Joins from Jenny Bryan
- Tibbles
- Intro to
data.tablepackage - Big data with
dplyranddata.table - Fast lookups with
dplyranddata.table
Installation
The dplyr environment has evolved into an ecosystem of packages. To simplify
package management, one can install and load the entire collection via the
tidyverse package. For more details on tidyverse see
here.
install.packages("tidyverse")
Construct a tibble (tibble)
library(tidyverse)
as_tibble(iris) # coerce data.frame to tibble tbl
## # A tibble: 150 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <fct>
## 1 5.10 3.50 1.40 0.200 setosa
## 2 4.90 3.00 1.40 0.200 setosa
## 3 4.70 3.20 1.30 0.200 setosa
## 4 4.60 3.10 1.50 0.200 setosa
## 5 5.00 3.60 1.40 0.200 setosa
## 6 5.40 3.90 1.70 0.400 setosa
## 7 4.60 3.40 1.40 0.300 setosa
## 8 5.00 3.40 1.50 0.200 setosa
## 9 4.40 2.90 1.40 0.200 setosa
## 10 4.90 3.10 1.50 0.100 setosa
## # ... with 140 more rows
Alternative functions producing the same result include as_data_frame and tbl_df:
as_data_frame(iris)
tbl_df(iris)
Reading and writing tabular files
While the base R read/write utilities can be used for data.frames, best time
performance with the least amount of typing is achieved with the export/import
functions from the readr package. For very large files the fread function from
the data.table package achieves the best time performance.
Import with readr
Import functions provided by readr include:
read_csv(): comma separated (CSV) filesread_tsv(): tab separated filesread_delim(): general delimited filesread_fwf(): fixed width filesread_table(): tabular files where colums are separated by white-space.read_log(): web log files
Create a sample tab delimited file for import
write_tsv(iris, "iris.txt") # Creates sample file
Import with read_tsv
iris_df <- read_tsv("iris.txt") # Import with read_tbv from readr package
iris_df
## # A tibble: 150 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 5.10 3.50 1.40 0.200 setosa
## 2 4.90 3.00 1.40 0.200 setosa
## 3 4.70 3.20 1.30 0.200 setosa
## 4 4.60 3.10 1.50 0.200 setosa
## 5 5.00 3.60 1.40 0.200 setosa
## 6 5.40 3.90 1.70 0.400 setosa
## 7 4.60 3.40 1.40 0.300 setosa
## 8 5.00 3.40 1.50 0.200 setosa
## 9 4.40 2.90 1.40 0.200 setosa
## 10 4.90 3.10 1.50 0.100 setosa
## # ... with 140 more rows
To import Google Sheets directly into R, see here.
Fast table import with fread
The fread function from the data.table package provides the best time performance for reading large
tabular files into R.
library(data.table)
iris_df <- as_data_frame(fread("iris.txt")) # Import with fread and conversion to tibble
iris_df
## # A tibble: 150 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 5.10 3.50 1.40 0.200 setosa
## 2 4.90 3.00 1.40 0.200 setosa
## 3 4.70 3.20 1.30 0.200 setosa
## 4 4.60 3.10 1.50 0.200 setosa
## 5 5.00 3.60 1.40 0.200 setosa
## 6 5.40 3.90 1.70 0.400 setosa
## 7 4.60 3.40 1.40 0.300 setosa
## 8 5.00 3.40 1.50 0.200 setosa
## 9 4.40 2.90 1.40 0.200 setosa
## 10 4.90 3.10 1.50 0.100 setosa
## # ... with 140 more rows
Note: to ignore lines starting with comment signs, one can pass on to fread a shell
command for preprocessing the file. The following example illustrates this option.
fread("grep -v '^#' iris.txt")
Export with readr
Export function provided by readr inlcude
write_delim(): general delimited fileswrite_csv(): comma separated (CSV) fileswrite_excel_csv(): excel style CSV fileswrite_tsv(): tab separated files
For instance, the write_tsv function writes a data.frame or tibble to a tab delimited file with much nicer
default settings than the base R write.table function.
write_tsv(iris_df, "iris.txt")
Column and row binds
The equivalents to base R’s rbind and cbind are bind_rows and bind_cols, respectively.
bind_cols(iris_df, iris_df)
## # A tibble: 150 x 10
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length1 Sepal.Width1
## <dbl> <dbl> <dbl> <dbl> <chr> <dbl> <dbl>
## 1 5.10 3.50 1.40 0.200 setosa 5.10 3.50
## 2 4.90 3.00 1.40 0.200 setosa 4.90 3.00
## 3 4.70 3.20 1.30 0.200 setosa 4.70 3.20
## 4 4.60 3.10 1.50 0.200 setosa 4.60 3.10
## 5 5.00 3.60 1.40 0.200 setosa 5.00 3.60
## 6 5.40 3.90 1.70 0.400 setosa 5.40 3.90
## 7 4.60 3.40 1.40 0.300 setosa 4.60 3.40
## 8 5.00 3.40 1.50 0.200 setosa 5.00 3.40
## 9 4.40 2.90 1.40 0.200 setosa 4.40 2.90
## 10 4.90 3.10 1.50 0.100 setosa 4.90 3.10
## # ... with 140 more rows, and 3 more variables: Petal.Length1 <dbl>, Petal.Width1 <dbl>,
## # Species1 <chr>
bind_rows(iris_df, iris_df)
## # A tibble: 300 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 5.10 3.50 1.40 0.200 setosa
## 2 4.90 3.00 1.40 0.200 setosa
## 3 4.70 3.20 1.30 0.200 setosa
## 4 4.60 3.10 1.50 0.200 setosa
## 5 5.00 3.60 1.40 0.200 setosa
## 6 5.40 3.90 1.70 0.400 setosa
## 7 4.60 3.40 1.40 0.300 setosa
## 8 5.00 3.40 1.50 0.200 setosa
## 9 4.40 2.90 1.40 0.200 setosa
## 10 4.90 3.10 1.50 0.100 setosa
## # ... with 290 more rows
Extract column as vector
The subsetting operators [[ and $can be used to extract from a tibble single columns as vector.
iris_df[[5]][1:12]
## [1] "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa"
## [11] "setosa" "setosa"
iris_df$Species[1:12]
## [1] "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa" "setosa"
## [11] "setosa" "setosa"
Important dplyr functions
filter()andslice()arrange()select()andrename()distinct()mutate()andtransmute()summarise()sample_n()andsample_frac()
Slice and filter functions
Filter function
filter(iris_df, Sepal.Length > 7.5, Species=="virginica")
## # A tibble: 6 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 7.60 3.00 6.60 2.10 virginica
## 2 7.70 3.80 6.70 2.20 virginica
## 3 7.70 2.60 6.90 2.30 virginica
## 4 7.70 2.80 6.70 2.00 virginica
## 5 7.90 3.80 6.40 2.00 virginica
## 6 7.70 3.00 6.10 2.30 virginica
Base R code equivalent
iris_df[iris_df[, "Sepal.Length"] > 7.5 & iris_df[, "Species"]=="virginica", ]
## # A tibble: 6 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 7.60 3.00 6.60 2.10 virginica
## 2 7.70 3.80 6.70 2.20 virginica
## 3 7.70 2.60 6.90 2.30 virginica
## 4 7.70 2.80 6.70 2.00 virginica
## 5 7.90 3.80 6.40 2.00 virginica
## 6 7.70 3.00 6.10 2.30 virginica
Including boolean operators
filter(iris_df, Sepal.Length > 7.5 | Sepal.Length < 5.5, Species=="virginica")
## # A tibble: 7 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 7.60 3.00 6.60 2.10 virginica
## 2 4.90 2.50 4.50 1.70 virginica
## 3 7.70 3.80 6.70 2.20 virginica
## 4 7.70 2.60 6.90 2.30 virginica
## 5 7.70 2.80 6.70 2.00 virginica
## 6 7.90 3.80 6.40 2.00 virginica
## 7 7.70 3.00 6.10 2.30 virginica
Subset rows by position
dplyr approach
slice(iris_df, 1:2)
## # A tibble: 2 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 5.10 3.50 1.40 0.200 setosa
## 2 4.90 3.00 1.40 0.200 setosa
Base R code equivalent
iris_df[1:2,]
## # A tibble: 2 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 5.10 3.50 1.40 0.200 setosa
## 2 4.90 3.00 1.40 0.200 setosa
Subset rows by names
Since tibbles do not contain row names, row wise subsetting via the [,] operator cannot be used.
However, the corresponding behavior can be achieved by passing to select a row position index
obtained by basic R intersect utilities such as match.
Create a suitable test tibble
df1 <- bind_cols(data_frame(ids1=paste0("g", 1:10)), as_data_frame(matrix(1:40, 10, 4, dimnames=list(1:10, paste0("CA", 1:4)))))
df1
## # A tibble: 10 x 5
## ids1 CA1 CA2 CA3 CA4
## <chr> <int> <int> <int> <int>
## 1 g1 1 11 21 31
## 2 g2 2 12 22 32
## 3 g3 3 13 23 33
## 4 g4 4 14 24 34
## 5 g5 5 15 25 35
## 6 g6 6 16 26 36
## 7 g7 7 17 27 37
## 8 g8 8 18 28 38
## 9 g9 9 19 29 39
## 10 g10 10 20 30 40
dplyr approach
slice(df1, match(c("g10", "g4", "g4"), df1$ids1))
## # A tibble: 3 x 5
## ids1 CA1 CA2 CA3 CA4
## <chr> <int> <int> <int> <int>
## 1 g10 10 20 30 40
## 2 g4 4 14 24 34
## 3 g4 4 14 24 34
Base R equivalent
df1_old <- as.data.frame(df1)
rownames(df1_old) <- df1_old[,1]
df1_old[c("g10", "g4", "g4"),]
## ids1 CA1 CA2 CA3 CA4
## g10 g10 10 20 30 40
## g4 g4 4 14 24 34
## g4.1 g4 4 14 24 34
Sorting with arrange
Row-wise ordering based on specific columns
dplyr approach
arrange(iris_df, Species, Sepal.Length, Sepal.Width)
## # A tibble: 150 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 4.30 3.00 1.10 0.100 setosa
## 2 4.40 2.90 1.40 0.200 setosa
## 3 4.40 3.00 1.30 0.200 setosa
## 4 4.40 3.20 1.30 0.200 setosa
## 5 4.50 2.30 1.30 0.300 setosa
## 6 4.60 3.10 1.50 0.200 setosa
## 7 4.60 3.20 1.40 0.200 setosa
## 8 4.60 3.40 1.40 0.300 setosa
## 9 4.60 3.60 1.00 0.200 setosa
## 10 4.70 3.20 1.30 0.200 setosa
## # ... with 140 more rows
For ordering descendingly use desc() function
arrange(iris_df, desc(Species), Sepal.Length, Sepal.Width)
## # A tibble: 150 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 4.90 2.50 4.50 1.70 virginica
## 2 5.60 2.80 4.90 2.00 virginica
## 3 5.70 2.50 5.00 2.00 virginica
## 4 5.80 2.70 5.10 1.90 virginica
## 5 5.80 2.70 5.10 1.90 virginica
## 6 5.80 2.80 5.10 2.40 virginica
## 7 5.90 3.00 5.10 1.80 virginica
## 8 6.00 2.20 5.00 1.50 virginica
## 9 6.00 3.00 4.80 1.80 virginica
## 10 6.10 2.60 5.60 1.40 virginica
## # ... with 140 more rows
Base R code equivalent
iris_df[order(iris_df$Species, iris_df$Sepal.Length, iris_df$Sepal.Width), ]
## # A tibble: 150 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 4.30 3.00 1.10 0.100 setosa
## 2 4.40 2.90 1.40 0.200 setosa
## 3 4.40 3.00 1.30 0.200 setosa
## 4 4.40 3.20 1.30 0.200 setosa
## 5 4.50 2.30 1.30 0.300 setosa
## 6 4.60 3.10 1.50 0.200 setosa
## 7 4.60 3.20 1.40 0.200 setosa
## 8 4.60 3.40 1.40 0.300 setosa
## 9 4.60 3.60 1.00 0.200 setosa
## 10 4.70 3.20 1.30 0.200 setosa
## # ... with 140 more rows
iris_df[order(iris_df$Species, decreasing=TRUE), ]
## # A tibble: 150 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 6.30 3.30 6.00 2.50 virginica
## 2 5.80 2.70 5.10 1.90 virginica
## 3 7.10 3.00 5.90 2.10 virginica
## 4 6.30 2.90 5.60 1.80 virginica
## 5 6.50 3.00 5.80 2.20 virginica
## 6 7.60 3.00 6.60 2.10 virginica
## 7 4.90 2.50 4.50 1.70 virginica
## 8 7.30 2.90 6.30 1.80 virginica
## 9 6.70 2.50 5.80 1.80 virginica
## 10 7.20 3.60 6.10 2.50 virginica
## # ... with 140 more rows
Select columns with select
Select specific columns
select(iris_df, Species, Petal.Length, Sepal.Length)
## # A tibble: 150 x 3
## Species Petal.Length Sepal.Length
## <chr> <dbl> <dbl>
## 1 setosa 1.40 5.10
## 2 setosa 1.40 4.90
## 3 setosa 1.30 4.70
## 4 setosa 1.50 4.60
## 5 setosa 1.40 5.00
## 6 setosa 1.70 5.40
## 7 setosa 1.40 4.60
## 8 setosa 1.50 5.00
## 9 setosa 1.40 4.40
## 10 setosa 1.50 4.90
## # ... with 140 more rows
Select range of columns by name
select(iris_df, Sepal.Length : Petal.Width)
## # A tibble: 150 x 4
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## <dbl> <dbl> <dbl> <dbl>
## 1 5.10 3.50 1.40 0.200
## 2 4.90 3.00 1.40 0.200
## 3 4.70 3.20 1.30 0.200
## 4 4.60 3.10 1.50 0.200
## 5 5.00 3.60 1.40 0.200
## 6 5.40 3.90 1.70 0.400
## 7 4.60 3.40 1.40 0.300
## 8 5.00 3.40 1.50 0.200
## 9 4.40 2.90 1.40 0.200
## 10 4.90 3.10 1.50 0.100
## # ... with 140 more rows
Drop specific columns (here range)
select(iris_df, -(Sepal.Length : Petal.Width))
## # A tibble: 150 x 1
## Species
## <chr>
## 1 setosa
## 2 setosa
## 3 setosa
## 4 setosa
## 5 setosa
## 6 setosa
## 7 setosa
## 8 setosa
## 9 setosa
## 10 setosa
## # ... with 140 more rows
Renaming columns with rename
dplyr approach
rename(iris_df, new_col_name = Species)
## # A tibble: 150 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width new_col_name
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 5.10 3.50 1.40 0.200 setosa
## 2 4.90 3.00 1.40 0.200 setosa
## 3 4.70 3.20 1.30 0.200 setosa
## 4 4.60 3.10 1.50 0.200 setosa
## 5 5.00 3.60 1.40 0.200 setosa
## 6 5.40 3.90 1.70 0.400 setosa
## 7 4.60 3.40 1.40 0.300 setosa
## 8 5.00 3.40 1.50 0.200 setosa
## 9 4.40 2.90 1.40 0.200 setosa
## 10 4.90 3.10 1.50 0.100 setosa
## # ... with 140 more rows
Base R code approach
colnames(iris_df)[colnames(iris_df)=="Species"] <- "new_col_names"
Obtain unique rows with distinct
dplyr approach
distinct(iris_df, Species, .keep_all=TRUE)
## # A tibble: 3 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 5.10 3.50 1.40 0.200 setosa
## 2 7.00 3.20 4.70 1.40 versicolor
## 3 6.30 3.30 6.00 2.50 virginica
Base R code approach
iris_df[!duplicated(iris_df$Species),]
## # A tibble: 3 x 5
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## <dbl> <dbl> <dbl> <dbl> <chr>
## 1 5.10 3.50 1.40 0.200 setosa
## 2 7.00 3.20 4.70 1.40 versicolor
## 3 6.30 3.30 6.00 2.50 virginica
Add columns
mutate
The mutate function allows to append columns to existing ones.
mutate(iris_df, Ratio = Sepal.Length / Sepal.Width, Sum = Sepal.Length + Sepal.Width)
## # A tibble: 150 x 7
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species Ratio Sum
## <dbl> <dbl> <dbl> <dbl> <chr> <dbl> <dbl>
## 1 5.10 3.50 1.40 0.200 setosa 1.46 8.60
## 2 4.90 3.00 1.40 0.200 setosa 1.63 7.90
## 3 4.70 3.20 1.30 0.200 setosa 1.47 7.90
## 4 4.60 3.10 1.50 0.200 setosa 1.48 7.70
## 5 5.00 3.60 1.40 0.200 setosa 1.39 8.60
## 6 5.40 3.90 1.70 0.400 setosa 1.38 9.30
## 7 4.60 3.40 1.40 0.300 setosa 1.35 8.00
## 8 5.00 3.40 1.50 0.200 setosa 1.47 8.40
## 9 4.40 2.90 1.40 0.200 setosa 1.52 7.30
## 10 4.90 3.10 1.50 0.100 setosa 1.58 8.00
## # ... with 140 more rows
transmute
The transmute function does the same as mutate but drops existing columns
transmute(iris_df, Ratio = Sepal.Length / Sepal.Width, Sum = Sepal.Length + Sepal.Width)
## # A tibble: 150 x 2
## Ratio Sum
## <dbl> <dbl>
## 1 1.46 8.60
## 2 1.63 7.90
## 3 1.47 7.90
## 4 1.48 7.70
## 5 1.39 8.60
## 6 1.38 9.30
## 7 1.35 8.00
## 8 1.47 8.40
## 9 1.52 7.30
## 10 1.58 8.00
## # ... with 140 more rows
bind_cols
The bind_cols function is the equivalent of cbind in base R. To add rows, use the corresponding
bind_rows function.
bind_cols(iris_df, iris_df)
## # A tibble: 150 x 10
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length1 Sepal.Width1
## <dbl> <dbl> <dbl> <dbl> <chr> <dbl> <dbl>
## 1 5.10 3.50 1.40 0.200 setosa 5.10 3.50
## 2 4.90 3.00 1.40 0.200 setosa 4.90 3.00
## 3 4.70 3.20 1.30 0.200 setosa 4.70 3.20
## 4 4.60 3.10 1.50 0.200 setosa 4.60 3.10
## 5 5.00 3.60 1.40 0.200 setosa 5.00 3.60
## 6 5.40 3.90 1.70 0.400 setosa 5.40 3.90
## 7 4.60 3.40 1.40 0.300 setosa 4.60 3.40
## 8 5.00 3.40 1.50 0.200 setosa 5.00 3.40
## 9 4.40 2.90 1.40 0.200 setosa 4.40 2.90
## 10 4.90 3.10 1.50 0.100 setosa 4.90 3.10
## # ... with 140 more rows, and 3 more variables: Petal.Length1 <dbl>, Petal.Width1 <dbl>,
## # Species1 <chr>
Summarize data
Summary calculation on single column
summarize(iris_df, mean(Petal.Length))
## # A tibble: 1 x 1
## `mean(Petal.Length)`
## <dbl>
## 1 3.76
Summary calculation on many columns
summarize_all(iris_df[,1:4], mean)
## # A tibble: 1 x 4
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## <dbl> <dbl> <dbl> <dbl>
## 1 5.84 3.06 3.76 1.20
Summarize by grouping column
summarize(group_by(iris_df, Species), mean(Petal.Length))
## # A tibble: 3 x 2
## Species `mean(Petal.Length)`
## <chr> <dbl>
## 1 setosa 1.46
## 2 versicolor 4.26
## 3 virginica 5.55
Aggregate summaries
summarize_all(group_by(iris_df, Species), mean)
## # A tibble: 3 x 5
## Species Sepal.Length Sepal.Width Petal.Length Petal.Width
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 setosa 5.01 3.43 1.46 0.246
## 2 versicolor 5.94 2.77 4.26 1.33
## 3 virginica 6.59 2.97 5.55 2.03
Note: group_by does the looping for the user similar to aggregate or tapply.
Merging tibbles
The dplyr package provides several join functions for merging tibbles by a common key column
similar to the merge function in base R. These *_join functions include:
inner_join(): returns join only for rows matching among bothtibblesfull_join(): returns join for all (matching and non-matching) rows of twotibblesleft_join(): returns join for all rows in firsttibbleright_join(): returns join for all rows in secondtibbleanti_join(): returns for firsttibbleonly those rows that have no match in the second one
Sample tibbles to illustrate *.join functions.
df1 <- bind_cols(data_frame(ids1=paste0("g", 1:10)), as_data_frame(matrix(1:40, 10, 4, dimnames=list(1:10, paste0("CA", 1:4)))))
df1
## # A tibble: 10 x 5
## ids1 CA1 CA2 CA3 CA4
## <chr> <int> <int> <int> <int>
## 1 g1 1 11 21 31
## 2 g2 2 12 22 32
## 3 g3 3 13 23 33
## 4 g4 4 14 24 34
## 5 g5 5 15 25 35
## 6 g6 6 16 26 36
## 7 g7 7 17 27 37
## 8 g8 8 18 28 38
## 9 g9 9 19 29 39
## 10 g10 10 20 30 40
df2 <- bind_cols(data_frame(ids2=paste0("g", c(2,5,11,12))), as_data_frame(matrix(1:16, 4, 4, dimnames=list(1:4, paste0("CB", 1:4)))))
df2
## # A tibble: 4 x 5
## ids2 CB1 CB2 CB3 CB4
## <chr> <int> <int> <int> <int>
## 1 g2 1 5 9 13
## 2 g5 2 6 10 14
## 3 g11 3 7 11 15
## 4 g12 4 8 12 16
Inner join
inner_join(df1, df2, by=c("ids1"="ids2"))
## # A tibble: 2 x 9
## ids1 CA1 CA2 CA3 CA4 CB1 CB2 CB3 CB4
## <chr> <int> <int> <int> <int> <int> <int> <int> <int>
## 1 g2 2 12 22 32 1 5 9 13
## 2 g5 5 15 25 35 2 6 10 14
Left join
left_join(df1, df2, by=c("ids1"="ids2"))
## # A tibble: 10 x 9
## ids1 CA1 CA2 CA3 CA4 CB1 CB2 CB3 CB4
## <chr> <int> <int> <int> <int> <int> <int> <int> <int>
## 1 g1 1 11 21 31 NA NA NA NA
## 2 g2 2 12 22 32 1 5 9 13
## 3 g3 3 13 23 33 NA NA NA NA
## 4 g4 4 14 24 34 NA NA NA NA
## 5 g5 5 15 25 35 2 6 10 14
## 6 g6 6 16 26 36 NA NA NA NA
## 7 g7 7 17 27 37 NA NA NA NA
## 8 g8 8 18 28 38 NA NA NA NA
## 9 g9 9 19 29 39 NA NA NA NA
## 10 g10 10 20 30 40 NA NA NA NA
Right join
right_join(df1, df2, by=c("ids1"="ids2"))
## # A tibble: 4 x 9
## ids1 CA1 CA2 CA3 CA4 CB1 CB2 CB3 CB4
## <chr> <int> <int> <int> <int> <int> <int> <int> <int>
## 1 g2 2 12 22 32 1 5 9 13
## 2 g5 5 15 25 35 2 6 10 14
## 3 g11 NA NA NA NA 3 7 11 15
## 4 g12 NA NA NA NA 4 8 12 16
Full join
full_join(df1, df2, by=c("ids1"="ids2"))
## # A tibble: 12 x 9
## ids1 CA1 CA2 CA3 CA4 CB1 CB2 CB3 CB4
## <chr> <int> <int> <int> <int> <int> <int> <int> <int>
## 1 g1 1 11 21 31 NA NA NA NA
## 2 g2 2 12 22 32 1 5 9 13
## 3 g3 3 13 23 33 NA NA NA NA
## 4 g4 4 14 24 34 NA NA NA NA
## 5 g5 5 15 25 35 2 6 10 14
## 6 g6 6 16 26 36 NA NA NA NA
## 7 g7 7 17 27 37 NA NA NA NA
## 8 g8 8 18 28 38 NA NA NA NA
## 9 g9 9 19 29 39 NA NA NA NA
## 10 g10 10 20 30 40 NA NA NA NA
## 11 g11 NA NA NA NA 3 7 11 15
## 12 g12 NA NA NA NA 4 8 12 16
Anti join
anti_join(df1, df2, by=c("ids1"="ids2"))
## # A tibble: 8 x 5
## ids1 CA1 CA2 CA3 CA4
## <chr> <int> <int> <int> <int>
## 1 g1 1 11 21 31
## 2 g3 3 13 23 33
## 3 g4 4 14 24 34
## 4 g6 6 16 26 36
## 5 g7 7 17 27 37
## 6 g8 8 18 28 38
## 7 g9 9 19 29 39
## 8 g10 10 20 30 40
For additional join options users want to cosult the *_join help pages.
Chaining
To simplify chaining of serveral operations, dplyr provides the %>%
operator, where x %>% f(y) turns into f(x, y). This way one can pipe
together multiple operations by writing them from left-to-right or
top-to-bottom. This makes for easy to type and readable code.
Example 1
Series of data manipulations and export
read_tsv("iris.txt") %>% # Import with read_tbv from readr package
as_tibble() %>% # Declare tibble to use
select(Sepal.Length:Species) %>% # Select columns
filter(Species=="setosa") %>% # Filter rows by some value
arrange(Sepal.Length) %>% # Sort by some column
mutate(Subtract=Petal.Length - Petal.Width) # Calculate and append
## # A tibble: 50 x 6
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species Subtract
## <dbl> <dbl> <dbl> <dbl> <chr> <dbl>
## 1 4.30 3.00 1.10 0.100 setosa 1.00
## 2 4.40 2.90 1.40 0.200 setosa 1.20
## 3 4.40 3.00 1.30 0.200 setosa 1.10
## 4 4.40 3.20 1.30 0.200 setosa 1.10
## 5 4.50 2.30 1.30 0.300 setosa 1.00
## 6 4.60 3.10 1.50 0.200 setosa 1.30
## 7 4.60 3.40 1.40 0.300 setosa 1.10
## 8 4.60 3.60 1.00 0.200 setosa 0.800
## 9 4.60 3.20 1.40 0.200 setosa 1.20
## 10 4.70 3.20 1.30 0.200 setosa 1.10
## # ... with 40 more rows
# write_tsv("iris.txt") # Export to file, omitted here to show result
Example 2
Series of summary calculations for grouped data (group_by)
iris_df %>% # Declare tibble to use
group_by(Species) %>% # Group by species
summarize(Mean_Sepal.Length=mean(Sepal.Length),
Max_Sepal.Length=max(Sepal.Length),
Min_Sepal.Length=min(Sepal.Length),
SD_Sepal.Length=sd(Sepal.Length),
Total=n())
## # A tibble: 3 x 6
## Species Mean_Sepal.Length Max_Sepal.Length Min_Sepal.Length SD_Sepal.Length Total
## <chr> <dbl> <dbl> <dbl> <dbl> <int>
## 1 setosa 5.01 5.80 4.30 0.352 50
## 2 versicolor 5.94 7.00 4.90 0.516 50
## 3 virginica 6.59 7.90 4.90 0.636 50
Example 3
Combining dplyr chaining with ggplot
iris_df %>%
group_by(Species) %>%
summarize_all(mean) %>%
reshape2::melt(id.vars=c("Species"), variable.name = "Samples", value.name="Values") %>%
ggplot(aes(Samples, Values, fill = Species)) +
geom_bar(position="dodge", stat="identity")

Previous Page Next Page
