You need to operate on each row of df1
(or mat
) in order to figure out, for each row how far away each object in df2
is. From that, you can pick the ones that meet your distance criterion.
I think you're getting a little confused about the use of apply
and about the use of which
. To really have which
work for you, you need to apply it to each row of mat
whereas your current code applies it to the entire mat
matrix. Also note that it is hard to use apply
here because you're comparing each row of mat
against a corresponding element of a vector defined by ((df1$cat)^2)*1000)
. So, I will instead show you examples using sapply
and lapply
. You could also use mapply
here, but I think the sapply
/mapply
syntax is clearer.
To address your desired output, I show two examples. One returns a list containing, for each row in df1
, the names of items in df2
that are within the distance threshold. This won't easily go back into your original df1
as a variable because each element in the list can contain multiple names. The second example pastes those names together as a single comma-separated character string in order to create the new variable you're looking for.
Example 1:
out1 <- lapply(1:nrow(df1), function(x) {
df2[which(mat[x,] < (((df1$cat)^2)*1000)[x]),'latlonloc']
})
Result:
> str(out1)
List of 20
$ : Factor w/ 6 levels "Boelenslaan",..:
$ : Factor w/ 6 levels "Boelenslaan",..:
$ : Factor w/ 6 levels "Boelenslaan",..:
$ : Factor w/ 6 levels "Boelenslaan",..:
$ : Factor w/ 6 levels "Boelenslaan",..:
$ : Factor w/ 6 levels "Boelenslaan",..:
$ : Factor w/ 6 levels "Boelenslaan",..:
$ : Factor w/ 6 levels "Boelenslaan",..: 2
$ : Factor w/ 6 levels "Boelenslaan",..:
$ : Factor w/ 6 levels "Boelenslaan",..: 4
$ : Factor w/ 6 levels "Boelenslaan",..:
$ : Factor w/ 6 levels "Boelenslaan",..:
$ : Factor w/ 6 levels "Boelenslaan",..:
$ : Factor w/ 6 levels "Boelenslaan",..:
$ : Factor w/ 6 levels "Boelenslaan",..: 6 4 5
$ : Factor w/ 6 levels "Boelenslaan",..:
$ : Factor w/ 6 levels "Boelenslaan",..:
$ : Factor w/ 6 levels "Boelenslaan",..:
$ : Factor w/ 6 levels "Boelenslaan",..: 4
$ : Factor w/ 6 levels "Boelenslaan",..:
Example 2:
out2 <- sapply(1:nrow(df1), function(x) {
paste(df2[which(mat[x,] < (((df1$cat)^2)*1000)[x]),'latlonloc'], collapse=',')
})
Result:
> out2
[1] "" ""
[3] "" ""
[5] "" ""
[7] "" "Borgercompagnie"
[9] "" "Garrelsweer"
[11] "" ""
[13] "" ""
[15] "Tinallinge,Garrelsweer,Stitswerd" ""
[17] "" ""
[19] "Garrelsweer" ""
I think the second of these is probably closest to what you're going for.