[Vectors | Factors | Matrices | Arrays | Data Frames | Lists] |
> a2_c(12,14,16) > a2 [1] 12 14 16 > a3_c("TX","NY","TN","CA") > a3 [1] "TX" "NY" "TN" "CA"It is possible to name a vector
> heights <- c(160,140,155,170) > names(heights) <- c("Ned","Jill","Pat","Ronnie") > heights Ned Jill Pat Ronnie 160 140 155 170Alternatively, you can do
> people<- c("Ned","Jill","Pat","Ronnie") > names(heights) <- people > heights Ned Jill Pat Ronnie 160 140 155 170To find the length of a vector do:
>a2_c(12,14,16) > length(a2) [1] 3We can refer to part of a vector:
> heights Ned Jill Pat Ronnie 160 140 155 170 > heights[3] Pat > heights[c(1,3)] Ned Pat 160 155 > heights[heights < 160] Jill Pat 140 155Doing "-" we can designed the part of the vector, we do not want.
> heights[-2] Ned Pat Ronnie 160 155 170 > heights[-c(1,3)] Jill Ronnie 140 170It is possible to change coordinates of a vector:
> a2[3]_5 > a2 [1] 12 14 5 > a2[c(1,3)]_c(12,15) > a2 [1] 12 14 15 > x [1] -1.7 -1.3 -5.0 0.2 6.0 1.5 2.3 2.7 > x1_replace(x,c(1,3),c(-2,-15)) > x1 [1] -2.0 -1.3 -15.0 0.2 6.0 1.5 2.3 2.7We can apend two vectors in one
> x [1] -1.7 -1.3 -5.0 0.2 6.0 1.5 2.3 2.7 > y [1] 12 34 5 12 6 7 2345 2 > x1_append(x,y) > x1 [1] -1.7 -1.3 -5.0 0.2 6.0 1.5 2.3 2.7 12.0 [10] 34.0 5.0 12.0 6.0 7.0 2345.0 2.0
> states<-factor(c("TX","NY","TN","CA")) > states [1] TX NY TN CA > print.default(states) [1] 4 2 3 1 attr(, "levels"): [1] "CA" "NY" "TN" "TX" attr(, "class"): [1] "factor" > codes(states) [1] 4 2 3 1 > is.factor(states) [1] T > grades_factor(c("b","a","b","c","c","a"),levels=c("c","b","a")) > grades [1] b a b c c a > print.default(grades) [1] 2 3 2 1 1 3 attr(, "levels"): [1] "c" "b" "a" attr(, "class"): [1] "factor"The category objects can be converted to factors by using the command factor(). When a category object is converted to a factor object, the levels attribute becomes the factor labels.
Factors may be created using the factor() function or by converting a character or numeric object using the factor() or as.factor() functions. Factors may also be created by splitting a data object into groups.
We can convert a numeric object to a factor:
> age_c(1,1,2,2,1,3,1,2)We can convert a character object to a factor using either factor() or as.factor():> age_factor(age,labels=c("20-35yrs","35-55yrs","55+yrs"))
> age [1] 20-35yrs 20-35yrs 35-55yrs 35-55yrs 20-35yrs 55+yrs 20-35yrs 35-55yrs
> age_c("20-35yrs","20-35yrs","35-55yrs","35-55yrs","20-35yrs","55+yrs") > age_factor(age) > age [1] 20-35yrs 20-35yrs 35-55yrs 35-55yrs 20-35yrs 55+yrsWhen a category object is converted to a factor object, the levels attribute becomes the factor labels.
Ordered factors are factors whose levels are taken to be ordered. To create an ordered factor, we can do as follows:
> age_c("20-35yrs","20-35yrs","35-55yrs","35-55yrs","20-35yrs","55+yrs", "20-35yrs","35-55yrs")+ > ordered(age)_c("20-35yrs","35-55yrs","55+yrs") > age [1] 20-35yrs 20-35yrs 35-55yrs 35-55yrs 20-35yrs 55+yrs 20-35yrs 35-55yrs 20-35yrs < 35-55yrs < 55+yrsWhen a category object is converted to an ordered factor object, the levels attribute is not used as factor labels. In order to keep the label names, the category object must first be converted to a factor object and then to an ordered factor object.
> age_c(22,31,37,52,27,60,34,53) > age_cut(age,pretty(age)) > age_factor(age) > age_ordered(age) > age [1] 20+ thru 30 30+ thru 40 30+ thru 40 50+ thru 60 20+ thru 30 50+ thru 60 [7] 30+ thru 40 50+ thru 60 20+ thru 30 < 30+ thru 40 < 50+ thru 60
> a2_c(42, 66, 40, 62, 42, 71, 43, 79, 42, 67) > kids_matrix(a2,ncol=2,byrow=T) > kids [,1] [,2] [1,] 42 66 [2,] 40 62 [3,] 42 71 [4,] 43 79 [5,] 42 67You also can create matrices binding together vectors. For example,
> a1_c(12,13,15) > a2_c(14,16,13) > a3_cbind(a1,a2) > a4_rbind(a1,a2) > a3 a1 a2 [1,] 12 14 [2,] 13 16 [3,] 15 13 > a4 [,1] [,2] [,3] a1 12 13 15 a2 14 16 13We can form a matrix from a vector with given diagonals:
> x [1] -1.7 -1.3 -5.0 0.2 6.0 1.5 2.3 2.7 > diag(x) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] -1.7 0.0 0 0.0 0 0.0 0.0 0.0 [2,] 0.0 -1.3 0 0.0 0 0.0 0.0 0.0 [3,] 0.0 0.0 -5 0.0 0 0.0 0.0 0.0 [4,] 0.0 0.0 0 0.2 0 0.0 0.0 0.0 [5,] 0.0 0.0 0 0.0 6 0.0 0.0 0.0 [6,] 0.0 0.0 0 0.0 0 1.5 0.0 0.0 [7,] 0.0 0.0 0 0.0 0 0.0 2.3 0.0 [8,] 0.0 0.0 0 0.0 0 0.0 0.0 2.7Given a matrix, it is possible to form a matrix with 1in the first column, 2 in the second column and so on.
> a3 [,1] [,2] [,3] [,4] [,5] [1,] 37176 36624 35870 39710 41116 [2,] 36624 36701 35590 40045 41054 [3,] 35870 35590 34713 38691 39894 [4,] 39710 40045 38691 43794 44736 [5,] 41116 41054 39894 44736 45960 > col(a3) [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [2,] 1 2 3 4 5 [3,] 1 2 3 4 5 [4,] 1 2 3 4 5 [5,] 1 2 3 4 5 > row(a3) [,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 5To name the rows and columns of a matrix, you use "dimnames":
> dimnames(kids)_list(c("Tom","Fred","Sue","Donna","Lee"), + c("Height","Weight")) > kids Height Weight Tom 42 66 Fred 40 62 Sue 42 71 Donna 43 79 Lee 42 67If you do not want to name the columns, you can use NULL:
> dimnames(kids)_list(c(NULL),c("Height","Weight")) > kids Height Weight [1,] 42 66 [2,] 40 62 [3,] 42 71 [4,] 43 79 [5,] 42 67You extract an element, a row or a column of a matrix, or any other part,
> a2 [,1] [,2] [,3] [,4] [,5] [1,] 371 366 358 397 411 [2,] 366 367 355 400 410 [3,] 358 355 347 386 398 [4,] 397 400 386 437 447 [5,] 411 410 398 447 459 > a2[2,]_c(12,145,15,15,12) > a2 [,1] [,2] [,3] [,4] [,5] [1,] 371 366 358 397 411 [2,] 12 145 15 15 12 [3,] 358 355 347 386 398 [4,] 397 400 386 437 447 [5,] 411 410 398 447 459 > a2[,3]_c(12,11114,12,542,2) > a2 [,1] [,2] [,3] [,4] [,5] [1,] 371 366 12 397 411 [2,] 12 145 11114 15 12 [3,] 358 355 12 386 398 [4,] 397 400 542 437 447 [5,] 411 410 2 447 459 > a2[,c(1,3)] 371 12 12 11114 358 12 397 542 411 2 > a2[,-2] 371 12 397 411 12 11114 15 12 358 12 386 398 397 542 437 447 411 2 447 459 > a2[-2,-3] 371 366 397 411 358 355 386 398 397 400 437 447 411 410 447 459
For example
> a1_ array(1:24,c(3,4,2)) > a1 , , 1 [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 , , 2 [,1] [,2] [,3] [,4] [1,] 13 16 19 22 [2,] 14 17 20 23 [3,] 15 18 21 24When printing an array, Splus starts with the highest dimension and works toward the lowest dimension, printing two--dimensional matrices at each stage.
It is possible to extract part of array
> a1[,1,] [,1] [,2] [1,] 1 13 [2,] 2 14 [3,] 3 15 > a1[1,,] [,1] [,2] [1,] 1 13 [2,] 4 16 [3,] 7 19 [4,] 10 22
For example,
> heights Ned Jill Pat Ronnie 160 140 155 170 > weights_c(220,124,156,146) > weights [1] 220 124 156 146 > data.frame(heights,weights) heights weights Ned 160 220 Jill 140 124 Pat 155 156 Ronnie 170 146 > a2_data.frame(heights,weights) > a2 heights weights Ned 160 220 Jill 140 124 Pat 155 156 Ronnie 170 146Data frames can be created by using the read.table(), data.frame(), expand.grid(), or the as.data.frame() functions.
For example,
> b2 [1] 1 3 > b3 [,1] [,2] [,3] [,4] [1,] "id" "12" "134" "14" [2,] "fname" "12" "134" "14" [3,] "lname" "12" "12" "14" [4,] "age" "12" "12" "14" [5,] "junk" "12" "12" "14" [6,] "1" "2" "3" "id" > b1_list(b2,b3) > b1 [[1]]: [1] 1 3 [[2]]: [,1] [,2] [,3] [,4] [1,] "id" "12" "134" "14" [2,] "fname" "12" "134" "14" [3,] "lname" "12" "12" "14" [4,] "age" "12" "12" "14" [5,] "junk" "12" "12" "14" [6,] "1" "2" "3" "id"We also can given names to a list. For example, the first component of the list is named "one"and the second "two"
> b1_list(one=b2,two=b3) > b1 $one: [1] 1 3 $two: [,1] [,2] [,3] [,4] [1,] "id" "12" "134" "14" [2,] "fname" "12" "134" "14" [3,] "lname" "12" "12" "14" [4,] "age" "12" "12" "14" [5,] "junk" "12" "12" "14" [6,] "1" "2" "3" "id" > names(b1)_c("uno","dos") > b1 $uno: [1] 1 3 $dos: [,1] [,2] [,3] [,4] [1,] "id" "12" "134" "14" [2,] "fname" "12" "134" "14" [3,] "lname" "12" "12" "14" [4,] "age" "12" "12" "14" [5,] "junk" "12" "12" "14" [6,] "1" "2" "3" "id"The individual components in the list retain their properties as vectors and extra dimension so that the result is a vector as such, individual elements can be extracted from each component in the same way as in any other vector:
> b1[[1]][2]*10 [1] 30The components of the list can then be extracted using their names attribute:
> b1$uno [1] 1 3 > b1[[1]] [1] 1 3