R语言学习基础(2)

注:参考书籍:《153分钟学会R语言》
胡说八道,真的是胡说八道----我都看不知道多少分钟了,还是云里雾里的。。

  • head函数和tail函数取出矩阵的头部和尾部
> x=matrix(1:12,3,4)
> head(x = x)
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
> head(x = x , 2)
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
> tail(x = x , 1)
     [,1] [,2] [,3] [,4]
[3,]    3    6    9   12
  • 移除矩阵的行或是列

> #移除行或列的数据
> x <- data.frame(matrix(1:30,nrow = 5, byrow = TRUE))
> dim(x)
[1] 5 6 #5行6列
> print(x)
  X1 X2 X3 X4 X5 X6
1  1  2  3  4  5  6
2  7  8  9 10 11 12
3 13 14 15 16 17 18
4 19 20 21 22 23 24
5 25 26 27 28 29 30
> x
  X1 X2 X3 X4 X5 X6
1  1  2  3  4  5  6
2  7  8  9 10 11 12
3 13 14 15 16 17 18
4 19 20 21 22 23 24
5 25 26 27 28 29 30
#移除整行或整列元素
> new.x1 <- x[-c(1,4),] 
> new.x1
  X1 X2 X3 X4 X5 X6
2  7  8  9 10 11 12
3 13 14 15 16 17 18
5 25 26 27 28 29 30
> new.x2 <- x[,-c(2,3)]
> new.x2
  X1 X4 X5 X6
1  1  4  5  6
2  7 10 11 12
3 13 16 17 18
4 19 22 23 24
5 25 28 29 30
  • 两个数据框相同么?
> al <- data.frame(num = 1:8, lib = letters[1:8])
> a2 <- al
> a2[[3,1]] <- 2  
> al
  num lib
1   1   a
2   2   b
3   3   c
4   4   d
5   5   e
6   6   f
7   7   g
8   8   h
> a2[[8,2]] <- "a"
> all(al == a2)#数据框a2的两个位置被改动
[1] FALSE
> any(al != a2)
[1] TRUE
> #比较两个数框是否相同,使用函数identical(element1,element2)
> identical(al,a2)
[1] FALSE
> #返回两个数据框不相同的位置
> which(al != a2 , arr.ind = TRUE)
     row col
[1,]   3   1
[2,]   8   2
> #去掉相同的行或相同的列
> #使用unique函数去除重复元素
> x <- c(2:4,3:5)
> x
[1] 2 3 4 3 4 5
> xu <- x[!duplicated(x)]
> xu
[1] 2 3 4 5
> #duplicated函数返回元素是否重复的逻辑值
> unique(x)
[1] 2 3 4 5
  • 维数变换使用
    apern函数
  • 删除列表中的元素
> #删除list中的元素,使用null(无效对象)
> lst <- list("a" = 1 , "b" = 2) 
> lst$a <- NULL #注意是大写
> lst
$b
[1] 2
  • 对矩阵的行或是列进行累加
> mat 
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20
> apply(mat , 2 , cumsum) #对列累加
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    3   13   23   33
[3,]    6   21   36   51
[4,]   10   30   50   70
[5,]   15   40   65   90
  • 比较两个向量元素的大小
> x <- 1:10
> y <- rev(x) #把行向量倒过来
 [1] 10  9  8  7  6  5  4  3  2  1
> pmax(x , y)
 [1] 10  9  8  7  6  6  7  8  9 10
> pmin(x , y)
 [1] 1 2 3 4 5 5 4 3 2 1
  • 判断数据框的列是否为数字
