R Program to create a factor corresponding to height of women data set

How to create a factor corresponding to the height of women data set

Here we are explaining how to write an R program to create a factor corresponding to the height of women data set. Here we are using built-in function cut() for this creation. The function cut() divides the range of x into intervals. It codes the values in x according to which intervals. The syntax of these functions are

cut(x, …)

– where x is an R object,like matrix, array or data frame

How to create a factor corresponding to the height of women data set in the R Program

Below are the steps used in the R program to create a factor corresponding to the height of the women data set, we directly give the values to built-in functions. And print the function result. Here we use the variable D for assigning women data variable H for assigning calculated factor value using cut() method. Print the resulting factor.

ALGORITHM

  • STEP 1: Assign variable D with given data
  • STEP 2: First print original data
  • STEP 3:Call the built-in function cut as cut(women$height,3)
  • STEP 4: Assign variable H with the function result
  • STEP 5: Print the result factor

R Source Code

# women dataset
dim(women)
## [1] 15  2
D = women
print("Women data set of height and weights:")
## [1] "Women data set of height and weights:"
print(D)
##    height weight
## 1      58    115
## 2      59    117
## 3      60    120
## 4      61    123
## 5      62    126
## 6      63    129
## 7      64    132
## 8      65    135
## 9      66    139
## 10     67    142
## 11     68    146
## 12     69    150
## 13     70    154
## 14     71    159
## 15     72    164
H = cut(women$height,3)
print("Factor corresponding to height:")
## [1] "Factor corresponding to height:"
print(table(H))
## H
##   (58,62.7] (62.7,67.3]   (67.3,72] 
##           5           5           5