简体   繁体   English

如何将函数应用于data.frame中的行对,类似于diff?

[英]How to apply a function to pairs of rows in a data.frame, similar to diff?

I've got a data frame that represents points in a plane. 我有一个表示平面中点的数据框。

     x     y
1  0.0   0.0
2  0.2   0.0
3  0.3  -0.1
...

I want to write a diff -like function for that data. 我想为该数据编写类似diff的函数。 I've written the distance function myDist (not Euclidean), but how can I apply it to the data frame? 我写了距离函数myDist (不是Euclidean),但是如何将它应用到数据框?

That is: By calling someFunction( myData, myDist ) , I want to obtain a vector with the values myDist(myData[1,],myData[2,]) , myDist(myData[2,],myData[3,]) , myDist(myData[3,],myData[4,]) , ... 那就是:通过调用someFunction( myData, myDist ) ,我想获得一个值为myDist(myData[1,],myData[2,])myDist(myData[2,],myData[3,])myDist(myData[3,],myData[4,]) ,...

Is there a function that does this, or do I have to write a loop? 有没有这样做的功能,还是我必须写一个循环?

Solved it with lapply : 解决它与lapply

lapply(seq_len(nrow(myData) - 1),
    function(i){
        myDist( myData[i,], myData[i+1,] )
    })

You could use head(myData, -1) and tail(myData, -1) to get 2 datasets with the rows lined up the way you want, then if myDist is vectorized you would just pass the results to myDist , if it is not vectorized you could use mapply (or the Vectorize function), or sapply , lapply , apply , loops or other functionality. 您可以使用head(myData, -1)tail(myData, -1)来获取2个数据集,其中的行按照您想要的方式排列,然后如果myDist被矢量化,您只需将结果传递给myDist ,如果不是vectorized你可以使用mapply (或Vectorize函数),或sapplylapplyapply ,loops或其他功能。

myData <- data.frame( x=1:10, y=sample(1:10), z=rnorm(10) )

myDist <- function( one, two ) {
    sqrt( rowSums( (one-two)^2 ) )
}

myDist( head(myData,-1), tail(myData,-1) )

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

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