- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a data frame with numerical as well as factor columns in R.
To create a data frame with numerical as well as factor columns in R, we simply need to add factor function before factor columns and numerical columns will be created without mentioning any specific characteristics, the values of numerical columns just need to be numerical in nature.
Example 1
Following snippet creates a sample data frame df1 with factor and numerical column −
Gender<-factor(sample(c("Male","Female"),20,replace=TRUE)) Score<-sample(1:20,20) df1<-data.frame(Gender,Score) df1
The following dataframe is created
Gender Score 1 Male 4 2 Male 18 3 Female 11 4 Female 3 5 Male 17 6 Female 13 7 Male 15 8 Male 1 9 Female 20 10 Male 6 11 Male 16 12 Female 5 13 Male 14 14 Female 8 15 Female 9 16 Male 10 17 Male 7 18 Female 2 19 Male 19 20 Male 12
To check the structure of df1 on the above created data frame, add the following code to the above snippet −
Gender<-factor(sample(c("Male","Female"),20,replace=TRUE)) Score<-sample(1:20,20) df1<-data.frame(Gender,Score) str(df1)
Output
If you execute all the above given snippets as a single program, it generates the following Output −
'data.frame': 20 obs. of 2 variables: $ Gender: Factor w/ 2 levels "Female","Male": 2 2 1 1 2 1 2 2 1 2 ... $ Score : int 4 18 11 3 17 13 15 1 20 6 ...
Example 2
Following snippet creates a sample data frame df2 with factor and numerical column −
Group<-factor(sample(LETTERS[1:4],20,replace=TRUE)) Rate<-round(rnorm(20,10,3),2) df2<-data.frame(Group,Rate) df2
The following dataframe is created
Group Rate 1 B 3.87 2 C 7.66 3 C 11.59 4 A 11.26 5 D 9.29 6 B 2.92 7 B 8.86 8 B 6.41 9 D 7.65 10 C 7.99 11 D 4.13 12 C 13.33 13 C 10.89 14 D 12.49 15 B 5.02 16 B 6.44 17 A 12.32 18 D 14.01 19 B 8.46 20 B 4.60
To check the structure of df2 on the above created data frame, add the following code to the above snippet −
Group<-factor(sample(LETTERS[1:4],20,replace=TRUE)) Rate<-round(rnorm(20,10,3),2) df2<-data.frame(Group,Rate) str(df2)
Output
If you execute all the above given snippets as a single program, it generates the following Output −
'data.frame': 20 obs. of 2 variables: $ Group: Factor w/ 4 levels "A","B","C","D": 2 3 3 1 4 2 2 2 4 3 ... $ Rate : num 3.87 7.66 11.59 11.26 9.29 ...
- Related Articles
- How to convert multiple columns in an R data frame into a single numerical column along with a column having column names as factor?
- How to create a data frame with one or more columns as a list in R?
- How to sort a numerical factor column in an R data frame?
- How to concatenate numerical columns in an R data frame?
- How to create table of two factor columns in an R data frame?
- How to create a vector with lowercase as well as uppercase letters in R?
- How to subset factor columns in an R data frame?
- How to convert a data frame to a matrix if the data frame contains factor variable as strings in R?
- How to find the number of numerical columns in an R data frame?
- How to standardize only numerical columns in an R data frame if categorical columns also exist?
- How to convert dot to comma for numerical columns in an R data frame?
- How to create barplot from data frame in R using rows as categories?
- How to find the maximum of factor levels in numerical column and return the output including other columns in the R data frame?
- How to convert a matrix to a data frame with column names and row names as new columns in R?
- How to create scatterplot using data frame columns in R?
