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))
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))
> # 添加统计变换,如两变量关系的平滑曲线 > ggplot(data=diamonds, aes(x=carat, y=price))+geom_point(aes(color=clarity))+stat_smooth()
> #分析不同切工(cut)下克拉数与价格的关系(类似于lattice中的条件多框图) > ggplot(data=diamonds, aes(x=carat, y=price))+geom_point(aes(color=clarity))+stat_smooth()+facet_wrap(~cut)
ggplot2的基本概念
- 数据(data)和映射(mapping)
- 几何对象(geometric)
- 标度(scale)
- 统计变换(statistics)
- 坐标系统(coordinate)
- 分面(facet)
本文来自博客园,作者:CestLaVie0019,转载请注明原文链接:https://www.cnblogs.com/fishwithsheep/p/16506038.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)