В Wed, 9 Nov 2022 02:38:15 +0000 "Duhl, Tiffany R." <tiffany.d...@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 ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.