[R] [External] Re: Selecting a minimum value of an attribute associated with point values neighboring a given point and assigning it as a new attribute
Ivan Krylov
kry|ov@r00t @end|ng |rom gm@||@com
Wed Nov 9 09:27:30 CET 2022
В Wed, 9 Nov 2022 02:38:15 +0000
"Duhl, Tiffany R." <Tiffany.Duhl using tufts.edu> пишет:
> MinConc <- function(x, lst, pts) {
> Concs <- lapply(lst, function(p) {
> pts$Conc[p]
> })
> return(min(Concs[[1]]))
> }
This function doesn't seem to use its first argument, x. Moreover,
its return value seems to only depend on the first element of its
second argument, lst.
> ###But I need the length of the list the function is applied to
> ###to be variable, depending on the length of the input csv file
> ###unlike the dummy variable dataframe that Eric used with a set
> ###length
> ###So I changed the "x" argument in lapply to "pts$X" but
> ###that generates an empty list
>
> Conc_min <- lapply(pts$X, function(i){
> MinConc(i, dist_matrix[i], pts)
> })
So instead of doing extra work to obtain the list indices (use
seq_along if you do need them) and to subset the one-element lists
returned by dist_matrix[i] (use dist_matrix[[i]] to get the value
instead of one-element list containing the value), it should be
possible to perform
Conc_min <- sapply(
dist_matrix,
function(neighbours) min(pts$Conc[neighbours])
)
This assumes that dist_matrix is of the same length as pts$X.
Admittedly, the example above can't consider each point by itself,
which isn't mentioned in dist_matrix (as far as I'm aware), so to
include the original point, we can run
mapply(
function(point, neighbours) min(pts$Conc[point], pts$Conc[neighbours]),
seq_along(pts$X), dist_matrix
)
--
Best regards,
Ivan
More information about the R-help
mailing list