使用R的apply函数之一简化代码

Kub*_*kar 3 r apply dataframe

我找不到令人满意的教程,可以解释我如何使用应用函数的所有可能性.我仍然是一个新手,但这通常会派上用场,并显着简化我的代码.所以这是我的例子......我有一个看起来像这样的数据框:

> head(p01)
   time key dwell
1   8.13   z  0.00
3   8.13   x  1.25
5   9.38   l  0.87
7  10.25   x  0.15
9  10.40   l  1.13
11 11.53   x  0.45
Run Code Online (Sandbox Code Playgroud)

进入R:

p01 <- structure(list(time = c(8.13, 8.13, 9.38, 10.25, 10.4, 11.53), 
key = c("z", "x", "l", "x", "l", "x"), dwell = c(0, 1.25, 
0.869, 0.15, 1.13, 0.45)), .Names = c("time", "key", "dwell"), row.names = c(1L, 3L, 5L, 7L, 9L, 11L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)

现在我想计算每个字母的出现次数p01$key并打印出来p01$occurences,这样结果看起来像这样:

    time key dwell occurences
1   8.13   z  0.00          1
3   8.13   x  1.25          3
5   9.38   l  0.87          2
7  10.25   x  0.15          3
9  10.40   l  1.13          2
11 11.53   x  0.45          3
Run Code Online (Sandbox Code Playgroud)

我现在这样做的方式是:

p01[p01$key == "l", "occurences"] <- table(p01$key)["l"]
p01[p01$key == "x", "occurences"] <- table(p01$key)["x"]
p01[p01$key == "z", "occurences"] <- table(p01$key)["z"]
Run Code Online (Sandbox Code Playgroud)

......当然这不是最好的解决方案.特别是因为真实数据包含更多可能性p01$key(16个不同字母之一).

最重要的是,我想计算dwell每个字母的总数,所以我现在正在做的是:

p01[p01$key == "l", "total_dwell"] <- tapply(p01$dwell, p01$key, sum)["l"]
p01[p01$key == "x", "total_dwell"] <- tapply(p01$dwell, p01$key, sum)["x"]
p01[p01$key == "z", "total_dwell"] <- tapply(p01$dwell, p01$key, sum)["z"]
Run Code Online (Sandbox Code Playgroud)

为了得到:

    time key dwell total_dwell
1   8.13   z  0.00        0.00
3   8.13   x  1.25        1.85
5   9.38   l  0.87        2.00
7  10.25   x  0.15        1.85
9  10.40   l  1.13        2.00
11 11.53   x  0.45        1.85
Run Code Online (Sandbox Code Playgroud)

在过去的6个小时里,我一直在谷歌上搜索并阅读几本书.真的很感激优雅的解决方案和/或一些综合教程的链接.我的解决方案显然有效,但这不是我第一次解决这个问题,我的脚本文件开始看起来很荒谬!

Rol*_*and 10

如果您的数据集很大,请尝试data.table.

library(data.table)
DT <- data.table(p01)
DT[,occurences:=.N,by=key]
DT[,total_dwell:=sum(dwell),by=key]

    time key dwell occurences total_dwell
1:  8.13   z 0.000          1       0.000
2:  8.13   x 1.250          3       1.850
3:  9.38   l 0.869          2       1.999
4: 10.25   x 0.150          3       1.850
5: 10.40   l 1.130          2       1.999
6: 11.53   x 0.450          3       1.850
Run Code Online (Sandbox Code Playgroud)

通过引用分配的两行可以组合如下:

DT[, `:=`(occurences = .N, total_dwell = sum(dwell)), by=key]
Run Code Online (Sandbox Code Playgroud)

  • 实际上,当你习惯它时,data.table语法更容易进行这种操作. (5认同)

Pau*_*tra 6

我用的是plyr:

res = ddply(p01, .(key), transform, 
                           occurrences = length(key), 
                           total_dwell = sum(dwell))
res
   time key dwell occurrences total_dwell
1  9.38   l 0.869           2       1.999
2 10.40   l 1.130           2       1.999
3  8.13   x 1.250           3       1.850
4 10.25   x 0.150           3       1.850
5 11.53   x 0.450           3       1.850
6  8.13   z 0.000           1       0.000
Run Code Online (Sandbox Code Playgroud)

请注意,在此之后,表按字母顺序排序key.你可以order用来求助time:

res[order(res$time),]
   time key dwell occurrences total_dwell
3  8.13   x 1.250           3       1.850
6  8.13   z 0.000           1       0.000
1  9.38   l 0.869           2       1.999
4 10.25   x 0.150           3       1.850
2 10.40   l 1.130           2       1.999
5 11.53   x 0.450           3       1.850
Run Code Online (Sandbox Code Playgroud)