What is table and xtabs function in R

This recipe explains what is table and xtabs function in R

Recipe Objective

What is the table and xtabs function?

A table () function in R returns the desired column data along with its corresponding frequencies. Hence, the users can create a frequency table using this function. Syntax: **table (data$variable)** xtabs () can be used calculating frequencies of variables in a given dataframe Syntax: **xtabs (~variable_name, data=data)** This recipe demonstrates an example on the table () and xtabs () in R.

Step 1 - Read a csv file

data <- read.csv('/content/bestsellers with categories.csv') # reading a csv file of 'bestsellers with categories'

Step 2 - Use table()

table(data$Name) # returns values corresponding to the Name variable

"Output of code is :"
 10-Day Green Smoothie Cleanse 
                                                                                                                           1 
                                                                                                           11/22/63: A Novel 
                                                                                                                           1 
                                                                                     12 Rules for Life: An Antidote to Chaos 
                                                                                                                           1 
                                                                                                      1984 (Signet Classics) 
                                                                                                                           1 
                                                          5,000 Awesome Facts (About Everything!) (National Geographic Kids) 
                                                                                                                           1 
                                                                               A Dance with Dragons (A Song of Ice and Fire) 
                                                                                                                           1 
and more.....

table(data$Price) # returns the Price variable values

"Output of code is :"
  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19 
 12   1   5   1  32  36  38  23  52  38  28  35  27  29  30  21  20  19  14   4 
 20  21  22  23  24  25  27  28  30  32  36  39  40  42  46  52  53  54  82 105 
 17   9   6   5   5   2   4   6   5   2   1   1   5   1  10   1   1   1   1   2 

Step 3 - Define a dataframe()

df <- data.frame(x = c("X","X","X","Y","Y","Z","Z","Z","Z","Z"), y = c(1,2,3,4,5,6,7,8,9,10), z = c(10,56,24,96,32,89,12,36,12,56)) print(df)

"Dataframe is :"
   x  y  z
1  X  1 10
2  X  2 56
3  X  3 24
4  Y  4 96
5  Y  5 32
6  Z  6 89
7  Z  7 12
8  Z  8 36
9  Z  9 12
10 Z 10 56

Step 4 - Use the xtabs()

xtabs(~x, data=df) # returns the frequencies of the variable/column - x in the dataframe

"Output of code is :"
x
X Y Z 
3 2 5 

{"mode":"full","isActive":false}

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

Mastering A/B Testing: A Practical Guide for Production
In this A/B Testing for Machine Learning Project, you will gain hands-on experience in conducting A/B tests, analyzing statistical significance, and understanding the challenges of building a solution for A/B testing in a production environment.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.