Translate

Saturday 26 November 2016

18 random numbers Functions in R language

random numbers
runif(1,0,10)
x<-matrix(100,0,1),ncol=10)
---
set.seed(2)
sample(1:10,5)
sample(x,5)
sample(0:1,100)
sample(0:1,100,replace=T)
sum(sample(0:1,100,replace=T))

state.name
sample(state.name,10)
--
s<-sample(1:5,1000,replace=T,prob=c(.2,.2,.2,.2,.2))
9059868766
venkat
table(s)
---
rnom(1)
-------------

R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

[Workspace loaded from ~/.RData]

> runif(1,1,10)
[1] 8.438811
> runif(1,1,10)
[1] 9.455057
> a= runif(1,1,10)
> a
[1] 1.533825
> a
[1] 1.533825
> a
[1] 1.533825
> a
[1] 1.533825
> a= runif(1,1,10)
> a
[1] 9.062645
> runif(4,1,10)
[1] 9.684915 7.945072 4.608548 8.766813
> runif(5,5,7)
[1] 6.302855 6.136968 6.119193 5.587079 6.033400
> an=matrix(runif(100,1,100),ncol=10)
> an
          [,1]     [,2]      [,3]     [,4]
 [1,] 17.75432 64.42554 33.544375 15.82550
 [2,] 85.59875 94.39768 63.713225 88.41076
 [3,] 83.64417 18.05964  2.511536 70.60304
 [4,] 78.92116 67.10841 64.811331 59.84145
 [5,] 89.59182 25.41236 69.217753 32.94199
 [6,] 97.07366 22.53897 77.627904 98.35555
 [7,] 16.68177 47.42317 51.392915 65.47258
 [8,] 38.17343 82.76507 60.983603 55.72940
 [9,] 19.47498 29.37258 31.161065 84.21841
[10,] 31.23285 74.66430 83.368968 29.16913
           [,5]     [,6]     [,7]     [,8]
 [1,] 86.260574 80.63066 30.94873 76.67941
 [2,] 61.753046 96.18675 20.12761 31.23936
 [3,] 21.795530 23.98979 33.31106 76.12806
 [4,] 24.627176 94.27216 34.70041 25.61517
 [5,]  7.915749 47.21915 37.51370 18.30664
 [6,] 87.660562 28.54266 66.19067 86.56617
 [7,]  8.286705 46.97633 11.86618 14.72213
 [8,]  4.057239 41.99362 27.68719 37.26553
 [9,] 30.313130 39.97871 61.40631 55.42384
[10,] 81.674761 13.22212 49.58386 76.71307
           [,9]    [,10]
 [1,] 98.641094 44.12686
 [2,] 45.728991 63.06460
 [3,] 52.971145 43.99829
 [4,] 59.973548 31.15030
 [5,] 34.073597 39.13850
 [6,]  1.354758 74.34744
 [7,] 41.567044 14.93894
 [8,] 56.111451 26.84879
 [9,] 55.458226 24.62272
[10,] 48.216264 34.75815
> runif(4,1,10)
[1] 3.045298 3.537711 1.005564 4.479743
> runif(4,1,10)
[1] 1.405773 2.423582 9.293291 9.501379
> set.seed(1)
> runif(4,1,10)
[1] 3.389578 4.349115 6.155680 9.173870
> runif(4,1,10)
[1] 2.815137 9.085507 9.502077 6.947180
> set.seed(1)
> runif(4,1,10)
[1] 3.389578 4.349115 6.155680 9.173870
> runif(4,1,10)
[1] 2.815137 9.085507 9.502077 6.947180
> runif(4,1,10)
[1] 6.662026 1.556076 2.853771 2.589011
> runif(4,1,10)
[1] 7.183206 4.456933 7.928573 5.479293
> set.seed(2)
> runif(4,1,10)
[1] 2.663940 7.321366 6.159937 2.512467
> runif(4,1,10)
[1] 9.494554 9.491275 2.162431 8.501039
> runif(4,1,10)
[1] 5.212167 5.949854 5.974067 3.150053
> set.seed(2)
> runif(4,1,10)
[1] 2.663940 7.321366 6.159937 2.512467
> runif(4,1,10)
[1] 9.494554 9.491275 2.162431 8.501039
> runif(4,1,10)
[1] 5.212167 5.949854 5.974067 3.150053
> sample(1:10,5)
[1] 8 2 4 6 7
> sample(1:60,5)
[1] 14 27  5 38 22
> sample(1:60,20)
 [1] 51  9 21 28 59 20 52  8  1 56 41 43 25 30 39
[16] 13 47  7 42 45
> sample(1:60,5)
[1]  7 10 55 46 58
> x<-1:60
> x
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
[16] 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
[31] 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
[46] 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
> sample(x,5)
[1] 21 30 48  1 57
> sample(0:1,5)
Error in sample.int(length(x), size, replace, prob) :
  cannot take a sample larger than the population when 'replace = FALSE'
> sample(0:2,5)
Error in sample.int(length(x), size, replace, prob) :
  cannot take a sample larger than the population when 'replace = FALSE'
> sample(0:2,5,replace=T)
[1] 2 2 0 2 2
> sample(0:2,5,replace=T)
[1] 2 1 2 2 2
> sample(0:2,5,replace=F)
Error in sample.int(length(x), size, replace, prob) :
  cannot take a sample larger than the population when 'replace = FALSE'
> sample(0:1,100,replace=T)
  [1] 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 0
 [23] 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 1 1 0 1
 [45] 1 0 0 0 0 1 0 1 1 1 1 0 1 1 0 0 1 1 0 1 1 0
 [67] 1 0 0 1 0 1 0 1 1 1 0 0 0 0 1 0 0 1 1 0 0 0
 [89] 0 0 1 0 1 0 1 0 0 0 1 1
> sum(sample(0:1,100,replace=T))
[1] 48
> table(sample(0:1,100,replace=T))

 0  1
51 49
> table(sample(0:1,100,replace=T))

 0  1
45 55
> table(sample(0:1,100,replace=T))

 0  1
47 53
> table(sample(0:1,100,replace=T))

 0  1
49 51
> state.name
 [1] "Alabama"        "Alaska"      
 [3] "Arizona"        "Arkansas"    
 [5] "California"     "Colorado"    
 [7] "Connecticut"    "Delaware"    
 [9] "Florida"        "Georgia"     
[11] "Hawaii"         "Idaho"       
[13] "Illinois"       "Indiana"     
[15] "Iowa"           "Kansas"      
[17] "Kentucky"       "Louisiana"   
[19] "Maine"          "Maryland"    
[21] "Massachusetts"  "Michigan"    
[23] "Minnesota"      "Mississippi" 
[25] "Missouri"       "Montana"     
[27] "Nebraska"       "Nevada"      
[29] "New Hampshire"  "New Jersey"  
[31] "New Mexico"     "New York"    
[33] "North Carolina" "North Dakota"
[35] "Ohio"           "Oklahoma"    
[37] "Oregon"         "Pennsylvania"
[39] "Rhode Island"   "South Carolina"
[41] "South Dakota"   "Tennessee"   
[43] "Texas"          "Utah"        
[45] "Vermont"        "Virginia"    
[47] "Washington"     "West Virginia"
[49] "Wisconsin"      "Wyoming"     
> sample(state.name,5)
[1] "Mississippi"   "Virginia"   
[3] "Massachusetts" "Alaska"     
[5] "Oklahoma"   
> sample(0:1,100,replace=T)
  [1] 0 1 0 0 0 1 0 0 1 1 1 1 0 1 0 1 0 0 0 1 1 1
 [23] 1 0 0 0 0 1 0 1 1 1 1 1 1 1 1 0 0 1 0 1 0 1
 [45] 0 0 0 1 0 1 1 1 0 1 1 1 0 0 1 0 1 0 0 0 1 0
 [67] 1 1 0 0 1 1 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1
 [89] 0 1 0 1 1 0 0 0 1 1 1 0
> sample(0:5,100,replace=T)
  [1] 2 5 3 0 1 3 2 2 0 5 4 4 2 3 2 5 3 5 0 4 0 2
 [23] 1 3 4 0 0 5 1 3 4 4 4 2 3 0 1 0 5 1 3 4 1 2
 [45] 2 1 5 0 2 5 3 3 5 3 0 0 0 3 0 0 0 2 4 0 0 5
 [67] 3 0 3 5 5 3 3 4 3 0 0 5 5 4 0 5 2 4 0 4 0 4
 [89] 1 0 5 3 2 0 2 2 5 0 5 5
> table(sample(0:5,100,replace=T))

 0  1  2  3  4  5
19 16 17 21 10 17
> sample(0:5,100,replace=T,prob=c(.2,.2,.2,.2,.2))
Error in sample.int(length(x), size, replace, prob) :
  incorrect number of probabilities
> sample(1:5,100,replace=T,prob=c(.2,.2,.2,.2,.2))
  [1] 1 1 3 2 4 2 5 4 5 2 1 2 5 1 2 4 4 1 4 2 5 5
 [23] 4 4 1 2 3 2 3 4 1 4 2 3 2 5 4 3 2 3 1 4 3 1
 [45] 3 3 3 1 2 2 2 1 2 2 5 4 3 2 5 1 3 5 1 5 3 1
 [67] 1 5 5 4 2 5 2 4 5 5 5 5 4 4 1 4 1 5 3 1 5 4
 [89] 2 1 4 4 2 2 2 3 3 1 4 1
> a=sample(1:5,100,replace=T,prob=c(.2,.2,.2,.2,.2))
> table(a)
a
 1  2  3  4  5
31 17 22 15 15
> table(a)
a
 1  2  3  4  5
31 17 22 15 15
> a=sample(1:5,100,replace=T,prob=c(.2,.2,.2,.2,.2))
> a
  [1] 2 4 2 3 5 2 5 3 3 4 3 1 4 3 2 5 4 2 1 4 1 4
 [23] 1 2 3 5 4 4 4 2 4 2 2 2 3 4 4 2 4 3 5 1 3 1
 [45] 1 4 1 1 1 5 3 2 3 3 1 2 1 5 5 5 5 4 4 4 5 1
 [67] 2 2 1 3 5 5 4 3 5 5 5 4 1 4 3 1 2 5 1 3 2 2
 [89] 5 1 5 2 1 4 1 5 2 2 1 1
> table(a)
a
 1  2  3  4  5
22 21 16 21 20
> a=sample(1:5,100,replace=T,prob=c(.6,.1,.1,.1,.1))
> a
  [1] 1 1 5 1 1 2 1 1 4 1 1 5 3 1 1 1 1 1 1 1 1 1
 [23] 1 4 3 1 5 3 2 1 1 1 5 2 1 2 1 3 2 1 1 2 2 1
 [45] 1 1 3 1 2 1 1 1 1 1 5 2 1 2 4 1 1 5 1 1 2 4
 [67] 1 1 1 1 1 5 1 1 1 3 1 1 3 2 3 1 1 1 1 1 1 1
 [89] 1 1 1 1 1 1 1 1 1 1 1 1
> table(a)
a
 1  2  3  4  5
69 12  8  4  7
> a=sample(1:5,100,replace=T,prob=c(.2,.4,.1,.1,.1))
> table(a)
a
 1  2  3  4  5
23 41  7 15 14



No comments:

Post a Comment

Note: only a member of this blog may post a comment.