ILLUSTRATUION OF MISCELLANEOUS COMMANDS 8/30/2017 ============================================================ > paste(letters[1:8],1:8,sep=".") [1] "a.1" "b.2" "c.3" "d.4" "e.5" "f.6" "g.7" "h.8" > paste(letters[1:8], collapse="") [1] "abcdefgh" > strsplit(.Last.value,"") [[1]] [1] "a" "b" "c" "d" "e" "f" "g" "h" > unlist(.Last.value) [1] "a" "b" "c" "d" "e" "f" "g" "h" > length("abcdefgh") [1] 1 > nchar("abcdefgh") [1] 8 ------------------------------------- > if(3<5) 1:5 else 10:15 [1] 1 2 3 4 5 > ifelse( (1:10) %% 2 , 1:10, 11:20 ) [1] 1 12 3 14 5 16 7 18 9 20 > replace(1:10, (1:10 %% 2)==0, 11:20) [1] 1 11 3 12 5 13 7 14 9 15 Warning message: In x[list] <- values : number of items to replace is not a multiple of replacement length ------------------------------------- > tmplist = NULL for(i in 1:5) tmplist = c(tmplist, list(1:i)) > tmplist [[1]] [1] 1 [[2]] [1] 1 2 [[3]] [1] 1 2 3 [[4]] [1] 1 2 3 4 [[5]] [1] 1 2 3 4 5 > unlist(tmplist) [1] 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 -------------------------------------- > tmp = table(rpois(100,2)) > attributes(tmp) $dim [1] 8 $dimnames $dimnames[[1]] [1] "0" "1" "2" "3" "4" "5" "6" "7" $class [1] "table" --------------------------------------- > outer(1:5, (-2):2, "+") [,1] [,2] [,3] [,4] [,5] [1,] -1 0 1 2 3 [2,] 0 1 2 3 4 [3,] 1 2 3 4 5 [4,] 2 3 4 5 6 [5,] 3 4 5 6 7 > attributes(.Last.value) $dim [1] 5 5 > c(outer(1:5, (-2):2, "+")) [1] -1 0 1 2 3 0 1 2 3 4 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 > outer(1:5, (-2):2, "*") [,1] [,2] [,3] [,4] [,5] [1,] -2 -1 0 1 2 [2,] -4 -2 0 2 4 [3,] -6 -3 0 3 6 [4,] -8 -4 0 4 8 [5,] -10 -5 0 5 10 > outer(letters[1:5], 3:7, paste, sep=".") [,1] [,2] [,3] [,4] [,5] [1,] "a.3" "a.4" "a.5" "a.6" "a.7" [2,] "b.3" "b.4" "b.5" "b.6" "b.7" [3,] "c.3" "c.4" "c.5" "c.6" "c.7" [4,] "d.3" "d.4" "d.5" "d.6" "d.7" [5,] "e.3" "e.4" "e.5" "e.6" "e.7" --------------------------------------- > x = 1:10 > y = 3:17 > z = 4:6 > x[] = y[] Warning message: In x[] = y[] : number of items to replace is not a multiple of replacement length > x [1] 3 4 5 6 7 8 9 10 11 12 > x[] = z[] Warning message: In x[] = z[] : number of items to replace is not a multiple of replacement length > x [1] 4 5 6 4 5 6 4 5 6 4 > x = z > x [1] 4 5 6 --------------------------------------------- > tmpmat = array(sample(100, 25), c(5,5)) > tmpmat [,1] [,2] [,3] [,4] [,5] [1,] 5 13 19 76 58 [2,] 98 41 83 96 47 [3,] 55 50 67 95 88 [4,] 86 89 61 80 7 [5,] 78 16 28 17 97 > mean(tmpmat) [1] 58 > apply(tmpmat,2,mean) [1] 64.4 41.8 51.6 72.8 59.4 > summary(tmpmat) V1 V2 V3 V4 V5 Min. : 5.0 Min. :13.0 Min. :19.0 Min. :17.0 Min. : 7.0 1st Qu.:55.0 1st Qu.:16.0 1st Qu.:28.0 1st Qu.:76.0 1st Qu.:47.0 Median :78.0 Median :41.0 Median :61.0 Median :80.0 Median :58.0 Mean :64.4 Mean :41.8 Mean :51.6 Mean :72.8 Mean :59.4 3rd Qu.:86.0 3rd Qu.:50.0 3rd Qu.:67.0 3rd Qu.:95.0 3rd Qu.:88.0 Max. :98.0 Max. :89.0 Max. :83.0 Max. :96.0 Max. :97.0 > c(tmpmat %*% rep(0.2,5)) [1] 34.2 73.0 71.0 64.6 47.2 > apply(tmpmat,1,mean) [1] 34.2 73.0 71.0 64.6 47.2