『Data Science』R语言学习笔记,基础语法
副标题[/!--empirenews.page--]
Data TypesData Object & Vectorx <- c(0.5,0.6) ## numeric x <- c(TRUE,FALSE) ## logical x <- c(T,F) ## logical x <- c("a","b","c") ## character x <- 9:29 ## integer x <- c(1+0i,2+4i) ## complex x <- vector("numeric",length = 10) ## create a numeric vector,which length is 10. x <- 0.6 ## get the class type of the variable class(x) ## print the class type of "x". x <- 1:10 ## set the class type to the variable forcibly. as.character(x) Listx <- list("...","...",...) MatricesMatrices are vectors with a dimension attribute. The dimension attribute is itself an integer vector of lenght 2 (nrow,ncol). m <- matrix(nrow = 2,ncol = 3) n <- matrix(1:6,nrow = 2,ncol = 3) dim(m) ## get the value of "norw,ncol" of the matrix. attributes(m) ## get the a of m <- 1:10 ## create a new numeric vector,from 1 to 10 dim(m) <- c(2,5) ## put the vector "m" into a matrix,and assign the value (nrow = 2,ncol = 3) to it. m ## print the value of "m". x <- 1:3 y <- 10:12 cbind(x,y) ## create a matrix by "cbind",binding the value of columns with variables,which has 3 rows and 2 columns. rbind(x,y) ## create a matrix by "rbind",binding the value of rows with variables,which has 2 rows and 3 columns. FactorsFactors are used to represent categorical data. One can think of a factor is an integer vector where each integer has a label. x <- factor(c("yes","yes","no","no")) ## create a factor with a character vector. x ## print the factor. table(x) ## list the label (with its quantity) of the factor in a table. unclass(x) ## list the value and the label of the factor. x <- factor(c("yes",level("yes","no"))) ## create a factor with a character vector which had set the "levels" in it. Missing ValuesMissing values are denoted by NA of NaN for undefined mathematical operations. is.na() is.nan() x <- c(1,2,NaN,NA,4) ## Create a vector for test the functions,```is.na()``` and ```is.nan()```. is.na(x) ## NA values have a class also,so there are integer NA,character NA,etc. is.nan(x) ## A NaN value is also NA but the converse is not true. Whole codes below: > x <- c(1,10,3) > is.na(x) [1] FALSE FALSE TRUE FALSE FALSE > is.nan(x) [1] FALSE FALSE FALSE FALSE FALSE > x <- c(1,4) > is.na(x) [1] FALSE FALSE TRUE TRUE FALSE > is.nan(x) [1] FALSE FALSE TRUE FALSE FALSE Data FramesData frames are used to store tabular data.
> x <- data.frame(foo = 1:4,bar = c(T,T,F,F)) ## create a Data Frame Object which has two columns and four rows. > x foo bar 1 1 TRUE 2 2 TRUE 3 3 FALSE 4 4 FALSE NamesR objects can also have names,which is very useful for writing readable code and self-describing objects. > x <- 4:6 ## Create a integer vector 'x' which has three elements. > names(x) <- c("foo","bar","norf") ## Assign names to vector 'x'. > x ## Print the value of 'x'. foo bar norf 4 5 6 Data ReadingReading Data
|