home

Blog

Here’s a cool graph on the US census data

Run R

census = read.csv('/usr/share/data/kaggle/census.csv')
#loops
#Count to ten
for(i in 1:100){
  x = sqrt(i)
  print(x)
  }
## [1] 1
## [1] 1.414214
## [1] 1.732051
## [1] 2
## [1] 2.236068
## [1] 2.44949
## [1] 2.645751
## [1] 2.828427
## [1] 3
## [1] 3.162278
## [1] 3.316625
## [1] 3.464102
## [1] 3.605551
## [1] 3.741657
## [1] 3.872983
## [1] 4
## [1] 4.123106
## [1] 4.242641
## [1] 4.358899
## [1] 4.472136
## [1] 4.582576
## [1] 4.690416
## [1] 4.795832
## [1] 4.898979
## [1] 5
## [1] 5.09902
## [1] 5.196152
## [1] 5.291503
## [1] 5.385165
## [1] 5.477226
## [1] 5.567764
## [1] 5.656854
## [1] 5.744563
## [1] 5.830952
## [1] 5.91608
## [1] 6
## [1] 6.082763
## [1] 6.164414
## [1] 6.244998
## [1] 6.324555
## [1] 6.403124
## [1] 6.480741
## [1] 6.557439
## [1] 6.63325
## [1] 6.708204
## [1] 6.78233
## [1] 6.855655
## [1] 6.928203
## [1] 7
## [1] 7.071068
## [1] 7.141428
## [1] 7.211103
## [1] 7.28011
## [1] 7.348469
## [1] 7.416198
## [1] 7.483315
## [1] 7.549834
## [1] 7.615773
## [1] 7.681146
## [1] 7.745967
## [1] 7.81025
## [1] 7.874008
## [1] 7.937254
## [1] 8
## [1] 8.062258
## [1] 8.124038
## [1] 8.185353
## [1] 8.246211
## [1] 8.306624
## [1] 8.3666
## [1] 8.42615
## [1] 8.485281
## [1] 8.544004
## [1] 8.602325
## [1] 8.660254
## [1] 8.717798
## [1] 8.774964
## [1] 8.831761
## [1] 8.888194
## [1] 8.944272
## [1] 9
## [1] 9.055385
## [1] 9.110434
## [1] 9.165151
## [1] 9.219544
## [1] 9.273618
## [1] 9.327379
## [1] 9.380832
## [1] 9.433981
## [1] 9.486833
## [1] 9.539392
## [1] 9.591663
## [1] 9.643651
## [1] 9.69536
## [1] 9.746794
## [1] 9.797959
## [1] 9.848858
## [1] 9.899495
## [1] 9.949874
## [1] 10
sample1=sample(1:1000,10, replace=F)
sample2=sample(1:1000,10, replace=F)
sample3=sample(1:1000,10, replace=F)
sample4=sample(1:1000,10, replace=F)
sample5=sample(1:1000,10, replace=F)
sam.mat = cbind(sample1, sample2, sample3, sample4, sample5)
head(sam.mat)
##      sample1 sample2 sample3 sample4 sample5
## [1,]     115     575     134     938     648
## [2,]     262     224     182     770     836
## [3,]     431     609     469     375     934
## [4,]     552     530     132       4     767
## [5,]     987     626     308     323      67
## [6,]     250     976     201     581     178
#loops
x = 100; #number to sample
samples=100
sam.mat=matrix(nrow=x, ncol=samples)
for(i in 1000:0){
  
}


#Apply
s=c(1:1000)
for(i in 1:length(s)){
  print(sqrt(s[i]))
}
## [1] 1
## [1] 1.414214
## [1] 1.732051
## [1] 2
## [1] 2.236068
## [1] 2.44949
## [1] 2.645751
## [1] 2.828427
## [1] 3
## [1] 3.162278
## [1] 3.316625
## [1] 3.464102
## [1] 3.605551
## [1] 3.741657
## [1] 3.872983
## [1] 4
## [1] 4.123106
## [1] 4.242641
## [1] 4.358899
## [1] 4.472136
## [1] 4.582576
## [1] 4.690416
## [1] 4.795832
## [1] 4.898979
## [1] 5
## [1] 5.09902
## [1] 5.196152
## [1] 5.291503
## [1] 5.385165
## [1] 5.477226
## [1] 5.567764
## [1] 5.656854
## [1] 5.744563
## [1] 5.830952
## [1] 5.91608
## [1] 6
## [1] 6.082763
## [1] 6.164414
## [1] 6.244998
## [1] 6.324555
## [1] 6.403124
## [1] 6.480741
## [1] 6.557439
## [1] 6.63325
## [1] 6.708204
## [1] 6.78233
## [1] 6.855655
## [1] 6.928203
## [1] 7
## [1] 7.071068
## [1] 7.141428
## [1] 7.211103
## [1] 7.28011
## [1] 7.348469
## [1] 7.416198
## [1] 7.483315
## [1] 7.549834
## [1] 7.615773
## [1] 7.681146
## [1] 7.745967
## [1] 7.81025
## [1] 7.874008
## [1] 7.937254
## [1] 8
## [1] 8.062258
## [1] 8.124038
## [1] 8.185353
## [1] 8.246211
## [1] 8.306624
## [1] 8.3666
## [1] 8.42615
## [1] 8.485281
## [1] 8.544004
## [1] 8.602325
## [1] 8.660254
## [1] 8.717798
## [1] 8.774964
## [1] 8.831761
## [1] 8.888194
## [1] 8.944272
## [1] 9
## [1] 9.055385
## [1] 9.110434
## [1] 9.165151
## [1] 9.219544
## [1] 9.273618
## [1] 9.327379
## [1] 9.380832
## [1] 9.433981
## [1] 9.486833
## [1] 9.539392
## [1] 9.591663
## [1] 9.643651
## [1] 9.69536
## [1] 9.746794
## [1] 9.797959
## [1] 9.848858
## [1] 9.899495
## [1] 9.949874
## [1] 10
## [1] 10.04988
## [1] 10.0995
## [1] 10.14889
## [1] 10.19804
## [1] 10.24695
## [1] 10.29563
## [1] 10.34408
## [1] 10.3923
## [1] 10.44031
## [1] 10.48809
## [1] 10.53565
## [1] 10.58301
## [1] 10.63015
## [1] 10.67708
## [1] 10.72381
## [1] 10.77033
## [1] 10.81665
## [1] 10.86278
## [1] 10.90871
## [1] 10.95445
## [1] 11
## [1] 11.04536
## [1] 11.09054
## [1] 11.13553
## [1] 11.18034
## [1] 11.22497
## [1] 11.26943
## [1] 11.31371
## [1] 11.35782
## [1] 11.40175
## [1] 11.44552
## [1] 11.48913
## [1] 11.53256
## [1] 11.57584
## [1] 11.61895
## [1] 11.6619
## [1] 11.7047
## [1] 11.74734
## [1] 11.78983
## [1] 11.83216
## [1] 11.87434
## [1] 11.91638
## [1] 11.95826
## [1] 12
## [1] 12.04159
## [1] 12.08305
## [1] 12.12436
## [1] 12.16553
## [1] 12.20656
## [1] 12.24745
## [1] 12.28821
## [1] 12.32883
## [1] 12.36932
## [1] 12.40967
## [1] 12.4499
## [1] 12.49
## [1] 12.52996
## [1] 12.56981
## [1] 12.60952
## [1] 12.64911
## [1] 12.68858
## [1] 12.72792
## [1] 12.76715
## [1] 12.80625
## [1] 12.84523
## [1] 12.8841
## [1] 12.92285
## [1] 12.96148
## [1] 13
## [1] 13.0384
## [1] 13.0767
## [1] 13.11488
## [1] 13.15295
## [1] 13.19091
## [1] 13.22876
## [1] 13.2665
## [1] 13.30413
## [1] 13.34166
## [1] 13.37909
## [1] 13.41641
## [1] 13.45362
## [1] 13.49074
## [1] 13.52775
## [1] 13.56466
## [1] 13.60147
## [1] 13.63818
## [1] 13.67479
## [1] 13.71131
## [1] 13.74773
## [1] 13.78405
## [1] 13.82027
## [1] 13.85641
## [1] 13.89244
## [1] 13.92839
## [1] 13.96424
## [1] 14
## [1] 14.03567
## [1] 14.07125
## [1] 14.10674
## [1] 14.14214
## [1] 14.17745
## [1] 14.21267
## [1] 14.24781
## [1] 14.28286
## [1] 14.31782
## [1] 14.3527
## [1] 14.38749
## [1] 14.42221
## [1] 14.45683
## [1] 14.49138
## [1] 14.52584
## [1] 14.56022
## [1] 14.59452
## [1] 14.62874
## [1] 14.66288
## [1] 14.69694
## [1] 14.73092
## [1] 14.76482
## [1] 14.79865
## [1] 14.8324
## [1] 14.86607
## [1] 14.89966
## [1] 14.93318
## [1] 14.96663
## [1] 15
## [1] 15.0333
## [1] 15.06652
## [1] 15.09967
## [1] 15.13275
## [1] 15.16575
## [1] 15.19868
## [1] 15.23155
## [1] 15.26434
## [1] 15.29706
## [1] 15.32971
## [1] 15.36229
## [1] 15.3948
## [1] 15.42725
## [1] 15.45962
## [1] 15.49193
## [1] 15.52417
## [1] 15.55635
## [1] 15.58846
## [1] 15.6205
## [1] 15.65248
## [1] 15.68439
## [1] 15.71623
## [1] 15.74802
## [1] 15.77973
## [1] 15.81139
## [1] 15.84298
## [1] 15.87451
## [1] 15.90597
## [1] 15.93738
## [1] 15.96872
## [1] 16
## [1] 16.03122
## [1] 16.06238
## [1] 16.09348
## [1] 16.12452
## [1] 16.15549
## [1] 16.18641
## [1] 16.21727
## [1] 16.24808
## [1] 16.27882
## [1] 16.30951
## [1] 16.34013
## [1] 16.37071
## [1] 16.40122
## [1] 16.43168
## [1] 16.46208
## [1] 16.49242
## [1] 16.52271
## [1] 16.55295
## [1] 16.58312
## [1] 16.61325
## [1] 16.64332
## [1] 16.67333
## [1] 16.70329
## [1] 16.7332
## [1] 16.76305
## [1] 16.79286
## [1] 16.8226
## [1] 16.8523
## [1] 16.88194
## [1] 16.91153
## [1] 16.94107
## [1] 16.97056
## [1] 17
## [1] 17.02939
## [1] 17.05872
## [1] 17.08801
## [1] 17.11724
## [1] 17.14643
## [1] 17.17556
## [1] 17.20465
## [1] 17.23369
## [1] 17.26268
## [1] 17.29162
## [1] 17.32051
## [1] 17.34935
## [1] 17.37815
## [1] 17.4069
## [1] 17.4356
## [1] 17.46425
## [1] 17.49286
## [1] 17.52142
## [1] 17.54993
## [1] 17.5784
## [1] 17.60682
## [1] 17.63519
## [1] 17.66352
## [1] 17.69181
## [1] 17.72005
## [1] 17.74824
## [1] 17.77639
## [1] 17.80449
## [1] 17.83255
## [1] 17.86057
## [1] 17.88854
## [1] 17.91647
## [1] 17.94436
## [1] 17.9722
## [1] 18
## [1] 18.02776
## [1] 18.05547
## [1] 18.08314
## [1] 18.11077
## [1] 18.13836
## [1] 18.1659
## [1] 18.19341
## [1] 18.22087
## [1] 18.24829
## [1] 18.27567
## [1] 18.30301
## [1] 18.3303
## [1] 18.35756
## [1] 18.38478
## [1] 18.41195
## [1] 18.43909
## [1] 18.46619
## [1] 18.49324
## [1] 18.52026
## [1] 18.54724
## [1] 18.57418
## [1] 18.60108
## [1] 18.62794
## [1] 18.65476
## [1] 18.68154
## [1] 18.70829
## [1] 18.73499
## [1] 18.76166
## [1] 18.78829
## [1] 18.81489
## [1] 18.84144
## [1] 18.86796
## [1] 18.89444
## [1] 18.92089
## [1] 18.9473
## [1] 18.97367
## [1] 19
## [1] 19.0263
## [1] 19.05256
## [1] 19.07878
## [1] 19.10497
## [1] 19.13113
## [1] 19.15724
## [1] 19.18333
## [1] 19.20937
## [1] 19.23538
## [1] 19.26136
## [1] 19.2873
## [1] 19.31321
## [1] 19.33908
## [1] 19.36492
## [1] 19.39072
## [1] 19.41649
## [1] 19.44222
## [1] 19.46792
## [1] 19.49359
## [1] 19.51922
## [1] 19.54482
## [1] 19.57039
## [1] 19.59592
## [1] 19.62142
## [1] 19.64688
## [1] 19.67232
## [1] 19.69772
## [1] 19.72308
## [1] 19.74842
## [1] 19.77372
## [1] 19.79899
## [1] 19.82423
## [1] 19.84943
## [1] 19.87461
## [1] 19.89975
## [1] 19.92486
## [1] 19.94994
## [1] 19.97498
## [1] 20
## [1] 20.02498
## [1] 20.04994
## [1] 20.07486
## [1] 20.09975
## [1] 20.12461
## [1] 20.14944
## [1] 20.17424
## [1] 20.19901
## [1] 20.22375
## [1] 20.24846
## [1] 20.27313
## [1] 20.29778
## [1] 20.3224
## [1] 20.34699
## [1] 20.37155
## [1] 20.39608
## [1] 20.42058
## [1] 20.44505
## [1] 20.46949
## [1] 20.4939
## [1] 20.51828
## [1] 20.54264
## [1] 20.56696
## [1] 20.59126
## [1] 20.61553
## [1] 20.63977
## [1] 20.66398
## [1] 20.68816
## [1] 20.71232
## [1] 20.73644
## [1] 20.76054
## [1] 20.78461
## [1] 20.80865
## [1] 20.83267
## [1] 20.85665
## [1] 20.88061
## [1] 20.90454
## [1] 20.92845
## [1] 20.95233
## [1] 20.97618
## [1] 21
## [1] 21.0238
## [1] 21.04757
## [1] 21.07131
## [1] 21.09502
## [1] 21.11871
## [1] 21.14237
## [1] 21.16601
## [1] 21.18962
## [1] 21.2132
## [1] 21.23676
## [1] 21.26029
## [1] 21.2838
## [1] 21.30728
## [1] 21.33073
## [1] 21.35416
## [1] 21.37756
## [1] 21.40093
## [1] 21.42429
## [1] 21.44761
## [1] 21.47091
## [1] 21.49419
## [1] 21.51743
## [1] 21.54066
## [1] 21.56386
## [1] 21.58703
## [1] 21.61018
## [1] 21.63331
## [1] 21.65641
## [1] 21.67948
## [1] 21.70253
## [1] 21.72556
## [1] 21.74856
## [1] 21.77154
## [1] 21.79449
## [1] 21.81742
## [1] 21.84033
## [1] 21.86321
## [1] 21.88607
## [1] 21.9089
## [1] 21.93171
## [1] 21.9545
## [1] 21.97726
## [1] 22
## [1] 22.02272
## [1] 22.04541
## [1] 22.06808
## [1] 22.09072
## [1] 22.11334
## [1] 22.13594
## [1] 22.15852
## [1] 22.18107
## [1] 22.2036
## [1] 22.22611
## [1] 22.2486
## [1] 22.27106
## [1] 22.2935
## [1] 22.31591
## [1] 22.33831
## [1] 22.36068
## [1] 22.38303
## [1] 22.40536
## [1] 22.42766
## [1] 22.44994
## [1] 22.47221
## [1] 22.49444
## [1] 22.51666
## [1] 22.53886
## [1] 22.56103
## [1] 22.58318
## [1] 22.60531
## [1] 22.62742
## [1] 22.6495
## [1] 22.67157
## [1] 22.69361
## [1] 22.71563
## [1] 22.73763
## [1] 22.75961
## [1] 22.78157
## [1] 22.80351
## [1] 22.82542
## [1] 22.84732
## [1] 22.86919
## [1] 22.89105
## [1] 22.91288
## [1] 22.93469
## [1] 22.95648
## [1] 22.97825
## [1] 23
## [1] 23.02173
## [1] 23.04344
## [1] 23.06513
## [1] 23.08679
## [1] 23.10844
## [1] 23.13007
## [1] 23.15167
## [1] 23.17326
## [1] 23.19483
## [1] 23.21637
## [1] 23.2379
## [1] 23.25941
## [1] 23.28089
## [1] 23.30236
## [1] 23.32381
## [1] 23.34524
## [1] 23.36664
## [1] 23.38803
## [1] 23.4094
## [1] 23.43075
## [1] 23.45208
## [1] 23.47339
## [1] 23.49468
## [1] 23.51595
## [1] 23.5372
## [1] 23.55844
## [1] 23.57965
## [1] 23.60085
## [1] 23.62202
## [1] 23.64318
## [1] 23.66432
## [1] 23.68544
## [1] 23.70654
## [1] 23.72762
## [1] 23.74868
## [1] 23.76973
## [1] 23.79075
## [1] 23.81176
## [1] 23.83275
## [1] 23.85372
## [1] 23.87467
## [1] 23.89561
## [1] 23.91652
## [1] 23.93742
## [1] 23.9583
## [1] 23.97916
## [1] 24
## [1] 24.02082
## [1] 24.04163
## [1] 24.06242
## [1] 24.08319
## [1] 24.10394
## [1] 24.12468
## [1] 24.14539
## [1] 24.16609
## [1] 24.18677
## [1] 24.20744
## [1] 24.22808
## [1] 24.24871
## [1] 24.26932
## [1] 24.28992
## [1] 24.31049
## [1] 24.33105
## [1] 24.35159
## [1] 24.37212
## [1] 24.39262
## [1] 24.41311
## [1] 24.43358
## [1] 24.45404
## [1] 24.47448
## [1] 24.4949
## [1] 24.5153
## [1] 24.53569
## [1] 24.55606
## [1] 24.57641
## [1] 24.59675
## [1] 24.61707
## [1] 24.63737
## [1] 24.65766
## [1] 24.67793
## [1] 24.69818
## [1] 24.71841
## [1] 24.73863
## [1] 24.75884
## [1] 24.77902
## [1] 24.79919
## [1] 24.81935
## [1] 24.83948
## [1] 24.85961
## [1] 24.87971
## [1] 24.8998
## [1] 24.91987
## [1] 24.93993
## [1] 24.95997
## [1] 24.97999
## [1] 25
## [1] 25.01999
## [1] 25.03997
## [1] 25.05993
## [1] 25.07987
## [1] 25.0998
## [1] 25.11971
## [1] 25.13961
## [1] 25.15949
## [1] 25.17936
## [1] 25.19921
## [1] 25.21904
## [1] 25.23886
## [1] 25.25866
## [1] 25.27845
## [1] 25.29822
## [1] 25.31798
## [1] 25.33772
## [1] 25.35744
## [1] 25.37716
## [1] 25.39685
## [1] 25.41653
## [1] 25.43619
## [1] 25.45584
## [1] 25.47548
## [1] 25.4951
## [1] 25.5147
## [1] 25.53429
## [1] 25.55386
## [1] 25.57342
## [1] 25.59297
## [1] 25.6125
## [1] 25.63201
## [1] 25.65151
## [1] 25.671
## [1] 25.69047
## [1] 25.70992
## [1] 25.72936
## [1] 25.74879
## [1] 25.7682
## [1] 25.78759
## [1] 25.80698
## [1] 25.82634
## [1] 25.8457
## [1] 25.86503
## [1] 25.88436
## [1] 25.90367
## [1] 25.92296
## [1] 25.94224
## [1] 25.96151
## [1] 25.98076
## [1] 26
## [1] 26.01922
## [1] 26.03843
## [1] 26.05763
## [1] 26.07681
## [1] 26.09598
## [1] 26.11513
## [1] 26.13427
## [1] 26.15339
## [1] 26.1725
## [1] 26.1916
## [1] 26.21068
## [1] 26.22975
## [1] 26.24881
## [1] 26.26785
## [1] 26.28688
## [1] 26.30589
## [1] 26.32489
## [1] 26.34388
## [1] 26.36285
## [1] 26.38181
## [1] 26.40076
## [1] 26.41969
## [1] 26.43861
## [1] 26.45751
## [1] 26.4764
## [1] 26.49528
## [1] 26.51415
## [1] 26.533
## [1] 26.55184
## [1] 26.57066
## [1] 26.58947
## [1] 26.60827
## [1] 26.62705
## [1] 26.64583
## [1] 26.66458
## [1] 26.68333
## [1] 26.70206
## [1] 26.72078
## [1] 26.73948
## [1] 26.75818
## [1] 26.77686
## [1] 26.79552
## [1] 26.81418
## [1] 26.83282
## [1] 26.85144
## [1] 26.87006
## [1] 26.88866
## [1] 26.90725
## [1] 26.92582
## [1] 26.94439
## [1] 26.96294
## [1] 26.98148
## [1] 27
## [1] 27.01851
## [1] 27.03701
## [1] 27.0555
## [1] 27.07397
## [1] 27.09243
## [1] 27.11088
## [1] 27.12932
## [1] 27.14774
## [1] 27.16616
## [1] 27.18455
## [1] 27.20294
## [1] 27.22132
## [1] 27.23968
## [1] 27.25803
## [1] 27.27636
## [1] 27.29469
## [1] 27.313
## [1] 27.3313
## [1] 27.34959
## [1] 27.36786
## [1] 27.38613
## [1] 27.40438
## [1] 27.42262
## [1] 27.44085
## [1] 27.45906
## [1] 27.47726
## [1] 27.49545
## [1] 27.51363
## [1] 27.5318
## [1] 27.54995
## [1] 27.5681
## [1] 27.58623
## [1] 27.60435
## [1] 27.62245
## [1] 27.64055
## [1] 27.65863
## [1] 27.67671
## [1] 27.69476
## [1] 27.71281
## [1] 27.73085
## [1] 27.74887
## [1] 27.76689
## [1] 27.78489
## [1] 27.80288
## [1] 27.82086
## [1] 27.83882
## [1] 27.85678
## [1] 27.87472
## [1] 27.89265
## [1] 27.91057
## [1] 27.92848
## [1] 27.94638
## [1] 27.96426
## [1] 27.98214
## [1] 28
## [1] 28.01785
## [1] 28.03569
## [1] 28.05352
## [1] 28.07134
## [1] 28.08914
## [1] 28.10694
## [1] 28.12472
## [1] 28.14249
## [1] 28.16026
## [1] 28.17801
## [1] 28.19574
## [1] 28.21347
## [1] 28.23119
## [1] 28.24889
## [1] 28.26659
## [1] 28.28427
## [1] 28.30194
## [1] 28.3196
## [1] 28.33725
## [1] 28.35489
## [1] 28.37252
## [1] 28.39014
## [1] 28.40775
## [1] 28.42534
## [1] 28.44293
## [1] 28.4605
## [1] 28.47806
## [1] 28.49561
## [1] 28.51315
## [1] 28.53069
## [1] 28.5482
## [1] 28.56571
## [1] 28.58321
## [1] 28.6007
## [1] 28.61818
## [1] 28.63564
## [1] 28.6531
## [1] 28.67054
## [1] 28.68798
## [1] 28.7054
## [1] 28.72281
## [1] 28.74022
## [1] 28.75761
## [1] 28.77499
## [1] 28.79236
## [1] 28.80972
## [1] 28.82707
## [1] 28.84441
## [1] 28.86174
## [1] 28.87906
## [1] 28.89637
## [1] 28.91366
## [1] 28.93095
## [1] 28.94823
## [1] 28.9655
## [1] 28.98275
## [1] 29
## [1] 29.01724
## [1] 29.03446
## [1] 29.05168
## [1] 29.06888
## [1] 29.08608
## [1] 29.10326
## [1] 29.12044
## [1] 29.1376
## [1] 29.15476
## [1] 29.1719
## [1] 29.18904
## [1] 29.20616
## [1] 29.22328
## [1] 29.24038
## [1] 29.25748
## [1] 29.27456
## [1] 29.29164
## [1] 29.3087
## [1] 29.32576
## [1] 29.3428
## [1] 29.35984
## [1] 29.37686
## [1] 29.39388
## [1] 29.41088
## [1] 29.42788
## [1] 29.44486
## [1] 29.46184
## [1] 29.47881
## [1] 29.49576
## [1] 29.51271
## [1] 29.52965
## [1] 29.54657
## [1] 29.56349
## [1] 29.5804
## [1] 29.5973
## [1] 29.61419
## [1] 29.63106
## [1] 29.64793
## [1] 29.66479
## [1] 29.68164
## [1] 29.69848
## [1] 29.71532
## [1] 29.73214
## [1] 29.74895
## [1] 29.76575
## [1] 29.78255
## [1] 29.79933
## [1] 29.8161
## [1] 29.83287
## [1] 29.84962
## [1] 29.86637
## [1] 29.88311
## [1] 29.89983
## [1] 29.91655
## [1] 29.93326
## [1] 29.94996
## [1] 29.96665
## [1] 29.98333
## [1] 30
## [1] 30.01666
## [1] 30.03331
## [1] 30.04996
## [1] 30.06659
## [1] 30.08322
## [1] 30.09983
## [1] 30.11644
## [1] 30.13304
## [1] 30.14963
## [1] 30.16621
## [1] 30.18278
## [1] 30.19934
## [1] 30.21589
## [1] 30.23243
## [1] 30.24897
## [1] 30.26549
## [1] 30.28201
## [1] 30.29851
## [1] 30.31501
## [1] 30.3315
## [1] 30.34798
## [1] 30.36445
## [1] 30.38092
## [1] 30.39737
## [1] 30.41381
## [1] 30.43025
## [1] 30.44667
## [1] 30.46309
## [1] 30.4795
## [1] 30.4959
## [1] 30.51229
## [1] 30.52868
## [1] 30.54505
## [1] 30.56141
## [1] 30.57777
## [1] 30.59412
## [1] 30.61046
## [1] 30.62679
## [1] 30.64311
## [1] 30.65942
## [1] 30.67572
## [1] 30.69202
## [1] 30.70831
## [1] 30.72458
## [1] 30.74085
## [1] 30.75711
## [1] 30.77337
## [1] 30.78961
## [1] 30.80584
## [1] 30.82207
## [1] 30.83829
## [1] 30.8545
## [1] 30.8707
## [1] 30.88689
## [1] 30.90307
## [1] 30.91925
## [1] 30.93542
## [1] 30.95158
## [1] 30.96773
## [1] 30.98387
## [1] 31
## [1] 31.01612
## [1] 31.03224
## [1] 31.04835
## [1] 31.06445
## [1] 31.08054
## [1] 31.09662
## [1] 31.1127
## [1] 31.12876
## [1] 31.14482
## [1] 31.16087
## [1] 31.17691
## [1] 31.19295
## [1] 31.20897
## [1] 31.22499
## [1] 31.241
## [1] 31.257
## [1] 31.27299
## [1] 31.28898
## [1] 31.30495
## [1] 31.32092
## [1] 31.33688
## [1] 31.35283
## [1] 31.36877
## [1] 31.38471
## [1] 31.40064
## [1] 31.41656
## [1] 31.43247
## [1] 31.44837
## [1] 31.46427
## [1] 31.48015
## [1] 31.49603
## [1] 31.5119
## [1] 31.52777
## [1] 31.54362
## [1] 31.55947
## [1] 31.57531
## [1] 31.59114
## [1] 31.60696
## [1] 31.62278
#Better way to do this:
a = sapply(s, sqrt)
#lapply: Just makes things in a list!
la = lapply(s, sqrt)

#More Apply

m = matrix(nrow=length(s), ncol=5)
for(i in 1:ncol(m)){
  m[,i]=s
}
head(m)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    1    1    1    1
## [2,]    2    2    2    2    2
## [3,]    3    3    3    3    3
## [4,]    4    4    4    4    4
## [5,]    5    5    5    5    5
## [6,]    6    6    6    6    6
ap = apply(m, 1, max)

#Parallel Processing
require(parallel)
## Loading required package: parallel
do = seq(1, 10000000)
p = proc.time()
l_works = sapply(do, sqrt)
proc.time() - p
##    user  system elapsed 
##   8.853   0.244   9.108
# WOW look how long that took!

nclus = 4
cl = makeCluster(nclus, type ='SOCK'); 
p = proc.time()
splits = clusterSplit(cl, do)
p_works2 = parSapply(cl, splits, sqrt)
proc.time() - p
##    user  system elapsed 
##   1.805   0.327   2.137
stopCluster(cl)
#That was intense! Look how fast it was, it took almost 1/4 the amount of time

nclus = 32
cl = makeCluster(nclus, type ='SOCK'); 
p = proc.time()
splits = clusterSplit(cl, do)
p_works2 = parSapply(cl, splits, sqrt)
proc.time() - p
##    user  system elapsed 
##   1.120   0.123   1.261
stopCluster(cl)
# That took pretty much the same time because 1.1 seconds is practically the amount of time it takes for R to read the code and communicate witht the server. 

#Conditional statements
citizenRatio = census$Citizen/census$TotalPop
for(i in 1:nrow(census)){
  census[1,'Citzen']/census[i,'TotalPop']
}

AZcitizenRatio = vector()
CAcitizenRatio = vector()
otherRatio=vector()
for(i in 1:nrow(census)){
  if(census[i,'State']=='Arizona'){
    AZcitizenRatio[i]=census[i,'Citizen']/census[i,'TotalPop']
  } else if (census[i,'State']=='California') {
    CAcitizenRatio[i]=census[i,'Citizen']/census[i,'TotalPop']
  } else {
    otherRatio[i] = census[i,'Citizen']/census[i,'TotalPop']
  }
}
summary(AZcitizenRatio)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##  0.1992  0.6194  0.7060  0.6918  0.7768  1.0000    1354
summary(CAcitizenRatio)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   0.140   0.558   0.665   0.642   0.742   1.000    3605
summary(otherRatio)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   0.000   0.683   0.742   0.724   0.784   1.000   10222
#Defining Functions
citratio <- function(x){
  ratio = x$Citizen/x$TotalPop
  med = median(ratio, na.rm = TRUE)
  return(med)
}

citratio(census)
## [1] 0.7366694
citratio(census[census$State=='California',])
## [1] 0.6645757
#Homework question


t = as.vector(10) #years
n = 2 #Initial Pop
K = 1000 #Capacity
r = .2 #Rate

pop = vector()
for(i in 1:1000){
  pop[i] = K/(1+((K-n)/n)*exp(-r*i))
}
plot(pop)

I tried to model population growth, so I used the logistic growth equation. This equation takes into account resources as well as immigration and emigration, although it is lumped into the variable ‘r’ (r = growth rate). This model could be improved if I wrote my own function for the variable r. I tried to set that up but I was having trouble setting up the syntax of integrating two functions into a loop. I’m going to keep trying to get that working though!

home