SlideShare ist ein Scribd-Unternehmen logo
1 von 17
FACTORS
Factors are the data objects which are used to
categorize the data and store it as levels.
They can store both strings and integers.
They are useful in the columns which have a
limited number of unique values.
Like "Male, "Female" and True, False etc. They are
useful in data analysis for statistical modeling.
Factors are created using the factor () function by
taking a vector as input.
• data <-
c("East","West","East","North","North","East",
"West","West","West","East","North")
• print(data) print(is.factor(data))
• # Apply the factor function.
• factor_data <- factor(data)
• print(factor_data)
• print(is.factor(factor_data))
Factors in Data Frame
• On creating any data frame with a column of text data, R
treats the text column as categorical data and creates factors
on it.
• height <- c(132,151,162,139,166,147,122)
• weight <- c(48,49,66,53,67,52,40)
• gender <-
c("male","male","female","female","male","female","male")
• # Create the data frame.
• input_data <- data.frame(height,weight,gender)
print(input_data)
• # Test if the gender column is a factor.
print(is.factor(input_data$gender))
• # Print the gender column so see the levels.
print(input_data$gender)
Changing the Order of Levels
• The order of the levels in a factor can be changed
by applying the factor function again with new
order of the levels.
• data <-
c("East","West","East","North","North","East","W
est", "West","West","East","North")
• factor_data <- f
• new_order_data <- factor(factor_data,levels =
c("East","West","North"))
print(new_order_data)actor(data)
print(factor_data)
Generating Factor Levels
• We can generate factor levels by using the gl() function.
• It takes two integers as input which indicates how many levels and
how many times
• Syntax
• gl(n, k, labels) Following is the description of the parameters used −
• n is a integer giving the number of levels.
• k is a integer giving the number of replications.
• labels is a vector of labels for the resulting factor levels.
• each level
• Example
• v <- gl(3, 4, labels = c("Tampa", "Seattle","Boston"))
• print(v)
Checking for a Factor in R
• The function is.factor() is used to check whether the variable
is a factor and returns “TRUE” if it is a factor.
• gender <- factor(c("female", "male", "male", "female"));
• print(is.factor(gender))
• Accessing elements of a Factor in R
• Like we access elements of a vector, the same way we access
the elements of a factor. If gender is a factor then gender[i]
would mean accessing ith element in the factor.
• Example:
• gender <- factor(c("female", "male", "male", "female"));
• gender[3]
• gender <- factor(c("female", "male", "male", "female"));
• gender[c(2, 4)]
•
Modification of a Factor in R
• After a factor is formed, its components can be modified but the
new values which need to be assigned must be at the predefined
level.
• Example:
• gender <- factor(c("female", "male", "male", "female" ));
• gender[2]<-"female"
• gender
• ender <- factor(c("female", "male", "male", "female" ));
•
• # add new level
• levels(gender) <- c(levels(gender), "other")
• gender[3] <- "other"
• gender
Factors in Data Frame
• The Data frame is similar to a 2D array with the columns
containing all the values of one variable and the rows
having one set of values from every column. There are
four things to remember about data frames:
• column names are compulsory and cannot be empty.
• Unique names should be assigned to each row.
• The data frame’s data can be only of three types- factor,
numeric, and character type.
• The same number of data items must be present in each
column.
• In R language when we create a data frame, its column is
categorical data and hence a factor is automatically
created on it.
We can create a data frame and check if its column is a
factor.
• Example:
• age <- c(40, 49, 48, 40, 67, 52, 53)
• salary <- c(103200, 106200, 150200,
• 10606, 10390, 14070, 10220)
• gender <- c("male", "male", "transgender",
• "female", "male", "female",
"transgender")
• employee<- data.frame(age, salary, gender)
• print(employee)
• print(is.factor(employee$gender))
• Gender<-
sample(c("Male","Female"),20,replace=TRUE)
• Values<-rnorm(20,mean=0,sd=1)
• Group<-sample(letters[1:5],20,replace=TRUE)
•
• df<-data.frame(Gender,Values,Group)
• library(ggplot2)
•
• # creating a boxplot
• ggplot(df,aes(Gender,Values))+geom_boxplot(aes
(fill=Group))
LIST
• Lists are the R objects which contain elements of different
types like − numbers, strings, vectors and another list
inside it.
• A list can also contain a matrix or a function as its
elements.
• List is created using list() function.
• Creating a List
• Following is an example to create a list containing strings,
numbers, vectors and a logical values.
• # Create a list containing strings, numbers, vectors and a
logical # values.
• list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23,
119.1)
• print(list_data)
Naming List Elements
• The list elements can be given names and they can be
accessed using these names.
• # Create a list containing a vector, a matrix and a list.
• list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-
2,8), nrow = 2), list("green",12.3))
• # Give names to the elements in the list.
names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner
list")
• # Show the list.
• print(list_data)
Accessing List Elements
• Elements of the list can be accessed by the index of the element in
the list. In case of named lists it can also be accessed using the
names.
• We continue to use the list in the above example −
• # Create a list containing a vector, a matrix and a list.
• list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow =
2), list("green",12.3))
• # Give names to the elements in the list.
• names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")
• # Access the first element of the list.
• print(list_data[1])
• # Access the thrid element.
• As it is also a list, all its elements will be printed.
• print(list_data[3])
• # Access the list element using the name of the element.
print(list_data$A_Matrix)
Manipulating List Elements
• We can add, delete and update list elements as shown below. We can
add and delete elements only at the end of a list. But we can update
any element.
• # Create a list containing a vector, a matrix and a list.
• list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2),
list("green",12.3))
• # Give names to the elements in the list. names(list_data) <- c("1st
Quarter", "A_Matrix", "A Inner list")
• # Add element at the end of the list.
• list_data[4] <- "New element" print(list_data[4])
• # Remove the last element.
• list_data[4] <- NULL
• # Print the 4th Element.
• print(list_data[4])
• # Update the 3rd Element.
• list_data[3] <- "updated element"
• print(list_data[3])
Merging Lists
• You can merge many lists into one list by
placing all the lists inside one list() function
• # Create two lists. list1 <- list(1,2,3) list2 <-
list("Sun","Mon","Tue") # Merge the two lists.
merged.list <- c(list1,list2) # Print the merged
list. print(merged.list)
Converting List to Vector
• A list can be converted to a vector so that the
elements of the vector can be used for further
manipulation. All the arithmetic operations on
vectors can be applied after the list is
converted into vectors. To do this conversion,
we use the unlist() function. It takes the list as
input and produces a vector.
• # Create lists.
• list1 <- list(1:5)
• print(list1)
• list2 <-list(10:14)
• print(list2)
• # Convert the lists to vectors.
• v1 <- unlist(list1)
• v2 <- unlist(list2)
• print(v1)
• print(v2)
• # Now add the vectors
• result <- v1+v2
• print(result)

Weitere ähnliche Inhalte

Was ist angesagt?

SQL Queries - DDL Commands
SQL Queries - DDL CommandsSQL Queries - DDL Commands
SQL Queries - DDL CommandsShubhamBauddh
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)Ankit Dubey
 
Data manipulation with dplyr
Data manipulation with dplyrData manipulation with dplyr
Data manipulation with dplyrRomain Francois
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and OperatorsMohan Kumar.R
 
Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Danial Virk
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in pythonhydpy
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programmingizahn
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factorskrishna singh
 
Basic Concept of Database
Basic Concept of DatabaseBasic Concept of Database
Basic Concept of DatabaseMarlon Jamera
 
Python For Data Science Cheat Sheet
Python For Data Science Cheat SheetPython For Data Science Cheat Sheet
Python For Data Science Cheat SheetKarlijn Willems
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP StringsAhmed Swilam
 
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...Raj vardhan
 

Was ist angesagt? (20)

SQL Queries - DDL Commands
SQL Queries - DDL CommandsSQL Queries - DDL Commands
SQL Queries - DDL Commands
 
Arrays
ArraysArrays
Arrays
 
Banking Database
Banking DatabaseBanking Database
Banking Database
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)
 
Data manipulation with dplyr
Data manipulation with dplyrData manipulation with dplyr
Data manipulation with dplyr
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and Operators
 
Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Aggregate function
Aggregate functionAggregate function
Aggregate function
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Data mining
Data miningData mining
Data mining
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 
Java notes jkuat it
Java notes jkuat itJava notes jkuat it
Java notes jkuat it
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors
 
Basic Concept of Database
Basic Concept of DatabaseBasic Concept of Database
Basic Concept of Database
 
Python For Data Science Cheat Sheet
Python For Data Science Cheat SheetPython For Data Science Cheat Sheet
Python For Data Science Cheat Sheet
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
 
SQL BASIC QUERIES
SQL  BASIC QUERIES SQL  BASIC QUERIES
SQL BASIC QUERIES
 
Arrays
ArraysArrays
Arrays
 

Ähnlich wie Factors.pptx (20)

R교육1
R교육1R교육1
R교육1
 
R training3
R training3R training3
R training3
 
Language R
Language RLanguage R
Language R
 
R training2
R training2R training2
R training2
 
Day 1b R structures objects.pptx
Day 1b   R structures   objects.pptxDay 1b   R structures   objects.pptx
Day 1b R structures objects.pptx
 
R factors
R   factorsR   factors
R factors
 
Data Types of R.pptx
Data Types of R.pptxData Types of R.pptx
Data Types of R.pptx
 
Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
Arrays
ArraysArrays
Arrays
 
R workshop
R workshopR workshop
R workshop
 
Data Analysis and Programming in R
Data Analysis and Programming in RData Analysis and Programming in R
Data Analysis and Programming in R
 
Oracle sql ppt2
Oracle sql ppt2Oracle sql ppt2
Oracle sql ppt2
 
R language introduction
R language introductionR language introduction
R language introduction
 
Array
ArrayArray
Array
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdf
 
R language tutorial.pptx
R language tutorial.pptxR language tutorial.pptx
R language tutorial.pptx
 
Arrays
ArraysArrays
Arrays
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 
Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!
 

Mehr von Ramakrishna Reddy Bijjam

Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxRamakrishna Reddy Bijjam
 
Python With MongoDB in advanced Python.pptx
Python With MongoDB in advanced Python.pptxPython With MongoDB in advanced Python.pptx
Python With MongoDB in advanced Python.pptxRamakrishna Reddy Bijjam
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxRamakrishna Reddy Bijjam
 
Certinity Factor and Dempster-shafer theory .pptx
Certinity Factor and Dempster-shafer theory .pptxCertinity Factor and Dempster-shafer theory .pptx
Certinity Factor and Dempster-shafer theory .pptxRamakrishna Reddy Bijjam
 
Auxiliary Memory in computer Architecture.pptx
Auxiliary Memory in computer Architecture.pptxAuxiliary Memory in computer Architecture.pptx
Auxiliary Memory in computer Architecture.pptxRamakrishna Reddy Bijjam
 

Mehr von Ramakrishna Reddy Bijjam (20)

Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptx
 
Auxiliary, Cache and Virtual memory.pptx
Auxiliary, Cache and Virtual memory.pptxAuxiliary, Cache and Virtual memory.pptx
Auxiliary, Cache and Virtual memory.pptx
 
Python With MongoDB in advanced Python.pptx
Python With MongoDB in advanced Python.pptxPython With MongoDB in advanced Python.pptx
Python With MongoDB in advanced Python.pptx
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
Certinity Factor and Dempster-shafer theory .pptx
Certinity Factor and Dempster-shafer theory .pptxCertinity Factor and Dempster-shafer theory .pptx
Certinity Factor and Dempster-shafer theory .pptx
 
Auxiliary Memory in computer Architecture.pptx
Auxiliary Memory in computer Architecture.pptxAuxiliary Memory in computer Architecture.pptx
Auxiliary Memory in computer Architecture.pptx
 
Random Forest Decision Tree.pptx
Random Forest Decision Tree.pptxRandom Forest Decision Tree.pptx
Random Forest Decision Tree.pptx
 
K Means Clustering in ML.pptx
K Means Clustering in ML.pptxK Means Clustering in ML.pptx
K Means Clustering in ML.pptx
 
Pandas.pptx
Pandas.pptxPandas.pptx
Pandas.pptx
 
Python With MongoDB.pptx
Python With MongoDB.pptxPython With MongoDB.pptx
Python With MongoDB.pptx
 
Python with MySql.pptx
Python with MySql.pptxPython with MySql.pptx
Python with MySql.pptx
 
PYTHON PROGRAMMING NOTES RKREDDY.pdf
PYTHON PROGRAMMING NOTES RKREDDY.pdfPYTHON PROGRAMMING NOTES RKREDDY.pdf
PYTHON PROGRAMMING NOTES RKREDDY.pdf
 
BInary file Operations.pptx
BInary file Operations.pptxBInary file Operations.pptx
BInary file Operations.pptx
 
Data Science in Python.pptx
Data Science in Python.pptxData Science in Python.pptx
Data Science in Python.pptx
 
CSV JSON and XML files in Python.pptx
CSV JSON and XML files in Python.pptxCSV JSON and XML files in Python.pptx
CSV JSON and XML files in Python.pptx
 
HTML files in python.pptx
HTML files in python.pptxHTML files in python.pptx
HTML files in python.pptx
 
Regular Expressions in Python.pptx
Regular Expressions in Python.pptxRegular Expressions in Python.pptx
Regular Expressions in Python.pptx
 
datareprersentation 1.pptx
datareprersentation 1.pptxdatareprersentation 1.pptx
datareprersentation 1.pptx
 
Apriori.pptx
Apriori.pptxApriori.pptx
Apriori.pptx
 
Eclat.pptx
Eclat.pptxEclat.pptx
Eclat.pptx
 

Kürzlich hochgeladen

Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxolyaivanovalion
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiSuhani Kapoor
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023ymrp368
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Delhi Call girls
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystSamantha Rae Coolbeth
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSAishani27
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxolyaivanovalion
 

Kürzlich hochgeladen (20)

Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data Analyst
 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICS
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptx
 

Factors.pptx

  • 1. FACTORS Factors are the data objects which are used to categorize the data and store it as levels. They can store both strings and integers. They are useful in the columns which have a limited number of unique values. Like "Male, "Female" and True, False etc. They are useful in data analysis for statistical modeling. Factors are created using the factor () function by taking a vector as input.
  • 2. • data <- c("East","West","East","North","North","East", "West","West","West","East","North") • print(data) print(is.factor(data)) • # Apply the factor function. • factor_data <- factor(data) • print(factor_data) • print(is.factor(factor_data))
  • 3. Factors in Data Frame • On creating any data frame with a column of text data, R treats the text column as categorical data and creates factors on it. • height <- c(132,151,162,139,166,147,122) • weight <- c(48,49,66,53,67,52,40) • gender <- c("male","male","female","female","male","female","male") • # Create the data frame. • input_data <- data.frame(height,weight,gender) print(input_data) • # Test if the gender column is a factor. print(is.factor(input_data$gender)) • # Print the gender column so see the levels. print(input_data$gender)
  • 4. Changing the Order of Levels • The order of the levels in a factor can be changed by applying the factor function again with new order of the levels. • data <- c("East","West","East","North","North","East","W est", "West","West","East","North") • factor_data <- f • new_order_data <- factor(factor_data,levels = c("East","West","North")) print(new_order_data)actor(data) print(factor_data)
  • 5. Generating Factor Levels • We can generate factor levels by using the gl() function. • It takes two integers as input which indicates how many levels and how many times • Syntax • gl(n, k, labels) Following is the description of the parameters used − • n is a integer giving the number of levels. • k is a integer giving the number of replications. • labels is a vector of labels for the resulting factor levels. • each level • Example • v <- gl(3, 4, labels = c("Tampa", "Seattle","Boston")) • print(v)
  • 6. Checking for a Factor in R • The function is.factor() is used to check whether the variable is a factor and returns “TRUE” if it is a factor. • gender <- factor(c("female", "male", "male", "female")); • print(is.factor(gender)) • Accessing elements of a Factor in R • Like we access elements of a vector, the same way we access the elements of a factor. If gender is a factor then gender[i] would mean accessing ith element in the factor. • Example: • gender <- factor(c("female", "male", "male", "female")); • gender[3] • gender <- factor(c("female", "male", "male", "female")); • gender[c(2, 4)] •
  • 7. Modification of a Factor in R • After a factor is formed, its components can be modified but the new values which need to be assigned must be at the predefined level. • Example: • gender <- factor(c("female", "male", "male", "female" )); • gender[2]<-"female" • gender • ender <- factor(c("female", "male", "male", "female" )); • • # add new level • levels(gender) <- c(levels(gender), "other") • gender[3] <- "other" • gender
  • 8. Factors in Data Frame • The Data frame is similar to a 2D array with the columns containing all the values of one variable and the rows having one set of values from every column. There are four things to remember about data frames: • column names are compulsory and cannot be empty. • Unique names should be assigned to each row. • The data frame’s data can be only of three types- factor, numeric, and character type. • The same number of data items must be present in each column. • In R language when we create a data frame, its column is categorical data and hence a factor is automatically created on it. We can create a data frame and check if its column is a factor.
  • 9. • Example: • age <- c(40, 49, 48, 40, 67, 52, 53) • salary <- c(103200, 106200, 150200, • 10606, 10390, 14070, 10220) • gender <- c("male", "male", "transgender", • "female", "male", "female", "transgender") • employee<- data.frame(age, salary, gender) • print(employee) • print(is.factor(employee$gender))
  • 10. • Gender<- sample(c("Male","Female"),20,replace=TRUE) • Values<-rnorm(20,mean=0,sd=1) • Group<-sample(letters[1:5],20,replace=TRUE) • • df<-data.frame(Gender,Values,Group) • library(ggplot2) • • # creating a boxplot • ggplot(df,aes(Gender,Values))+geom_boxplot(aes (fill=Group))
  • 11. LIST • Lists are the R objects which contain elements of different types like − numbers, strings, vectors and another list inside it. • A list can also contain a matrix or a function as its elements. • List is created using list() function. • Creating a List • Following is an example to create a list containing strings, numbers, vectors and a logical values. • # Create a list containing strings, numbers, vectors and a logical # values. • list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1) • print(list_data)
  • 12. Naming List Elements • The list elements can be given names and they can be accessed using these names. • # Create a list containing a vector, a matrix and a list. • list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,- 2,8), nrow = 2), list("green",12.3)) • # Give names to the elements in the list. names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list") • # Show the list. • print(list_data)
  • 13. Accessing List Elements • Elements of the list can be accessed by the index of the element in the list. In case of named lists it can also be accessed using the names. • We continue to use the list in the above example − • # Create a list containing a vector, a matrix and a list. • list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2), list("green",12.3)) • # Give names to the elements in the list. • names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list") • # Access the first element of the list. • print(list_data[1]) • # Access the thrid element. • As it is also a list, all its elements will be printed. • print(list_data[3]) • # Access the list element using the name of the element. print(list_data$A_Matrix)
  • 14. Manipulating List Elements • We can add, delete and update list elements as shown below. We can add and delete elements only at the end of a list. But we can update any element. • # Create a list containing a vector, a matrix and a list. • list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2), list("green",12.3)) • # Give names to the elements in the list. names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list") • # Add element at the end of the list. • list_data[4] <- "New element" print(list_data[4]) • # Remove the last element. • list_data[4] <- NULL • # Print the 4th Element. • print(list_data[4]) • # Update the 3rd Element. • list_data[3] <- "updated element" • print(list_data[3])
  • 15. Merging Lists • You can merge many lists into one list by placing all the lists inside one list() function • # Create two lists. list1 <- list(1,2,3) list2 <- list("Sun","Mon","Tue") # Merge the two lists. merged.list <- c(list1,list2) # Print the merged list. print(merged.list)
  • 16. Converting List to Vector • A list can be converted to a vector so that the elements of the vector can be used for further manipulation. All the arithmetic operations on vectors can be applied after the list is converted into vectors. To do this conversion, we use the unlist() function. It takes the list as input and produces a vector.
  • 17. • # Create lists. • list1 <- list(1:5) • print(list1) • list2 <-list(10:14) • print(list2) • # Convert the lists to vectors. • v1 <- unlist(list1) • v2 <- unlist(list2) • print(v1) • print(v2) • # Now add the vectors • result <- v1+v2 • print(result)