R学习01

来源:互联网 发布:全速网络办公 编辑:程序博客网 时间:2024/06/08 00:11

入门

  1. 下载网址 R
    选择windows版本->base->选择默认安装风格
  2. 下载网址 Rstudio
  3. 常用获取package的地方
    CRAN/Bioconductor/Github
    • CRAN上,在命令行输入
install.packages(“包名”)
  • Github上,在命令行输入
install_github(“”)
  • http://mirrors.opencas.org/cran/官网上也能直接下载包,但一般直接就rstudio中以命令行下载即可。
    4.使用包
library(包名) #引用某个包data()        #查看该package下的数据集list?数据集名     #查看某一数据集的详细信息

5.获取帮助

  • ?函数名(R的帮助文档)
  • Google/Stackoverflow
  • 如何问问题
#操作系统、版本、哪一步产生的错误、预期是什么、得到的结果是什么、其他有用信息  Win7 R 3.2.0 lm()  "seg fault on large data frame"
  1. 补充

    • www.kaggle.com提供大数据竞赛的平台
    • rpubs.com类似于github的代码发布平台

使用

>getwd() #list the current directory>setwd("f:/temp") #change the current directory to f:/temp>#if f:/temp didn't exsits could use dir.create()>dir.create("temp") #注意前提是当前工作目录已在f:/下>#一系列代码xxx>savehistory() #将comman history保存在当前目录下(设置的或默认的)>save.image() #将工作空间保存>q() #退出>#一些其他命令>loadhistory("myfile") #reload a command's history (default=.Rhistory)>load("myfile") #load a worksapce into the current session(default=.RData)>save(objectlist,file="myfile") #save specific objects to a file)

It’s a good idea to keep your projects in separate directories.I typically start an R session by issuing the setwd() command whit the appropriate path to a project,followed by the load() command without options.This lets me start up where I left off in my last session and keeps the data and settings separate between projects.On Windows and Mac OS X platform,it’s even easier.Just navigate to the project directory and double-click on the saved image file(.RData). Doing so will start R,load the saved workspace,and set the current working directory to this location.——《R in Action》

输入输出

>source("xx.R")  #引入一段脚本文件

文本重定向

>sink()  #不带参数,默认是terminal输出>sink(filename="xx.txt",append=TRUE,split=TRUE)>#append表明以追加方式,split表明输出同时到文件和terminal>#小例子> sink("tp.txt")  #writ all output to file tp.txt> for (i in 1:5) print(i);> sink()  #stop sinking, =sink(NULL)In the tp.txt, the content is:[1] 1[1] 2[1] 3[1] 4[1] 5> sink.number()[1] 0> for (i in 1:5) print(i) #no sinking, then print to screen[1] 1[1] 2[1] 3[1] 4[1] 5> unlink("tp.txt")  #delete the file tp.txt

图形输出

>#sink()不对图形起作用>#保存一个图片的例子>png("myplot.png")>x<-(1,2,3)>hist(x)>dev.off()

还有很多图形输出函数,需要时请查找相关资料