R语言_基础

a = c(1,2,3)

#查看帮助
help(mean)
?mean
example(mean)
help.start()
help(package="plyr")
help(baseball)  查看plyr包下面的baseball数据集的描述

#工作空间
getwd() 
setwd(“c:/myproject”) 
ls()    #列出当前工作空间中的对象
rm(list=ls())
dir()   #列出当前目录
dir.create(“c:/myproject”)
options()           #查看R软件默认选项的设置
options(digits=3)   #保留3为小数
data()  #展示已经加载的数据集
save.image("mylife")    #保存工作空间到mylife文件中
load("mylife")      #载入工作空间

#输入与输出
#重定向至文本文件和图像文件
source("inputFile.R")
sink("outFile",append=TRUE,split=TRUE)
pdf("outPic.pdf")
jpeg("outPic.jpg")
postscript("filename.ps")
#利用键盘编辑小数据
mydata = edit(mydata)
fix(mydata)
#读取文本文件
mydataframe = read.table(file,header=TRUE,sep="delimiter",row.names="name",stringsAsFactors=FALSE)
#一个例子
sink("myout.txt",split=TRUE)
pdf("myout.pdf")
source("s1.R")
sink()
dev.off()   #文本输出至myout.txt,图形输出至myout.pdf

#处理对象的实用函数
length
dim
str
class
mode
names
c(obj1,obj2)
cbind(obj1,obj2)
rbind(obj1,obj2)
head
tail
ls()
rm(obj1,obj2)

#批处理
R CMD BATCH options infile outfile #linux
"c:\program\r\r.exe" CMD BATCH --vanilla --slave "c:\myscript.R"

#查看变量属性
mode(a) 
class(a) 
is.numeric(a)
is.character(a)

#算术与逻辑运算
1 == 1
1 != 2
TRUE & TRUE
TRUE | FALSE
! TRUE

#数据集标注,采用值标签
patient$gender = factor(patient$gender,levels=c(1,2),labels=c("male","female"))


#全局统计
data
library(MASS)
data(Insurance)
str(Insurance)
names(Insurance)
summary(Insurance)



#抽样
sub1 = sample(nrow(Insurance),10,replace=TRUE)
Insurance[sub1,]        #利用抽样技术随机获取子集
index = sample(2,nrow(Insurance),replace=TRUE,prob=c(0.8,0.2))
Insurance[index==1,]
Insurance[index==2,]    #利用抽样技术对数据进行分区管理

#R基本画图plot()函数
#par()对R图形进行修饰
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
opar <- par(no.readonly = TRUE)
par(mfrow=c(1,2))
par(pin=c(2,3))
par(lwd=2, cex=1.5)
par(cex.axis=0.75, font.axis=3)
plot(dose, drugA, type=”b”, pch=19, lty=2, col=”red”)
plot(dose, drugB, type=”b”, pch=23, lty=6, col=”blue”, bg=”green”)
par(opar)
par(mfrow=c(1,1))

#R函数定义与实现
#R函数调用
sum.of.squares <- function(x,y) {
x^2 + y^2
}
sum.of.squares(3, 4)

参考资料

  1. 王路情博客—数据科学家
  2. 《R in action》
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值