简体   繁体   中英

Problem with row-wise operation in base R

I have a problem with performing row-wise operations using 'apply' function in R. I want to calculate the distance between two points:

d <- function(x,y){
length <- norm(x-y,type="2")
as.numeric(length)
}

The coordinates are given by two dataframes:

start <- data.frame(
a = c(7, 5, 17, 1), 
b = c(5, 17, 1, 2))

stop <- data.frame( 
b = c(5, 17, 1, 2),
c = c(17, 1, 2, 1))

My point is to calculate successive distances given by start and stop coordiantes. I wish it worked like:

d(start[1,], stop[1,])
d(start[2,], stop[2,])
d(start[3,], stop[3,])
etc...

I have tried:

apply(X = start, MARGIN = 1, FUN = d, y = stop)

which brought some strange results. Can you please help me with finding the proper solution? I know how to perform the operation using dplyr rowwise() function, however my wish is to use base only. Can you also explain me why did I receive such a strange results with apply()?

Loop over the sequence of rows and apply the d

sapply(seq_len(nrow(start)), function(i) d(start[i,], stop[i,]))
[1] 12.165525 20.000000 16.031220  1.414214

Or if we want to use apply , create a single data by cbind ing the two data and then subset by indexing

apply(cbind(start, stop), 1, FUN = function(x) d(x[1:2], x[3:4]))
[1] 12.165525 20.000000 16.031220  1.414214

Or may use dapply for efficiency

library(collapse)
dapply(cbind(start, stop), MARGIN = 1, parallel = TRUE,
   FUN = function(x) d(x[1:2], x[3:4]))
[1] 12.165525 20.000000 16.031220  1.414214

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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