简体   繁体   English

从小标题的第1行减去第2行

[英]Subtracting Row 2 from Row 1 in a Tibble

I have a tibble object in R, which looks like the following: 我在R中有一个小对象,如下所示: 在此处输入图片说明

For each column, I want to subtract row1 from row2. 对于每一列,我想从row2中减去row1。 My current approach is below: 我当前的方法如下:

mean.diff <- each.group.mean[1,2:num.groups]-each.group.mean[2,2:num.centroids]

What is confusing is that to execute this statement on the 2x9660 tibble, the code takes 8-15 secs, as determined by a Sys.time() calculation. 令人困惑的是,要在2x9660上执行此语句,代码需要8到15秒,这是由Sys.time()计算确定的。 Would appreciate any help to make this faster and to understand why this command takes so long. 感谢您提供任何帮助,以加快此速度并理解为什么此命令需要这么长时间。

By unlist ing, we could convert this to a vector and make it more efficient 通过unlist ,我们可以将其转换为vector并使其更有效

unlist(each.group.mean[1,2:num.groups], use.names = FALSE)-
    unlist(each.group.mean[2,2:num.centroids], use.names = FALSE)

The reason is that a data.frame or tibble etc have many class attributes that makes it slower to do the subset and then the computation. 其原因是,一个data.frametibble等有很多类的属性,使得它更慢做的子集,然后计算。 By converting to vector all those classes are stripped off. 通过转换为vector所有这些类都被剥离。 Using a reproducible example 使用可复制的示例

data 数据

set.seed(24)
df1 <- as.tibble(as.data.frame(matrix(sample(1:10, 9660*10, 
     replace = TRUE), ncol = 9660)))

Benchmarks 基准

system.time(df1[1,] - df1[2,])
#   user  system elapsed 
#   0.78    0.00    0.78 

system.time(unlist(df1[1,], use.names = FALSE) - unlist(df1[2,], use.names = FALSE))
#   user  system elapsed 
#   0.03    0.00    0.03 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM