﻿ R Programming: Create an ordered factor from data consisting of the names of months - w3resource

# R Programming: Create an ordered factor from data consisting of the names of months

## 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 :

``````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("Original vector:")
print(mons_v)
f = factor(mons_v)
print("Ordered factors of the said vector:")
print(f)
print(table(f))
``````

Sample 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
```

R Programming Code Editor:

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

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?

﻿