How to create a random sample of week days in R?

R ProgrammingServer Side ProgrammingProgramming

To create a vector of weekdays we can use the command weekdays(Sys.Date()+0:6) and if we want to create a random sample of week days then sample function can be used along with the weekdays command. For example, if we want to create a random sample of 20 days then it can be done as sample(weekdays(Sys.Date()+0:6),20,replace=TRUE).

Examples

 Live Demo

Example1<−sample(weekdays(Sys.Date()+0:6),20,replace=TRUE)
Example1

Output

[1] "Friday" "Tuesday" "Saturday" "Monday" "Monday" "Sunday"
[7] "Monday" "Sunday" "Tuesday" "Sunday" "Friday" "Friday"
[13] "Wednesday" "Thursday" "Thursday" "Monday" "Sunday" "Tuesday"
[19] "Saturday" "Thursday"

Example

 Live Demo

Example2<−sample(weekdays(Sys.Date()+0:6),30,replace=TRUE)
Example2

Output

[1] "Monday" "Saturday" "Wednesday" "Saturday" "Saturday" "Sunday"
[7] "Saturday" "Saturday" "Wednesday" "Monday" "Sunday" "Tuesday"
[13] "Monday" "Sunday" "Tuesday" "Sunday" "Sunday" "Tuesday"
[19] "Saturday" "Friday" " Wednesday" "Monday" "Wednesday" "Wednesday"
[25] "Wednesday" "Monday" "Monday" "Monday" "Monday" "Thursday"

Example

 Live Demo

Example3<−sample(weekdays(Sys.Date()+0:6),7,replace=TRUE)
Example3

Output

[1] "Friday" "Thursday" "Thursday" "Saturday" "Sunday" "Sunday" "Friday"

Example

 Live Demo

Example4<−sample(weekdays(Sys.Date()+0:6),31,replace=TRUE)
Example4

Output

[1] "Tuesday" "Tuesday" "Wednesday" "Tuesday" "Thursday" "Sunday"
[7] "Friday" "Thursday" "Sunday" "Thursday" "Sunday" "Monday"
[13] "Friday" "Friday" "Tuesday" "Monday" "Saturday" "Friday"
[19] "Tuesday" "Friday" "Saturday" "Wednesday" "Friday" "Saturday"
[25] "Tuesday" "Friday" "Tuesday" "Sunday" "Sunday" "Wednesday"
[31] "Sunday"

Example

 Live Demo

Example5<−sample(weekdays(Sys.Date()+0:6),181,replace=TRUE)
Example5

Output

[1] "Wednesday" "Monday" "Monday" "Saturday" "Sunday" "Sunday"
[7] "Friday" "Wednesday" "Thursday" "Wednesday" "Saturday" "Sunday"
[13] "Saturday" "Sunday" "Friday" "Monday" "Tuesday" "Tuesday"
[19] "Saturday" "Friday" "Thursday" "Monday" "Thursday" "Monday"
[25] "Monday" "Tuesday" "Friday" "Tuesday" "Sunday" "Wednesday"
[31] "Wednesday" "Saturday" "Sunday" "Monday" "Thursday" "Friday"
[37] "Sunday" "Thursday" "Monday" "Sunday" "Wednesday" "Thursday"
[43] "Thursday" "Monday" "Wednesday" "Saturday" "Sunday" "Wednesday"
[49] "Tuesday" "Sunday" "Wednesday" "Sunday" "Monday" "Sunday"
[55] "Tuesday" "Sunday" "Saturday" "Tuesday" "Saturday" "Friday"
[61] "Tuesday" "Monday" "Sunday" "Monday" "Monday" "Wednesday"
[67] "Thursday" "Thursday" "Monday" "Friday" "Sunday" "Tuesday"
[73] "Friday" "Friday" "Tuesday" "Friday" "Saturday" "Tuesday"
[79] "Saturday" "Thursday" "Thursday" "Friday" "Monday" "Tuesday"
[85] "Friday" "Tuesday" "Friday" "Friday" "Saturday" "Monday"
[91] "Wednesday" "Sunday" "Saturday" "Monday" "Thursday" "Saturday"
[97] "Thursday" "Wednesday" "Tuesday" "Sunday" "Monday" "Saturday"
[103] "Monday" "Wednesday" "Tuesday" "Friday" "Tuesday" "Thursday"
[109] "Thursday" "Monday" "Thursday" "Saturday" "Tuesday" "Monday"
[115] "Friday" "Sunday" "Monday" "Saturday" "Tuesday" "Thursday"
[121] "Saturday" "Tuesday" "Saturday" "Thursday" "Wednesday" "Saturday"
[127] "Monday" "Monday" "Saturday" "Tuesday" "Tuesday" "Sunday"
[133] "Wednesday" "Sunday" "Sunday" "Saturday" "Friday" "Friday"
[139] "Friday" "Monday" "Sunday" "Saturday" "Monday" "Sunday"
[145] "Friday" "Wednesday" "Tuesday" "Monday" "Wednesday" "Sunday"
[151] "Thursday" "Monday" "Wednesday" "Sunday" "Thursday" "Wednesday"
[157] "Friday" "Monday" "Tuesday" "Sunday" "Thursday" "Wednesday"
[163] "Thursday" "Wednesday" "Friday" "Thursday" "Wednesday" "Sunday"
[169] "Monday" "Saturday" "Saturday" "Wednesday" "Thursday" "Thursday"
[175] "Tuesday" "Saturday" "Wednesday" "Friday" "Sunday" "Thursday"
[181] "Monday"

Example

 Live Demo

Example6<−sample(weekdays(Sys.Date()+0:6),365,replace=TRUE)
Example6

Output

[1] "Tuesday" "Saturday" "Friday" "Tuesday" "Saturday" "Thursday"
[7] "Tuesday" "Saturday" "Sunday" "Wednesday" "Sunday" "Monday"
[13] "Sunday" "Friday" "Sunday" "Wednesday" "Sunday" "Saturday"
[19] "Saturday" "Friday" "Sunday" "Friday" "Friday" "Monday"
[25] "Friday" "Thursday" "Wednesday" "Wednesday" "Thursday" "Tuesday"
[31] "Thursday" "Friday" "Thursday" "Wednesday" "Monday" "Wednesday"
[37] "Wednesday" "Sunday" "Monday" "Thursday" "Sunday" "Monday"
[43] "Friday" "Friday" "Friday" "Sunday" "Thursday" "Sunday"
[49] "Wednesday" "Saturday" "Monday" "Wednesday" "Tuesday" "Tuesday"
[55] "Tuesday" "Tuesday" "Wednesday" "Thursday" "Sunday" "Saturday"
[61] "Tuesday" "Wednesday" "Wednesday" "Monday" "Monday" "Thursday"
[67] "Wednesday" "Wednesday" "Thursday" "Tuesday" "Wednesday" "Sunday"
[73] "Tuesday" "Saturday" "Tuesday" "Wednesday" "Wednesday" "Friday"
[79] "Tuesday" "Thursday" "Thursday" "Saturday" "Monday" "Friday"
[85] "Sunday" "Sunday" "Sunday" "Monday" "Friday" "Thursday"
[91] "Sunday" "Wednesday" "Monday" "Monday" "Monday" "Wednesday"
[97] "Wednesday" "Thursday" "Friday" "Sunday" "Saturday" "Sunday"
[103] "Saturday" "Thursday" "Thursday" "Wednesday" "Thursday" "Thursday"
[109] "Monday" "Tuesday" "Wednesday" "Thursday" "Monday" "Thursday"
[115] "Tuesday" "Monday" "Monday" "Thursday" "Saturday" "Tuesday"
[121] "Friday" "Monday" "Thursday" "Tuesday" "Saturday" "Monday"
[127] "Saturday" "Sunday" "Tuesday" "Friday" "Thursday" "Saturday"
[133] "Saturday" "Saturday" "Wednesday" "Thursday" "Friday" "Thursday"
[139] "Sunday" "Thursday" "Saturday" "Friday" "Wednesday" "Friday"
[145] "Saturday" "Wednesday" "Tuesday" "Sunday" "Wednesday" "Thursday"
[151] "Sunday" "Sunday" "Sunday" "Friday" "Friday" "Monday"
[157] "Sunday" "Thursday" "Saturday" "Saturday" "Monday" "Tuesday"
[163] "Sunday" "Thursday" "Saturday" "Thursday" "Friday" "Tuesday"
[169] "Thursday" "Sunday" "Tuesday" "Monday" "Tuesday" "Sunday"
[175] "Friday" "Wednesday" "Wednesday" "Saturday" "Monday" "Tuesday"
[181] "Wednesday" "Friday" "Saturday" "Sunday" "Sunday" "Monday"
[187] "Saturday" "Thursday" "Wednesday" "Wednesday" "Tuesday" "Friday"
[193] "Wednesday" "Monday" "Wednesday" "Monday" "Saturday" "Monday"
[199] "Tuesday" "Tuesday" "Friday" "Monday" "Tuesday" "Monday"
[205] "Monday" "Wednesday" "Saturday" "Sunday" "Friday" "Wednesday"
[211] "Friday" "Wednesday" "Friday" "Thursday" "Tuesday" "Monday"
[217] "Sunday" "Saturday" "Sunday" "Wednesday" "Wednesday" "Thursday"
[223] "Thursday" "Sunday" "Wednesday" "Wednesday" "Tuesday" "Monday"
[229] "Friday" "Thursday" "Wednesday" "Saturday" "Thursday" "Friday"
[235] "Tuesday" "Saturday" "Saturday" "Tuesday" "Sunday" "Friday"
[241] "Thursday" "Thursday" "Tuesday" "Friday" "Sunday" "Tuesday"
[247] "Thursday" "Wednesday" "Friday" "Friday" "Sunday" "Monday"
[253] "Wednesday" "Thursday" "Monday" "Friday" "Tuesday" "Sunday"
[259] "Tuesday" "Monday" "Sunday" "Friday" "Monday" "Tuesday"
[265] "Thursday" "Sunday" "Sunday" "Sunday" "Saturday" "Friday"
[271] "Tuesday" "Friday" "Thursday" "Sunday" "Saturday" "Sunday"
[277] "Friday" "Friday" "Thursday" "Sunday" "Sunday" "Wednesday"
[283] "Thursday" "Sunday" "Tuesday" "Thursday" "Friday" "Tuesday"
[289] "Monday" "Monday" "Thursday" "Monday" "Tuesday" "Saturday"
[295] "Friday" "Monday" "Wednesday" "Tuesday" "Tuesday" "Sunday"
[301] "Thursday" "Wednesday" "Wednesday" "Saturday" "Saturday" "Saturday"
[307] "Tuesday" "Saturday" "Monday" "Thursday" "Monday" "Thursday"
[313] "Friday" "Saturday" "Tuesday" "Saturday" "Thursday" "Wednesday"
[319] "Thursday" "Wednesday" "Friday" "Friday" "Saturday" "Monday"
[325] "Monday" "Sunday" "Monday" "Wednesday" "Sunday" "Monday"
[331] "Friday" "Wednesday" "Sunday" "Saturday" "Sunday" "Wednesday"
[337] "Saturday" "Monday" "Tuesday" "Saturday" "Monday" "Sunday"
[343] "Monday" "Friday" "Friday" "Friday" "Friday" "Thursday"
[349] "Tuesday" "Sunday" "Sunday" "Monday" "Wednesday" "Tuesday"
[355] "Wednesday" "Wednesday" "Sunday" "Tuesday" "Tuesday" "Monday"
[361] "Thursday" "Thursday" "Sunday" "Tuesday" "Monday"
raja
Published on 05-Feb-2021 10:50:44
Advertisements