w3resource

R Programming: Create a factor corresponding to height of women data set, which contains height and weights for a sample of women

R Programming: Factors Exercise-7 with Solution

Write a R program to create a factor corresponding to height of women data set, which contains height and weights for a sample of women.

Sample Solution :

R Programming Code :

data = women
print("Women data set of height and weights:")
print(data)
height_f = cut(women$height,3)
print("Factor corresponding to height:")
print(table(height_f))

Sample Output:

[1] "Women data set of height and weights:"
   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
[1] "Factor corresponding to height:"
height_f
  (58,62.7] (62.7,67.3]   (67.3,72] 
          5           5           5                          

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to extract the five of the levels of factor created from a random sample from the LETTERS (Part of the base R distribution.).

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.