R语言学习笔记一

菜鸟教程-R语言

基础运算

> # 四舍五入
> round(1.125465, 2)
[1] 1.13
> round(1.125465, 5) #当取整位是偶数的时候,五也会被舍去
[1] 1.12546
> ceiling(5.5) # 对n向上取整
[1] 6
> floor(5.5) # 对n向下取整
[1] 5
>
> # 三角函数-弧度制
> sin(pi/6)
[1] 0.5
> cos(pi/4)
[1] 0.7071068
> tan(pi/3)
[1] 1.732051
>
> # 反三角函数
>asin(0.5)
[1] 0.5235988
> acos(0.7071068)
[1] 0.7853981
> atan(1.732051)
[1] 1.047198
> # 分布函数
> dnorm(0) #d-概率密度函数
[1] 0.3989423
> pnorm(0) #p-概率密度积分函数(从无限小到x的积分)
[1] 0.5
> qnorm(0.95) # q-分位数函数
[1] 1.644854
> rnorm(3, 5, 2) #r-随机数函数-用于概率仿真,产生3个平均值为5,标准差为2的正态随机数
[1] 5.929230 6.007384 6.948097

数据类型

  • 向量(vector)
    • c()是创造向量的函数,R语言的下标不代表偏移量,代表第几个,从1开始
    > a = c(10,20,30,40,50)
    > a[2] #取第2个
    [1] 20
    > a[1:4] #取第1到第4,第1和第4均包含
    [1] 10 20 30 40
    > a[c(1, 3, 5)] #取第1,第3,第5
    [1] 10 30 50
    > a[c(-1, -5)] #去掉第1和第5
    [1] 20 30 40
    # 支持标量计算
  • 列表(list)
  • 矩阵(matrix)
  • 数组(array)
  • 因子(factor)
  • 数据框(data.frame)

绘图系统
R语言绘图参考链接1
最常用的三个包:graphics、lattice、ggplot2

>demo(graphics)
> library(lattice)
> xyplot(Temp~Ozone|factor(Month),data=airquality,main="Temp(F) vx Ozone(ppb) by Month",layout=c(5,1))

image
R语言不存在叫'ggplot2'这个名字的程辑包的解决方法
geom_point使用方法说明

> ggplot(data=diamonds, mappings=aes(x=carat, y=price))+geom_point(aes(color=clarity))
Error in `check_required_aesthetics()`:
! geom_point requires the following missing aesthetics: x and y
Run `]8;;rstudio:run:rlang::last_error()rlang::last_error()]8;;` to see where the error occurred.
> # 删除mappings,修改成以下命令
>>ggplot(data=diamonds, aes(x=carat, y=price))+geom_point(aes(color=clarity))

image

> # 添加统计变换,如两变量关系的平滑曲线
> ggplot(data=diamonds, aes(x=carat, y=price))+geom_point(aes(color=clarity))+stat_smooth()

image

> #分析不同切工(cut)下克拉数与价格的关系(类似于lattice中的条件多框图)
> ggplot(data=diamonds, aes(x=carat, y=price))+geom_point(aes(color=clarity))+stat_smooth()+facet_wrap(~cut)

image

ggplot2的基本概念

  • 数据(data)和映射(mapping)
  • 几何对象(geometric)
  • 标度(scale)
  • 统计变换(statistics)
  • 坐标系统(coordinate)
  • 分面(facet)

R Graph Gallery

R Shiny

posted @   CestLaVie0019  阅读(547)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
点击右上角即可分享
微信分享提示