Warning: session_start(): open(/tmp/sess_d07dce67c1413ea0a0ef4e4c96b741ba, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
tapply() function in R - LearnShareIT

tapply() function in R

tapply function in r

Keep reading this article if you are looking for a simple version of the apply() function. Today, we will show you the tapply() function – a function belonging to the apply() family function in R.

What is the tapply() function in R?

The tapply() function executes a defined function to a group of vectors. In the previous article, we introduced the apply() function. We strongly recommend reading the article before going on with this article. 

Syntax: 

tapply(data, label, function)

Parametes: 

  • data: The values corresponding to each vector.
  • label: The name of target vectors.
  • function: The applied function.

How to use the tapply() function?

Now, we will see how to use the tapply() function with a few popular data types.

Use the tapply() function with vectors

One grouped by one variable

To warm up, look at the simple example below. We will use two vectors to create one-to-one variables. We will create a vector named shapes to store the shape and a vector named perimeter to store their perimeter. Then, we call the tapply() function and use the mean function to calculate the mean perimeter of each shape.

Code:

shapes = c("square", "rectangle", "oval", "oval", "triangle", "triangle")
perimeter = c(8, 16, 12.56, 28.26, 8, 32)
mean_perimeter = tapply(perimeter,shapes, mean)

cat("The mean of perimeters are:\n")
print(mean_perimeter)

Result:

The mean of perimeters are:
     oval rectangle    square  triangle 
    20.41     16.00      8.00     20.00 

One grouped by many variables

Also, you can categorize the data by many fields by passing a list of labels. Look at the following example to see more details. 

Code:

shapes = c("square", "rectangle", "oval", "oval", "triangle", "triangle")
color = c("blue", "red", "green", "pink", "red", "blue")
perimeter = c(8, 16, 12.56, 28.26, 8, 32)
 
mean_perimeter = tapply(perimeter, list(shapes,color), mean)
 
cat("The mean of perimeters are:\n")
print(mean_perimeter)

Result:

The mean of perimeters are:
          blue green  pink red
oval        NA 12.56 28.26  NA
rectangle   NA    NA    NA  16
square       8    NA    NA  NA
triangle    32    NA    NA   8

Use the tapply() function with a data frame

Actually, each data frame’s column is created from a vector. So, the tapply() function can be used with a data frame with the inputs as columns.

Let’s create a data frame from the vectors above and then carry out the same idea.

Code:

shapes = c("square", "rectangle", "oval", "oval", "triangle", "triangle")
color = c("blue", "red", "green", "pink", "red", "blue")
perimeter = c(8, 16, 12.56, 28.26, 8, 32)
 
df = data.frame(shapes, color, perimeter)
 
mean_perimeter = tapply(df$perimeter, list(df$shapes, df$color), mean)
 
cat("The mean of perimeters are:\n")
print(mean_perimeter)

Result:

The mean of perimeters are:
          blue green  pink red
oval        NA 12.56 28.26  NA
rectangle   NA    NA    NA  16
square       8    NA    NA  NA
triangle    32    NA    NA   8

Summary

In summary, the tapply() function executes a function to a target vector based on a label vector. If you want to apply the function based on multiple labels, pass them as a list of vectors.

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *