Generating vectors

Generating vectors

r studio,statistics

 With the (:) column operator Regular sequences of numbers can be very handy for all sorts of reasons. Such sequences can be generated in different ways. The easiest way is to use the column operator (:)
> index <- 1:25
> index
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
[23] 23 24 25
> index <- 25:1
> index
 [1] 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4
[23]  3  2  1

The sequence function seq()

 The function seq() together with its arguments from, to, by or length is used to generate more general sequences. Specify the beginning and end of the sequence and either specify the length of the sequence or the increment


> m <- seq (from = .7, to = 6,by = 0.5)
> m
 [1] 0.7 1.2 1.7 2.2 2.7 3.2 3.7 4.2 4.7 5.2 5.7
> n<-seq(from=.9,to=10,by=.10)
> n
 [1]  0.9  1.0  1.1  1.2  1.3  1.4  1.5  1.6  1.7  1.8  1.9  2.0  2.1
[14]  2.2  2.3  2.4  2.5  2.6  2.7  2.8  2.9  3.0  3.1  3.2  3.3  3.4
[27]  3.5  3.6  3.7  3.8  3.9  4.0  4.1  4.2  4.3  4.4  4.5  4.6  4.7
[40]  4.8  4.9  5.0  5.1  5.2  5.3  5.4  5.5  5.6  5.7  5.8  5.9  6.0
[53]  6.1  6.2  6.3  6.4  6.5  6.6  6.7  6.8  6.9  7.0  7.1  7.2  7.3
[66]  7.4  7.5  7.6  7.7  7.8  7.9  8.0  8.1  8.2  8.3  8.4  8.5  8.6
[79]  8.7  8.8  8.9  9.0  9.1  9.2  9.3  9.4  9.5  9.6  9.7  9.8  9.9
[92] 10.0
The following commands have the same result:
> l <-seq(.2,9,length = 14)
> l
 [1] 0.2000000 0.8769231 1.5538462 2.2307692 2.9076923 3.5846154
 [7] 4.2615385 4.9384615 5.6153846 6.2923077 6.9692308 7.6461538
[13] 8.3230769 9.0000000
> l<-(.2:4/2)
> l
[1] 0.1 0.6 1.1 1.6
#sum
> #
> #(i^3+4i^2) where i=10:100
> mohi<-10:100
> sum(mohi^3+4*mohi^2)
[1] 26852735
> rj<-1:25
> sum((2^rj)/rj+3*rj/(rj^2))
[1] 2807618
> #Function paste to create
> #("label 1","label 2".......,"label 30")
> paste("label",1:30)
 [1] "label 1"  "label 2"  "label 3"  "label 4" 
 [5] "label 5"  "label 6"  "label 7"  "label 8" 
 [9] "label 9"  "label 10" "label 11" "label 12"
[13] "label 13" "label 14" "label 15" "label 16"
[17] "label 17" "label 18" "label 19" "label 20"
[21] "label 21" "label 22" "label 23" "label 24"
[25] "label 25" "label 26" "label 27" "label 28"
[29] "label 29" "label 30"
> #("fn1","fn2",....,"fn30")
> paste("fn",1:30,sep="")
 [1] "fn1"  "fn2"  "fn3"  "fn4"  "fn5"  "fn6" 
 [7] "fn7"  "fn8"  "fn9"  "fn10" "fn11" "fn12"
[13] "fn13" "fn14" "fn15" "fn16" "fn17" "fn18"
[19] "fn19" "fn20" "fn21" "fn22" "fn23" "fn24"
[25] "fn25" "fn26" "fn27" "fn28" "fn29" "fn30"
> #Creat two vectors of random integer which are chosen with replacement from the integers 0,1,2.....,999. both vectors have lengh 250
> set.seed(50)
> rVec<-sample(0:999,250,replace = T)
> qVec<-sample(0:999,250,replace = T)
> #suppose x=(x1,x2,......xn) denote rVec and y=(y1,y2....yn) denot qVec
> # creat the vector (y2-x1,......,yn-xn-1)
> qVec[-1]-rVec[-length(rVec)]
  [1] -359  692 -724   40 -626 -719 -809  527  -89
 [10] -829  248  144 -749 -352 -220 -249  387 -492
 [19]   85 -106  303  -97 -436  146  282 -206 -385
 [28]  -96 -567 -757  287  277 -562  292  -89  -93
 [37] -847 -822 -203  679  309 -199 -273    4  -47
 [46]  142  122  414 -602 -304 -674   -8 -662 -168
 [55] -349  -63 -221  115    1 -600 -382 -487    2
 [64]  375   19 -113 -634  107   60   47  214 -325
 [73]  -49 -290  169  290 -624  457 -408  581 -189
 [82]  204  -80  409  209 -410  461   37 -127  185
 [91]  382 -446   44  -56 -270 -598 -378 -155  134
[100] -187  109  316 -139  158  305  -39 -119  182
[109]  441 -403 -107  615  614 -378 -464   31 -385
[118]  665  674 -217 -279 -406  -45 -489 -350 -451
[127]  -18  660  504   -6   60 -130 -379 -302 -219
[136]  -21  438  129 -201 -275  131  694  -96 -176
[145]  117 -113  887 -439 -126 -148  392 -158  444
[154] -291  232  -12 -274  477 -510  336 -759 -363
[163] -195 -220  160 -308 -333  302 -183  227  -12
[172]  428  665 -301   -8  222  -50 -444 -425 -650
[181] -424  318  154  238 -727   71  472  908  265
[190]  654 -644 -754  657 -382 -313  910 -381  394
[199] -596  602  397 -572  378 -274 -271  601 -791
[208] -378 -461   39  163 -118 -332 -170  -94  262
[217] -474  566 -273 -366 -400  374   42  100  135
[226]  609 -527  580 -219  128 -524  620 -206  410
[235] -280  -66  -50  252  279   48 -595  -59 -623
[244]  247  514   62 -102  475  287
Reactions

Post a Comment

0 Comments