Apply Function to Every Row of Data Using dplyr Package in R | rowwise Function Explained
In this R tutorial you’ll learn how to apply a function to each row of a data frame or tibble with the dplyr package of the tidyverse.
This task can be done with the rowwise function and, hence, this article contains one examples for this function. More precisely, the article contains the following information:
- Construction of Example Data
- Example: Apply rowwise Function of dplyr Package in R
- Video & Further Resources
If you want to know more about these contents, keep reading:
Construction of Example Data
First, we need to install and load the dplyr package to R:
install.packages("dplyr") # Install and load dplyr library("dplyr")
Furthermore, we need to create some example data as basement for the example:
data <- as.tbl(data.frame(x1 = 1:5, # Create example data x2 = 2:6, x3 = 3:7)) data # Print data to console # # A tibble: 5 x 3 # x1 x2 x3 # <int> <int> <int> # 1 1 2 3 # 2 2 3 4 # 3 3 4 5 # 4 4 5 6 # 5 5 6 7
Our example data consists of five rows and three numeric columns. Note that we are using a tibble in this example. However, we could also apply the following R code to a data frame.
Example: Apply rowwise Function of dplyr Package in R
If we want to apply a function to each row of a data table, we can use the rowwise function of the dplyr package in combination with the mutate function. Have a look at the following R syntax:
data %>% # Apply rowwise function rowwise() %>% mutate(row_sum = sum(x1, x2, x3)) # # A tibble: 5 x 4 # x1 x2 x3 row_sum # <int> <int> <int> <int> # 1 1 2 3 6 # 2 2 3 4 9 # 3 3 4 5 12 # 4 4 5 6 15 # 5 5 6 7 18
As you can see based on the output of the RStudio console, we just created a new tibble with an additional variable row_sum, containing the row sums of each row of our data matrix.
In other words: We applied the sum function to each row of our tibble. However, we could use any other function instead of the sum function.
Video & Further Resources
Do you need further explanations on the content of this article? Then you might watch the following video of my YouTube channel. I illustrate the R programming codes of this article in the video:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you could have a look at some of the related R programming posts of my homepage:
- Apply Function to Every Row of Data Frame or Matrix in Base R
- mutate & transmute R Functions of dplyr Package
- dplyr Package in R
- R Functions List (+ Examples)
- The R Programming Language
To summarize: In this article you learned how to use an R command for each row of a data table using the dplyr package of the R programming language. If you have any additional comments and/or questions, don’t hesitate to tell me about it in the comments section below.
Statistics Globe Newsletter