apply() is slow - how to make it faster or what are my alternatives?

前端 未结 3 2190
面向向阳花
面向向阳花 2020-12-14 03:33

I have a quite large data frame, about 10 millions of rows. It has columns x and y, and what I want is to compute

hypot <- funct         


        
3条回答
  •  余生分开走
    2020-12-14 04:07

    R is vectorised, so you could use the following, plugging in your own matrix of course

    X = t(matrix(1:4, 2, 2))^2
    >      [,1] [,2]
     [1,]    1    4
     [2,]    9   16
    
    rowSums(X)^0.5
    

    Nice and efficient :)

提交回复
热议问题