R Programming: Create an Ordered Factor from Month names
R Programming: Factors Exercise-3 with Solution
Write a R program to create an ordered factor from data consisting of the names of months.
Sample Solution :
R Programming Code :
# Create a vector of month names
mons_v = c("March","April","January","November","January",
"September","October","September","November","August","February",
"January","November","November","February","May","August","February",
"July","December","August","August","September","November","September",
"February","April")
# Print the original vector of month names
print("Original vector:")
print(mons_v)
# Convert the month names into a factor (unordered by default)
f = factor(mons_v)
# Print the ordered factors of the month names
print("Ordered factors of the said vector:")
print(f)
# Print the frequency table of the factors
print(table(f))
Output:
[1] "Original vector:" [1] "March" "April" "January" "November" "January" "September" [7] "October" "September" "November" "August" "February" "January" [13] "November" "November" "February" "May" "August" "February" [19] "July" "December" "August" "August" "September" "November" [25] "September" "February" "April" [1] "Ordered factors of the said vector:" [1] March April January November January September October [8] September November August February January November November [15] February May August February July December August [22] August September November September February April 11 Levels: April August December February January July March May ... September f April August December February January July March May 2 4 1 4 3 1 1 1 November October September 5 1 4
Explanation:
- mons_v = c(...): Creates a character vector mons_v containing the names of months.
- print("Original vector:"): Displays the label "Original vector:".
- print(mons_v): Prints the contents of the mons_v vector, showing the month names.
- f = factor(mons_v): Converts the vector mons_v into a factor f. A factor is a data structure used to categorize the data and store it as levels.
- print("Ordered factors of the said vector:"): Prints the label "Ordered factors of the said vector:".
- print(f): Displays the factor f, showing the unique levels of the months.
- print(table(f)): Displays a frequency table of the levels in the factor f, showing how many times each month appears in the vector.
R Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a R program to change the first level of a factor with another level of a given factor.
Next: Write a R program to concatenate two given factor in a single factor.
Test your Programming skills with w3resource's quiz.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/r-programming-exercises/factors/r-programming-factors-exercise-3.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics