Create a data frame with numerical as well as factor columns in R.

R ProgrammingServer Side ProgrammingProgramming

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 ...
raja
Updated on 09-Nov-2021 07:59:36

Advertisements