R语言2——数据类型和基本运算
R语言2——数据类型和基本运算
目录
1.R的数据类型
(1)Logical(逻辑型):只有两个值TRUE, FALSE
(2)Numeric(数字):整数、小数等
(3)Complex(复合型):带有虚数i的数,如1 + 2i
(4)Character(字符):包含在“ ”之中,如“hello!”
(5)Vectors向量:c()函数,将元素组合成一个向量。c(1,2,3,4)
(6)Lists列表:list()内容包含许多不同类型的元素,如 list(c(1,2,3),"we",2,3,12.3)
(7)Matrices矩阵:矩阵时二维的,M = matrix( c('a','a','b','c','b','a'), nrow = 3, ncol = 2, byrow = TRUE)
(8)Arrays 数组:矩阵被限制为二维,但阵列可以具有任何数量的维度。 数组函数使用一个dim属性创建所需的维数。 array(c('one','tow',"three"),dim = c(3,3,3))
(9)Data Frames 数据帧:数据帧是表格数据对象,每列可以包含不同的数据模式
v <- FALSE
v
n <- 12.2
n
c <- 2+2i
c
s <- "hello!"
s
x1 <- c(1,2,3,4)
x1
x2 <- c(1,2,"three",4)
x2
l1 <- list(c(1,2,3),"we",2,3,12.3)
l1
M = matrix( c('a','a','b','c','b','a'), nrow = 3, ncol = 2, byrow = TRUE)
M
a <- array(c('one','tow',"three"),dim = c(3,3,3))
a
bmi <- data.frame(
gender = c("Male", "Male","Female"),
height = c(152, 171.5, 165),
weight = c(81,93, 78),
Age = c(42,38,26)
)
bmi
结果如下:
> v <- FALSE
> v
[1] FALSE
> n <- 12.2
> n
[1] 12.2
> c <- 2+2i
> c
[1] 2+2i
> s <- "hello!"
> s
[1] "hello!"
> x1 <- c(1,2,3,4)
> x1
[1] 1 2 3 4
> x2 <- c(1,2,"three",4)
> x2
[1] "1" "2" "three" "4"
> l1 <- list(c(1,2,3),"we",2,3,12.3)
> l1
[[1]]
[1] 1 2 3
[[2]]
[1] "we"
[[3]]
[1] 2
[[4]]
[1] 3
[[5]]
[1] 12.3
> M = matrix( c('a','a','b','c','b','a'), nrow = 3, ncol = 2, byrow = TRUE)
> M
[,1] [,2]
[1,] "a" "a"
[2,] "b" "c"
[3,] "b" "a"
> a <- array(c('one','tow',"three"),dim = c(3,3,3))
> a
, , 1
[,1] [,2] [,3]
[1,] "one" "one" "one"
[2,] "tow" "tow" "tow"
[3,] "three" "three" "three"
, , 2
[,1] [,2] [,3]
[1,] "one" "one" "one"
[2,] "tow" "tow" "tow"
[3,] "three" "three" "three"
, , 3
[,1] [,2] [,3]
[1,] "one" "one" "one"
[2,] "tow" "tow" "tow"
[3,] "three" "three" "three"
> bmi <- data.frame(
+ gender = c("Male", "Male","Female"),
+ height = c(152, 171.5, 165),
+ weight = c(81,93, 78),
+ Age = c(42,38,26)
+ )
> bmi
gender height weight Age
1 Male 152.0 81 42
2 Male 171.5 93 38
3 Female 165.0 78 26
>
2.R的运算符
(1)赋值运算符:右边赋值给左边<-(或者=,<<−),左边赋值给右边>-(或者=,->>),
(2)算术运算符:加+,减-,乘*,除/,求余%%,相除求商%/%,
(3)关系运算符:前一个变量中的值是否大于第二个变量的值 >,前一个变量中的值是否小于第二个变量的值 <,前一个变量中的值是否等于第二个变量的值 ==,同理,小于等于<=,大于等于>=,不等于!=
(4)逻辑运算符:与& 或 | 非!
(5)其他运算符:按顺序创建一系列数字冒号: ,元素是否属于向量%in% , 将矩阵与其转置相乘%*%
v <- c(1,2.3,3,4,6,12,56)
t <- c(0.4,3,2,5,6,11,75)
v + t
v - t
v * t
v / t
v %% t
v %|% t
v > t
v < t
v == t
v != t
v2 <- c(4,1,FALSE,2+3i)
t2 <- c(3,1,TRUE,2+3i)
v & t
v | t
! v
v3<- c(1:10,by=2)
v %in% v3
> v <- c(1,2.3,3,4,6,12,56)
> t <- c(0.4,3,2,5,6,11,75)
> v + t
[1] 1.4 5.3 5.0 9.0 12.0 23.0 131.0
> v - t
[1] 0.6 -0.7 1.0 -1.0 0.0 1.0 -19.0
> v * t
[1] 0.4 6.9 6.0 20.0 36.0 132.0 4200.0
> v / t
[1] 2.5000000 0.7666667 1.5000000 0.8000000 1.0000000 1.0909091 0.7466667
> v %% t
[1] 0.2 2.3 1.0 4.0 0.0 1.0 56.0
> v %|% t
Error in v %|% t : 没有"%|%"这个函数
> v > t
[1] TRUE FALSE TRUE FALSE FALSE TRUE FALSE
> v < t
[1] FALSE TRUE FALSE TRUE FALSE FALSE TRUE
> v == t
[1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE
> v != t
[1] TRUE TRUE TRUE TRUE FALSE TRUE TRUE
>
> v2 <- c(4,1,FALSE,2+3i)
> t2 <- c(3,1,TRUE,2+3i)
> v & t
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE
> v | t
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE
> ! v
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> v3<- c(1:10,by=2)
> v %in% v3
[1] TRUE FALSE TRUE TRUE TRUE FALSE FALSE
>
3.常见数学函数
(1)n的平方根:sqrt(n)
(2)自然常数e的n次方:exp(n)
(3)n的对数函数,返回n的几次方等于m:log(m,n)
(4)log(m,10):log10(m)
(5)对 n 四舍五入取整:round(n) ,对 n 保留 m 位小数四舍五入,round(n,m)
(6)对 n 向上取整:ceiling(n)
(7) 对 n 向下取整:floor(n)
(8)(反)三角函数:sin(x),cos(x),tan(x),asin(x),acos(x),atan(x)