Into R:
1. variable assignment
- "->" and "=" There are subtle differences, their role in the program range in scope after the assignment is different;
- In order to maintain a consistent programming divisions, we recommended to use the "<-" symbol at the time of the assignment;
2.R script
R script concept is simple, it is a document code there, is **. R ** is the end. When using R to do the analysis, we sometimes write long code, and the code that we want after repeated changes can be used, then we need to use the R script to store our code.
3. Create the script R:
In order to open the Rstudio: "File - New - File - R Script"
4.R code runs
** Note: ** Code in R need to select the code you want to run click "run" to run the upper right corner, you can use shortcut keys "ctrl + enter" to run.
Five basic data types:
Basic concepts and operations
Variables: variable is derived from a mathematical abstraction, can be stored in a computer language calculation result or "means that a particular value"
VS constant variable: specific things in motion, if a certain amount remains the same, it is called constant; on the contrary, it is called variable
Data type: a set of properties and the set value of the same general term for a set of operations defined in this set of values;
Store different data types of variables: the data type of the variable group, the programming language of the data type matches the assigned memory space for storage.
Data corresponding to R five basic types:
- Character - Character
- Numeric - Digital
- Integer - integer
- Complex * - plural
- Logical - Logical number
# 变量
# 变量储存计算结果
result <- 1+2*3/4
result
# 变量表示值的抽象概念
company <- "公司A"
company
company <- "公司B"
company
# 赋值运算符
n <- 10 #将数值10赋值给变量n
10 -> n #箭头也可以向右
n = 10 #除了箭头外,等号也可以用来赋值
_______________________________________________________________________________________________________________
# 五种基本数据类型
ch <- "你好"
n <- 1.23
i <- as.integer(1)
co <- 1+2i
lo <- TRUE
Common data types: Date and Time
Date - Date:
- Mutator: as.Date ()
Time - TIme
- POSIXct class represents: the internal storage of a large integer - assignment function as.POSIXct
- POSIXlt: internal storage is a list containing all kinds of time information (year, month, day, time ......) - mutator as.POSIXlt
- unclass () function will open this list
- $ + List for a specific element name: Display specific element values
# 日期(Date)和时间(Time)
# 定义日期变量
x1 <- as.Date("2015-03-26")
x2 <- as.Date("2015-01-02")
x1
x2
# 日期变量的专属操作
x1-3 ## 日期减去整数
x1 - x2 ## 两个日期的差值
class(x1-x2) ## 产生difftime的数据类型,表示两个日志相差的天数
as.integer(x1 - x2) ## 将 difftime直接转换成整数
## 定义一个 POSIXct 类型的时间变量
tm1 <- as.POSIXct("2013-07-24 23:55:26")
tm1
as.integer(tm1) ## 查看tm1所对应的整数
tm1-3 ## POSIXct减去3
## 定义一个 POSIXlt 类型的时间变量
tm2 <- as.POSIXlt("2013-07-24 23:55:26")
tm2
# class(tm2) ## 显示tm2的类别 - POSIXlt
unclass(tm2) ## 返回POSIXlt值背后,定义它的列表
tm2$sec ## $+名称,显示POSIXlt列表名称对应的数值
R Operator:
Data analysis of the most common types of operators.