How to create a random sample of months in R?

R ProgrammingServer Side ProgrammingProgramming

Random samples can be created by using sample function and months in a year can be generated with the help of month.name function. Therefore, if we want to create a random sample of months then month.name can be used with sample function as sample(month.name) and if the size of the sample is larger than 12 then replace=TRUE argument should be used.

Examples

 Live Demo

x1<−sample(month.name)
x1

Output

[1] "October" "April" "March" "May" "June" "December"
[7] "July" "September" "January" "February" "November" "August"

Example

 Live Demo

x2<−sample(month.name,20,replace=TRUE)
x2

Output

[1] "August" "May" "June" "May" "October" "October"
[7] "February" "October" "March" "May" "October" "November"
[13] "December" "November" "August" "May" "July" "January"
[19] "July" "June"

Example

 Live Demo

x3<−sample(month.name,30,replace=TRUE)
x3

Output

[1] "April" "April" "November" "January" "September" "October"
[7] "June" "July" "March" "May" "December" "October"
[13] "December" "September" "August" "June" "November" "March"
[19] "November" "January" "May" "August" "February" "September"
[25] "July" "July" "February" "July" "November" "September"

Example

 Live Demo

x4<−sample(month.name,50,replace=TRUE)
x4

Output

[1] "June" "September" "July" "June" "October" "November" [7] "February" "August" "March" "April" "July" "August" [13] "October" "April" "June" "April" "March" "April" [19] "November" "September" "January" "August" "June" "March" [25] "September" "October" "July" "October" "March" "August" [31] "December" "February" "October" "July" "April" "December" [37] "November" "March" "May" "September" "May" "December" [43] "March" "September" "January" "August" "October" "April" [49] "June" "September"

Example

 Live Demo

x5<−sample(month.name,60,replace=TRUE)
x5

Output

[1] "December" "June" "May" "February" "February" "January"
[7] "January" "November" "March" "March" "July" "October"
[13] "February" "July" "November" "February" "October" "March"
[19] "September" "June" "April" "April" "November" "February"
[25] "May" "February" "March" "April" "September" "October"
[31] "November" "November" "April" "July" "June" "September"
[37] "December" "May" "August" "April" "October" "August"
[43] "November" "September" "February" "March" "July" "December"
[49] "March" "September" "December" "May" "November" "January"
[55] "March" "November" "May" "October" "February" "March"

Example

 Live Demo

x6<−sample(month.name,61,replace=TRUE)
x6

Output

[1] "February" "June" "March" "March" "August" "November"
[7] "November" "March" "December" "September" "November" "January"
[13] "June" "February" "February" "July" "July" "June"
[19] "July" "March" "September" "July" "November" "October"
[25] "February" "October" "February" "January" "April" "November"
[31] "September" "August" "October" "July" "May" "November"
[37] "March" "April" "July" "February" "September" "July"
[43] "March" "January" "November" "August" "January" "September"
[49] "April" "September" "December" "March" "July" "January"
[55] "February" "March" "August" "December" "October" "June"
[61] "June"

Example

 Live Demo

x7<−sample(month.name,90,replace=TRUE)
x7

Output

[1] "January" "June" "February" "November" "April" "February"
[7] "December" "November" "July" "July" "October" "May"
[13] "November" "May" "September" "June" "November" "December"
[19] "February" "July" "March" "June" "March" "March"
[25] "December" "June" "May" "November" "October" "May"
[31] "May" "November" "August" "November" "September" "May"
[37] "May" "April" "February" "September" "July" "March"
[43] "September" "March" "January" "September" "December" "October"
[49] "December" "April" "February" "April" "January" "December"
[55] "September" "February" "September" "October" "December" "April"
[61] "June" "March" "October" "September" "October" "January"
[67] "July" "September" "March" "February" "May" "August"
[73] "April" "August" "January" "December" "September" "October"
[79] "March" "January" "May" "August" "February" "July"
[85] "February" "February" "December" "July" "March" "December"

Example

 Live Demo

x8<−sample(month.name,92,replace=TRUE)
x8

Output

[1] "February" "December" "October" "July" "June" "November"
[7] "July" "October" "December" "March" "April" "July"
[13] "April" "February" "January" "July" "June" "February"
[19] "January" "October" "February" "November" "September" "October"
[25] "March" "September" "November" "September" "June" "November"
[31] "July" "April" "February" "February" "March" "November"
[37] "September" "December" "April" "April" "March" "February"
[43] "July" "February" "November" "August" "July" "October"
[49] "April" "February" "June" "July" "March" "August"
[55] "March" "December" "May" "November" "November" "February"
[61] "November" "October" "September" "July" "July" "May"
[67] "January" "December" "April" "January" "November" "May"
[73] "October" "September" "December" "June" "January" "December"
[79] "March" "February" "December" "December" "February" "December"
[85] "October" "July" "June" "January" "October" "June"
[91] "August" "October"
raja
Published on 06-Nov-2020 17:33:02
Advertisements