To summarize the "Getting Started 3R" (Reading, 'Riting,' Rrithmetic) in reading and writing, reading and writing at different data structures is still a little different.
A space connecting wordsthe elements, paste()the received parameter should be a plurality of variables, sepwe determine a connector between a plurality of vectors, and collapsedecide how to merge vector elements uniform.
factor(x,levels,labels)R factor can be created, but levelsrefers to the input value of x, labelsrepresents the output value of the factor of the newly created.
Conversion factor
1 2 3 4 5
Numbers <-factor (C ( . 9 , . 8 , 10 , . 8 , . 9 )) STR (Numbers) as.character (Numbers) ### returns the character elements as.numeric (numbers) internal representation ### returns factor as .numeric (as.character (Numbers)) ### return a numeric element
Ordered factor
Statistical data category
1
table(state.region)
Ordinal variables
Using the factor()function, and the specified parametersordered=TRUE
Use ordered()function
matrix
1 2 3 4 5
Matrix (Data, ncol, nrow, byrow) Dim () ### See matrix dimension of rbind () ### rows by a vector matrix cbind () ### columns by a vector matrix cbind ( . 1 : . 3 , . 4 : . 6 , Matrix ( . 7 : 12 is , ncol = 2 ))
Index, modify and naming
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
first.matrix <- matrix [ . 1 : 12 is , ncol = . 4 , byrow = TRUE ] ############# value first.matrix [ . 1 : 2 , 2 : . 3 ] first.matrix [ 2 : 3 ,] ### numeric index first.matrix [- 2 , - 3 ] ### in addition to extracting the second row, third column all data first.matrix [-C ( . 1 , 3 ),] ## to reduce the dimension of vector # first.matrix [ 2 ,, drop = large column Array, matrix, and List dataframe ERAL "> FALSE ] ### without reducing the dimensions, the matrix is still ############# modified first.matrix [ . 3 , 2 ] <- . 4 first.matrix [ 2 ,] <- C ( . 1 , . 3 ) first.matrix [ . 1 : 2 , . 3 : . 4 ] <- C ( . 8 , . 4 , 2 , . 1 ) ############# named ranks rownames (X) <- C ( 'A' , 'B' ) colnames (X) < -C ( 'C' , 'D' ) colnames (X) [ . 1 ] <- 'AA' X [ 'B' ,] ### as an index with the name
Compute
1 2 3
T () ### transposition Solve () ### Inversion X% *% T (X) ### is multiplied by
array
Vectors and matrices are arrays.
1 2
Array ( . 1 : 24 , Dim C = ( . 3 , . 4 , 2 )) ### to create a three-dimensional array Dim (x) <- C ( . 3 , . 4 , 2 ) ### to change the dimension of the vector x
data.frame
Created by Matrix x.df<-as.data.frame(x)
Created by Vector data<-data.frame(x,y,z)
If the variable is a string type is created, R will be automatically converted into factor can be used stringAsFactor=FALSEto maintain string type
1 2
names (Data) [ 2 ] <- 'B' ### named header rownames (Data) <- C ( 'A' , 'B' , 'C' ) ### named observation