查看原文
其他

R语言基础(一)

pythonic生物人 pythonic生物人 2022-09-11

"pythonic生物人"的第84篇分享

本文简要梳理R语言的帮助函数、工作空间、IO、模块安装等基础知识。

本文速览

1、R中帮助函数
打开R帮助文档首页
查看模块ggplot2的帮助文档
输出某个函数demo

2、R中工作空间workspace常用函数 
列出历史输入命令
保存当前工作空间
导入已保存的工作空间
获取当前工作路径
切换工作路径
退出R

3、输入与输出
windows下执行R脚本
R中将图形输入为pdf、png、svg、jpeg等格式 
从R中导出数据保存为txt格式
从R中导出数据保存为excel格式

4、包安装
手动选镜像
options设置镜像

1、R中帮助函数

  • 打开R帮助文档首页

help.start()

  • 查看模块ggplot2的帮助文档

help(ggplo2)或者?ggplot2

  • 输出某个函数demo

example(summary)

summry> summary(attenu, digits = 4#-> summary.data.frame(...), default precision
     event            mag           station         dist            accel        
 Min.   : 1.00   Min.   :5.000   117    :  5   Min.   :  0.50   Min.   :0.00300  
 1st Qu.: 9.00   1st Qu.:5.300   1028   :  4   1st Qu.: 11.32   1st Qu.:0.04425  
 Median :18.00   Median :6.100   113    :  4   Median : 23.40   Median :0.11300  
 Mean   :14.74   Mean   :6.084   112    :  3   Mean   : 45.60   Mean   :0.15422  
 3rd Qu.:20.00   3rd Qu.:6.600   135    :  3   3rd Qu.: 47.55   3rd Qu.:0.21925  
 Max.   :23.00   Max.   :7.700   (Other):147   Max.   :370.00   Max.   :0.81000  
                                 NA's   : 16                                     

summry> summary(attenu $ station, maxsum = 20) #-> summary.factor(...)
    117    1028     113     112     135     475    1030    1083    1093    1095     111     116    1219    1299     130    1308    1377    1383 (Other)    NA'

      5       4       4       3       3       3       2       2       2       2       2       2       2       2       2       2       2       2     120      16 

summry> lst <- unclass(attenu$station) > 20 # logical with NAs

summry> ## summary.default() for logicals -- different from *.factor:
summry> summary(lst)
   Mode   FALSE    TRUE    NA's 
logical      28     138      16 

summry> summary(as.factor(lst))
FALSE  TRUE  NA'

   28   138    16 

2、R中工作空间workspace常用函数

  • 列出历史输入命令

history()

  • 保存当前工作空间

save.image(file='first_r.Rdata')

  • 导入已保存的工作空间

load('first_r.Rdata')

  • 获取当前工作路径

getwd()

[1] "C:/Users/helen/Desktop/《Python基础教程(第3版)》/R"类似linux中的pwd,输出绝对路径

  • 切换工作路径

setwd("C:/Users/helen/Desktop/《Python基础教程(第3版)》")
getwd()

[1] "C:/Users/helen/Desktop/《Python基础教程(第3版)》"

  • 退出R

q()

将会询问你是否保存工作空间


3、输入与输出

  • windows下执行R脚本

sourece('R脚本')

  • R中将图形输入为pdf、png、svg、jpeg等格式

1st.R脚本

library(ggplot2)
#pdf("first.pdf")#保存为pdf格式,置于绘图之前
png(file = "first.png", width = 400, height = 350)
print(ggplot(mpg, aes(x = displ, y = hwy)) + 
  geom_point())#注意使用pdf,否则会显示报错:用R语言保存的pdf文件打不开,显示“文件已被损坏,且无法修复”
dev.off( )

source('1st.R')

  

  • 从R中导出数据保存为txt格式

> x<-c(1,2,3)
> x
[1] 1 2 3
> write.table(x,"first.txt",sep = "\t")

  • 从R中导出数据保存为excel格式

x<-c(1,2,3)
library(xlsx) 
write.xlsx(x, "first.xlsx")

4、包安装

  • 手动选镜像

install.packages('ggplot')

  • options设置镜像

options(repos=structure(c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")))#设置清华镜像,加速安装
install.packages("pheatmap", dependencies = TRUE)

其它国内镜像:

https://mirrors.tuna.tsinghua.edu.cn/CRAN/TUNA Team, Tsinghua University
https://mirrors.bfsu.edu.cn/CRAN/Beijing Foreign Studies University
https://mirrors.ustc.edu.cn/CRAN/University of Science and Technology of China
https://mirror-hk.koddos.net/CRAN/KoDDoS in Hong Kong
https://mirrors.e-ducation.cn/CRAN/Elite Education
https://mirror.lzu.edu.cn/CRAN/Lanzhou University Open Source Society
https://mirrors.nju.edu.cn/CRAN/eScience Center, Nanjing University
https://mirrors.tongji.edu.cn/CRAN/Tongji University
https://mirrors.sjtug.sjtu.edu.cn/cran/Shanghai Jiao Tong University

阿里云镜像:https://mirrors.aliyun.com/CRAN/


参考资料

r-in-action-second-edition

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存