在R中执行逐列操作

ery*_*ery 2 loops r quantile

乡亲

我有一个建筑物区域的温度数据,如下所示:

Lines <- "Date,Zone01,Zone02
 01/01  01:00:00,24.5,21.3
 01/01  02:00:00,24.3,21.1
 01/01  03:00:00,24.1,21.1
 01/01  04:00:00,24.1,20.9
 01/01  05:00:00,25.,21.
 01/01  06:00:00,26.,21.
 01/01  07:00:00,26.6,22.3
 01/01  08:00:00,28.,24.
 01/01  09:00:00,28.9,26.5
 01/01  10:00:00,29.4,29
 01/01  11:00:00,30.,32.
 01/01  12:00:00,33.,35.
 01/01  13:00:00,33.4,36
 01/01  14:00:00,35.8,38
 01/01  15:00:00,32.3,37
 01/01  16:00:00,30.,34.
 01/01  17:00:00,29.,33.
 01/01  18:00:00,28.,32.
 01/01  19:00:00,26.3,30
 01/01  20:00:00,26.,28.
 01/01  21:00:00,25.9,25
 01/01  22:00:00,25.8,21.3
 01/01  23:00:00,25.6,21.4
 01/01  24:00:00,25.5,21.5
 01/02  01:00:00,25.4,21.6
 01/02  02:00:00,25.3,21.8"
Run Code Online (Sandbox Code Playgroud)

我想要做的是计算每个区域的第99百分位的温度.我会做这个命令:

Q=quantile(Lines$Zone01,0.99)
Run Code Online (Sandbox Code Playgroud)

但是,我必须手动为数据集中的每一列执行此操作.有没有办法让这个命令遍历所有列(从第二列开始)?

非常感谢.

And*_*rie 7

apply在这种情况下,使用家庭中的功能sapply:

> sapply(Lines[, -1], quantile, 0.99)
Zone01.99% Zone02.99% 
     35.20      37.75 
Run Code Online (Sandbox Code Playgroud)

您会注意到这样做的结果是将quantilegets附加到列名称.要删除它,请names=FALSE作为参数传递给quantile:

> sapply(Lines[, -1], quantile, 0.99, names=FALSE)
Zone01 Zone02 
 35.20  37.75 
Run Code Online (Sandbox Code Playgroud)


Cha*_*ase 5

Package plyr有一个漂亮的函数numcolwise,如果它是数字的话,它将在你的数据帧的每一列上运行.就像是:

library(plyr)
> numcolwise(function(x) quantile(x, .99))(dat)
    Zone01 Zone02
99%   35.2  37.75
Run Code Online (Sandbox Code Playgroud)

应该做的伎俩.

当然,您始终可以使用基本申请系列:

> apply(dat[, -1], 2, function(x) quantile(x, .99)) 
Zone01 Zone02 
 35.20  37.75
Run Code Online (Sandbox Code Playgroud)