How is table function in R useful?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

# How is table function in R useful?

How is table function in R useful

0

## Recipe Objective

Table function (table())in R performs a tabulation of categorical variable and gives its frequency as output. It is further useful to create conditional frequency table and Proportinal frequency table.

This recipe demonstrates how to use table() function to create the following two tables:

1. Frequency table
2. Frequency table with proportion

Syntax:

table(x)

where: x = one or more objects which are mostly factors

## Step 1: Importing required library and Reading dataset

``` # Data manipulation package install.packages("tidyverse") library(tidyverse) ​ # reading a dataset customer_seg = read.csv('R_66_Mall_Customers.csv') ​ glimpse(customer_seg) ```
```Rows: 200
Columns: 5
\$ CustomerID              1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1...
\$ Gender                  Male, Male, Female, Female, Female, Female, ...
\$ Age                     19, 21, 20, 23, 31, 22, 35, 23, 64, 30, 67, ...
\$ Annual.Income..k..      15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 19, ...
\$ Spending.Score..1.100.  39, 81, 6, 77, 40, 76, 6, 94, 3, 72, 14, 99,...
```

Dataset description: It is the basic data about the customers going to the supermarket mall. The variable that we interested in finding the frequency is Gender - Male or female

## 1. Frequency table

We pass the column Gender as an arguement in table function to give the frequency table.

``` table(customer_seg\$Gender) ```
```Female   Male
112     88
```

Note: 112 is the number of times Female was used in the column Gender

## 2. Frequency table with proportion

We use the prop.table() function along with the table() funnction to get the proportions

``` # creating a frequency table and storing it in variable table_1 table_1 = as.table(table(customer_seg\$Gender)) ​ # passing the frequency table as the argument in prop.table() prop.table(table_1) ```
```Female   Male
0.56   0.44
```

#### Relevant Projects

##### Census Income Data Set Project - Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based on census data.

##### Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

##### Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

##### Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

##### 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.

##### Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

##### Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

##### PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

##### Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

##### Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.