美文网首页
《Learning R》笔记 Chapter 14 Explor

《Learning R》笔记 Chapter 14 Explor

作者: 天火燎原天 | 来源:发表于2018-02-25 20:44 被阅读0次

Explore

在探索连续型数据时,sd、var、range等都是常用函数。先cut,再table则可以将连续转为离散再进行观察。
在比较两个多更多等长的numeric vector时,pmin()和pmax()可以输出再每一个位置最小/最大的值。

> (x=sample(10,10));(y=sample(10,10));(z=sample(10,10))
 [1]  5  8  2  4  6  9  1  3  7 10
 [1]  4  5  8  7 10  2  6  3  9  1
 [1] 10  9  2  1  7  8  3  4  6  5
> pmin(x,y,z)
 [1] 4 5 2 1 6 2 1 3 6 1

cummin和cummax接受一个vector,输入这个vector的最小/最大 so far的值。

> cummax(x)
 [1]  5  8  8  8  8  9  9  9  9 10

quantile()函数则提供了vector的xxx%分位:

> (x=rnorm(20))
 [1] -0.97392547 -2.68243940 -0.03796838 -0.65249979 -0.28756329  0.38868737  0.68847986         -0.43226118
 [9]  1.55034408  1.30703724  0.01662464 -0.46428297 -0.12325135  1.20905396 -1.03437545  1.26728394
[17] -0.42276944  0.10972380  2.16093479 -0.54529102
> quantile(x)
         0%         25%         50%         75%        100% 
-2.68243940 -0.48453498 -0.08060986  0.81862339  2.16093479 
> quantile(x,c(.1,.3,.5,.7,.9))
        10%         30%         50%         70%         90% 
-0.97997047 -0.44186772 -0.08060986  0.47862511  1.33136792 

fivenum()是quantile的一个速度优化版本,特定输出5个分位(最小,最大,中值,25%,75%)的值。

> fivenum(x)
[1] -2.68243940 -0.50478700 -0.08060986  0.94876691  2.16093479

Plotting

ggplot2在绘制barplot时,要让bar不堆积(默认设置‘stack’),应该如下指令。position的其他参数还有‘fill’

geom_bar(stat = 'identity' , position = 'dodge')

要转置整个图像,使用如下命令:

coord_flip()

相关文章

网友评论

      本文标题:《Learning R》笔记 Chapter 14 Explor

      本文链接:https://www.haomeiwen.com/subject/wizrxftx.html