All the learning resources are from https://www.w3cschool.cn. if there is any violation, please contact me without hesitation. The aim of the article is just to summarize some points when I am learning at the Wscschool recently and there must be some errors either in grammar or knowledge . I would accept any critics. And I believe we can make progress together!
Ok! Let's begin!
Install packages
install. packages(Name)
Valuation
string_name<-" hello world"
print(string_name)
- '<-' ,'=','->',these marks are viable. In most cases they are equal to each other. However there are tiny differences among them when you check on your variables in the work platform. The variable created by '=' sometimes cannot be reserved, especially in the circumstance of function invocation.
Data Types and Structure
There are six basic types of R data structure, which compose what we called "the object", please consider the counterparts in C or Java
All six types are as follows.
- logical eg. True or False
- numeric eg. 23.4
- Integer eg. 2L, 3L
- Complex eg. 2+3i
- Character eg. "GOOD" ,'a', '23.45' 'True'
- Raw eg. 48 65 6c 6c 6f (#the same as hello)
R Objects
Vector
#create a vector
myvec<- c("you", ‘“and”, "me")
print(myvec)
- pay attention to use "<-" instead of '=' when valuating,though both all them are viable for running.
- use English style when you type codes
- it is both ok to input strings as 'and' or "and"
List
#create a list
```{r create a list}
mylist<- list(c(2,3.1), myvec, sin,cos)
print(mylist)
[output]
[[1]]
[1] 2.0 3.1
[[2]]
[1] "you" "and" "me"
[[3]]
function (x) .Primitive("sin")
[[4]]
function (x) .Primitive("cos")
- pay attention to your use of punctuation, such as ',' ,'.'
-there is difference between lists and data frames(or tables); In different row of a list, the numbers of element(or columns) can vary, however in tables, numbers of all the elements in a column are required to be identical.
Matrix
mymatrix<- matrix(c(1,2,5,"and",'he','she','it','shy','3'), ncol=3,nrow=3,byrow=TRUE)
print(mymatrix)
- For logical objects, TRUE,all letters are capitals
- Devision: It is a good way to transfer the vector into a matrix in any dimension you want by changing the parameter 'ncol' and 'nrow'
- It is ok to change the order of parameters in the function
- 'byrow' means dividing the vector according to the order of row, a logical parameter, 'TRUE' or 'FALSE'; there is no parameter named as 'bycol'; When 'byrow' is omitted, the R compiler would consider it as FALSE, that is to prioritize column over row.
- In R, the name of function tends to be lowercase letters like 'matrix'
- In matrix, all the elements have identical data types, if there are something inconsistent, the matrix would consider them as 'string' automatically when there is an element of 'string' style.
-In matrix, when all the elements are numbers, we can notice that all the elements in the same column are automatically transferred as numeric and however columns elsewhere are still in the form of 'Integer'
Arrays :
it can be used to create a set of matrix beyond the limit of two dimension
myarr<- array(c('male','female'), dim=c(3,2,2,2))
print(myarr)
[output]
, , 1, 1
[,1] [,2]
[1,] "male" "female"
[2,] "female" "male"
[3,] "male" "female"
, , 2, 1
[,1] [,2]
[1,] "male" "female"
[2,] "female" "male"
[3,] "male" "female"
, , 1, 2
[,1] [,2]
[1,] "male" "female"
[2,] "female" "male"
[3,] "male" "female"
, , 2, 2
[,1] [,2]
[1,] "male" "female"
[2,] "female" "male"
[3,] "male" "female"
- it is a good way to create a set of matrix with similar elements and make it orderly by 'dimension', with column priority by default in each matrix.
- 'dim=c(...)': 'dim' is strongly suggested to be fully typed though the omission of it doesn't hurt when running.(probably leading to some unnecessary errors in other situations)
When the dimension of the vector created by 'c()' is over the matrix dimension, the repetition can beyond the matrix and the sequence continue in the following matrix.
myarr<- array(c('male','female',1,2), dim=c(3,2,2,2))
print(myarr)
[output]
[,1] [,2]
[1,] "male" "2"
[2,] "female" "male"
[3,] "1" "female"
, , 2, 1
[,1] [,2]
[1,] "1" "female"
[2,] "2" "1"
[3,] "male" "2"
, , 1, 2
[,1] [,2]
[1,] "male" "2"
[2,] "female" "male"
[3,] "1" "female"
, , 2, 2
[,1] [,2]
[1,] "1" "female"
[2,] "2" "1"
[3,] "male" "2"
Factors
factor(c()): create a factor
nlevels(onefactor): numbers of categories
myfactor<-factor(c('apple','apple','banana','34','pear','pear','tomato'))
nfactor=nlevels(myfactor)
print(myfactor)
print(nfactor)
[output]
[1] apple apple banana 34 pear pear tomato
Levels: 34 apple banana pear tomato
[1] 5
- factor(O), O(object) can be a matrix ,a vector, and an array
myfactor<-factor(M<-matrix(c('apple','apple','banana','34','pear','pear'),ncol=2))
#M can be replaced by 'myarray'
print(M)
nfactor=nlevels(myfactor)
print(myfactor)
print(nfactor)
[output]
[,1] [,2]
[1,] "apple" "34"
[2,] "apple" "pear"
[3,] "banana" "pear"
[1] apple apple banana 34 pear pear
Levels: 34 apple banana pear
[1] 4
Data frames
One can consider 'Data frames' as tables
data.frame(colname1=c(),colname2=c(),...,)
Stu_info=data.frame(name=c('lihua','zhangli','wangchang'),
gender=c('f','f','m'),
score=c(93,89,74),
rank=c('A','B','B')
)
print(Stu_info)
image.png
- warning: there has to be no punctuation after the syntax 'rank=c('A','B','B')',
- the name of the column is not compulsory, it can be omitted.
- little difference exits when you use ' rank=c('A','B','B')' and' rank<-c('A','B','B')'. The name of the column is shown in a different way. '=' is recommended.
How to name the variables in R
- The name can be composed of '.','_',numbers and characters.
- a name beginning with '.' is viable.
- '%' is not allowed in any place.
- a name with a start of '_' is invalid.
- a name with a beginning of '.' and then a number followed is invalid, eg. .2var_name
extension: the connection of characters
function: cat(vector1, 'is', vector2)
- cat(): the result would show up automatically, there is no need to use 'print()'
- the mark of the dimension of the vector created by 'c(string1,string2,...)' ,as "..." ,is naturally dropped for combining a complete phrase.
var_2 <- c("learn","R")
cat_var_2<-cat ("var_2 is ", var_2 ,"
")
print(cat_var_2)
[output]: var_2 is learn R
Search for variables in the workbench
function : ls(pattern='')
- It is a useful tool to search for certain variables with 'pattern='...' '.
- when use ls(), variables with the start of '.' are hidden except for adding a parameter 'all.name=TRUE'.
print(ls(all.name=TRUE))
# or
print(ls())
# search certain variable with 'var'
print(ls(pattern='var'))
[output]
[1] ".Random.seed" "cat_var_2" "dim" "M" "myarr" "myfactor" "mylist"
[8] "mymatrix" "myvec" "nfactor" "rank" "Stu_info" "var.2" "var_2"
[1] "cat_var_2" "dim" "M" "myarr" "myfactor" "mylist" "mymatrix" "myvec"
[9] "nfactor" "rank" "Stu_info" "var.2" "var_2"
[1] "cat_var_2" "var.2" "var_2"
Delete variables
function: rm(var_name): delete certain variable
function: rm(list=ls()): delete all variables
网友评论