学习笔记
R in Action
chapter 9
【attach】
R 的搜索路径是一种层状结构,当前搜索位置是1,通过函数attach()可以进入搜索位置2。
> install.packages("multcomp")
> library(multcomp)
> attach(cholesterol) #用 $
符号访问对象不方便的时候,用attach
> head(cholesterol)
trt response
1 1time 3.8612
2 1time 10.3868
3 1time 5.9059
4 1time 3.0609
5 1time 7.7204
6 1time 2.7139
> response #用attach之后,可以直接指定变量名字调用数据
[1] 3.8612 10.3868 5.9059 3.0609 7.7204 2.7139 4.9243 2.3039 7.5301 9.4123 10.3993
[12] 8.6027 13.6320 3.5054 7.7703 8.6266 9.2274 6.3159 15.8258 8.3443 13.9621 13.9606
[23] 13.9176 8.0534 11.0432 12.3692 10.3921 9.0286 12.8416 18.1794 16.9819 15.4576 19.9793
[34] 14.7389 13.5850 10.8648 17.5897 8.8194 17.9635 17.6316 21.5119 27.2445 20.5199 15.7707
[45] 22.8850 23.9527 21.5925 18.3058 20.3851 17.3071
> detach(cholesterol) #去除绑定,用detach
> response #此时再按变量名就找不到对象了
错误: 找不到对象'response'
参考:
http://www.biosino.org/pages/newhtm/r/schtml/attach_0028_0029-and-detach_0028_0029.html
【table】
> attach(cholesterol)
> trt
[1] 1time 1time 1time 1time 1time 1time 1time 1time 1time 1time 2times 2times
[13] 2times 2times 2times 2times 2times 2times 2times 2times 4times 4times 4times 4times
[25] 4times 4times 4times 4times 4times 4times drugD drugD drugD drugD drugD drugD
[37] drugD drugD drugD drugD drugE drugE drugE drugE drugE drugE drugE drugE
[49] drugE drugE
Levels: 1time 2times 4times drugD drugE
> table(trt) #统计各组样本的大小
trt
1time 2times 4times drugD drugE
10 10 10 10 10
【aggregate】
> aggregate(response,by=list(trt),FUN=mean) #对数据按by进行分组统计FUN的各种数值
Group.1 x
1 1time 5.78197
2 2times 9.22497
3 4times 12.37478
4 drugD 15.36117
5 drugE 20.94752
> aggregate(response,by=list(trt),FUN=sum)
Group.1 x
1 1time 57.8197
2 2times 92.2497
3 4times 123.7478
4 drugD 153.6117
5 drugE 209.4752
> aggregate(response,by=list(trt),FUN=max)
Group.1 x
1 1time 10.3868
2 2times 15.8258
3 4times 18.1794
4 drugD 19.9793
5 drugE 27.2445
参考:
http://www.biostatistic.net/thread-6303-1-1.html
http://blog.csdn.net/stat_elliott/article/details/41518565
【plotmeans】
> library(gplots)
> plotmeans(response~trt,xlab="Treatment",ylab="Response",main="Mean Plot with 95% CI")
#treatment means, with95 percent confidence limits