R语言和其他语言类似 学懂了C语言再学这些比较简单 下面仅是对R语言基本概念的梳理 后面会结合项目写一些程序 主要以作图为主
数据类型
元素类型包括:
Logical(逻辑型) |
TRUE, FALSE |
Numeric(数字) |
12.3,5,999 |
Integer(整型) |
2L,34L,0L |
Complex(复合型) |
3 + 2i |
Character(字符) |
'a' , '"good", "TRUE", '23.4' |
Raw(原型) |
"Hello" 被存储为 48 65 6c 6c 6f |
R对象非常多但是经常使用矢量、列表、矩阵、数组、因子、数据帧。
向量Vectors
当你想用多个元素创建向量时,你应该使用c()函数,这意味着将元素组合成一个向量。
# Create a vector.
apple <- c('red','green',"yellow")
print(apple)
# Get the class of the vector.
print(class(apple))
[1] "red" "green" "yellow"
[1] "character"
列表Lists
列表是一个R对象,它可以在其中包含许多不同类型的元素,如向量,函数甚至其中的另一个列表。
# Create a list.
list1 <- list(c(2,5,3),21.3,sin)
# Print the list.
print(list1)
[[1]]
[1] 2 5 3
[[2]]
[1] 21.3
[[3]]
function (x) .Primitive("sin")
矩阵Matrices
矩阵是二维矩形数据集。 它可以使用矩阵函数的向量输入创建。
# Create a matrix.
M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
print(M)
[,1] [,2] [,3]
[1,] "a" "a" "b"
[2,] "c" "b" "a"