> sapply(x , is.numeric)
 [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
  • sample函数
> #随机抽取数字
> y = sample(5)
> y
[1] 2 1 5 3 4
> sample(y)
[1] 5 3 2 1 4
> sample(y , replace = TRUE)
[1] 2 2 3 4 2
> #解靴带法
> sample(y , 2 ) #非放回地 从y中抽取2项
[1] 3 5
> sample(y , 2 , replace = TRUE)
[1] 1 5
 #merge函数的参数如果是向量的话,做笛卡尔积
#标准化和中心化
> x <- c(rnorm(2),2*rnorm(3))
> x
[1] -0.02973143 -0.29157917 -1.18222232  2.71501078
[5]  0.05909058
> m <- scale(x , scale = F) #只中心化
> m
           [,1]
[1,] -0.2838451
[2,] -0.5456929
[3,] -1.4363360
[4,]  2.4608971
[5,] -0.1950231
attr(,"scaled:center")
[1] 0.2541137
> n <- scale(x , center = F)  #只标准化
> n
            [,1]
[1,] -0.01997882
[2,] -0.19593437
[3,] -0.79442571
[4,]  1.82442366
[5,]  0.03970748
attr(,"scaled:scale")
[1] 1.488147
#使用function函数定义二元函数的格式
> f <- function(x , y){r <- sqrt(x^2 + y^2) ; 10*sin(r)/r }
#如何把行向量转化成列向量,使用t(t(x))
> x <- 1:5
> x
[1] 1 2 3 4 5
> t(x)
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
> t(t(x))
     [,1]
[1,]    1
[2,]    2
[3,]    3
[4,]    4
[5,]    5
#两个向量做内积,方法一
> y=t(t(x)) #y是一个列向量,x是行向量
> crossprod(x,y)
     [,1]
[1,]   55
> crossprod(y , x)
     [,1]
[1,]   55
#内积的方法二
> x%*%y
     [,1]
[1,]   55
> y%*%x
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    2    4    6    8   10
[3,]    3    6    9   12   15
[4,]    4    8   12   16   20
[5,]    5   10   15   20   25
> y%o%x
, , 1

     [,1]
[1,]    1
[2,]    2
[3,]    3
[4,]    4
[5,]    5

, , 2

     [,1]
[1,]    2
[2,]    4
[3,]    6
[4,]    8
[5,]   10

, , 3

     [,1]
[1,]    3
[2,]    6
[3,]    9
[4,]   12
[5,]   15

, , 4

     [,1]
[1,]    4
[2,]    8
[3,]   12
[4,]   16
[5,]   20

, , 5

     [,1]
[1,]    5
[2,]   10
[3,]   15
[4,]   20
[5,]   25

> x<- -1+1i #定义复数,方法1
> y = complex(1 , 1) #定义复数,方法2
> x==y
[1] FALSE
> Mod(x) #计算复数x的模长
[1] 1.414214
> Conj(x) #取复数x的共轭
[1] -1-1i
> diag(x = x , nrow = 3)
      [,1]  [,2]  [,3]
[1,] -1+1i  0+0i  0+0i
[2,]  0+0i -1+1i  0+0i
[3,]  0+0i  0+0i -1+1i
> #eigen()函数用于计算特征值
> #构造上三角或者下三角矩阵
> m <- matrix (1:16 , 4 ,4)
> lower.tri(m)
      [,1]  [,2]  [,3]  [,4]
[1,] FALSE FALSE FALSE FALSE
[2,]  TRUE FALSE FALSE FALSE
[3,]  TRUE  TRUE FALSE FALSE
[4,]  TRUE  TRUE  TRUE FALSE
> m[lower.tri(m)] <- 0
> m
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    0    6   10   14
[3,]    0    0   11   15
[4,]    0    0    0   16
#开立方根
> "^"(27 , 1/3)
[1] 3
> #幂函数运算
> #求余数使用%% ; %/%表示整除
> #求矩阵各行(列)的均值
> #方法一 使用apply函数> #方法二 使用colMeans 或者 rowMeans函数
> m <- 4 ; n <- 5
> A <- matrix(1:m*n , m , n)
> matrix(apply(A , 2 , mean))
     [,1]
[1,] 12.5
[2,] 12.5
[3,] 12.5
[4,] 12.5
[5,] 12.5
> matrix(apply(A , 2 , mean) , m , n)
     [,1] [,2] [,3] [,4] [,5]
[1,] 12.5 12.5 12.5 12.5 12.5
[2,] 12.5 12.5 12.5 12.5 12.5
[3,] 12.5 12.5 12.5 12.5 12.5
[4,] 12.5 12.5 12.5 12.5 12.5
> #计算组合数使用choose函数;
> choose(5,3)
[1] 10
> combn(5,3) #列出所有组合
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    1    1    1    1    1    2    2    2     3
[2,]    2    2    2    3    3    4    3    3    4     4
[3,]    3    4    5    4    5    5    4    5    5     5
> factorial(5) #计算阶乘
[1] 120
> #求导数,使用D(function , “variable_name”)
> f1 <- expression(sin(x)*x)
> D(f1 , "x")
cos(x) * x + sin(x)
#求方程的根,使用uniroot函数(二分法)
>  f <- function(x)x^3 - 2*x - 1
> uniroot(f , c(0,2))
$root
[1] 1.618018

$f.root
[1] -9.17404e-05

$iter
[1] 6

$init.it
[1] NA

$estim.prec
[1] 6.103516e-05
#下面看一下uniroot函数的弊端--无法求本身是极值的零解
> f <- function(x)x^2 + 2*x +1
> uniroot(f , c(-2 , 2))
Error in uniroot(f, c(-2, 2)) : 位于极点边的f()值之正负号不相反
#解决方法,使用optimize函数
> optimize(f , c(-2 , 2)) #第一个参数是函数,第二个参数是初始区间
$minimum
[1] -1

$objective
[1] 0

> #可见,uninroot函数使用的是二分法求解根
> #在分布前的字母含义:d--概率密度函数;p--累积分布函数;r--产生该分布的随机数;q表示分位数函数
> help.search("distribution") #可以查各个分布的表达,列在下表
> #计算N(3,1)分布下,P(2<=X<=5)值
> pnorm(5 , 3 , 1) - pnorm(2 , 3 , 1)
[1] 0.8185946
#如何更改大小写
> x <- "MiXeD"
> tolower(x)
[1] "mixed"
> x <- "mIXed 123"
> toupper(x)
[1] "MIXED 123"
> letters[1:4]
[1] "a" "b" "c" "d"
#循环for的使用
> for(var in letters[1:5])
+ {x <- var; print(paste("foo" , var , sep = ""))}
[1] "fooa"
[1] "foob"
[1] "fooc"
[1] "food"
[1] "fooe"
#计数字符串中有多少个字母
> month.name[5]
[1] "May"
> nchar(month.name[4])
[1] 5
#取出子字符串,注意是闭区间!索引是从0开始!不同于python
> substr("abcde" , 2 , 4)
[1] "bcd"
> substr("abcde" , 2 , 3)
[1] "bc"
#取出子字符串,方法2
> substring("abdcdef" , 1:5 ,1:3)
[1] "a" "b" "d" ""  "" 
#正则表达式
> grep("J." , month.abb)
[1] 1 6 7 #January June July
> #使用“.”匹配任意字符;使用"\.来匹配"."
> help("regex") #regulation expression
> #使用as.Data(),as.POSIXet()函数将字符串日期转化成"Data"类型数据,可以进行运算
> d1 <- c("06/29/07")
> d1
[1] "06/29/07"
> D1 <- as.Date(d1 , "%m/%d/%y")
> D1
[1] "2007-06-29"
> d2 <- c("07/02/07")
> D2 <- as.Date(d2 , "%m/%d/%y")
> D2
[1] "2007-07-02"
> D2-D1  #在这种日期格式下,可以做加减
Time difference of 3 days
> difftime(D1 , D2 , units = "days") 
Time difference of -3 days
> format(Sys.Date() , format = "%A , %d %B %Y") #在这种格式,返回当前系统时间
[1] "星期五 , 10 一月 2020"

> dev.new()
NULL
> hist(rnorm(25) , col = "VioletRed") #画出直方图
> dev.off()
RStudioGD 
        2 
        
> dev.new()
NULL
> layout(matrix(c(1,1,1,2,3,4,2,3,4) , nr = 3 , byrow = T))
> hist(rnorm(25) , col = "VioletRed")
> hist(rnorm(25) , col = "VioletRed")
> hist(rnorm(25) , col = "VioletRed")
> hist(rnorm(25) , col = "VioletRed")
> hist(rnorm(25) , col = "VioletRed")
> dev.off()
RStudioGD 
        2 
#绘图时使用par函数可以划分绘图区域        
分布 R函数(参数)
beta beta(shape1 ,shape2)
二项 binom(size,prob)
x^2 chisq(df)
均匀 unif(max,min)
指数 exp(rate)
F f(df1 ,df2)
伽马 gamma(shape,scale)
超几何 hyper(m,n,k)
正态 norm(mean,std)
泊松 pois(lamda)
t t(df)
威布尔 weibull(shape,scale)
发布了109 篇原创文章 · 获赞 30 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43448491/article/details/103922514
今日推荐