我有动物位置数据。我试图为每只动物在每个位置周围20公里的缓冲区内创建随机位置。我有生成随机位置的工作代码(+一个已使用的位置(。我现在正试图将另一个for循环包裹在这个工作循环周围,这样我就可以在401只动物的数据帧中存储随机+使用过的位置。我不知道如何索引第二个数据帧,它需要为每只动物保存所有随机+使用的位置。
我的数据的一个子集看起来像这样,其中数据帧也是sf对象:
df <- data.frame(matrix(ncol = 17, nrow = 200))
colnames(df) <- c('id', 'tod_', 'x', 'y', 'time', 'distance', 'speed',
'tdif', 'heading', 'BirdsID', 'sex', 'month', 'date', 'hunting',
'dist.km', 'hunting2', 'diel')
dat_sf_utm <- st_as_sf(dat, coords = c("x", "y"), crs = 5070)
然后我开始for循环。大量注释。
# Create an empty data.frame to hold "random" and "used" points FOR EACH BIRD
temp2 <- data.frame(matrix(NA, nrow = 20 * ((nrow(dat_sf_utm)) - (length(unique(dat_sf_utm$BirdsID)))),
ncol = 20))
colnames(temp2) <- c("used", "x", "y", "distance", "BirdsID", "tod_", "time",
"tdif", "heading", "sex", "month", "date", "hunting", "dist.km",
"hunting2", "diel", "geometry", "id", "speed", "strata")
unique(dat_sf_utm$BirdsID)
for(j in unique(dat_sf_utm$BirdsID)){
trial <- dat_sf_utm %>%
group_by(BirdsID) %>% # Group by BirdsID
filter(BirdsID == j) %>% # filter BirdsID j
bind_cols(do.call(rbind, # Bind cols
(.$geometry)) %>% # Of "geometry"
as_tibble() %>% # And make them a tibble
setNames(c("x", "y")) # And call them xy
)
temp <- data.frame(matrix(NA, nrow = 20 * (nrow(trial)-1), # Make empty df to hold
ncol = 20)) # "random" and "used" locs
colnames(temp) <- c("used", "x", "y", "distance", "BirdsID", "tod_", "time",
"tdif", "heading", "sex", "month", "date", "hunting", "dist.km",
"hunting2", "diel", "geometry", "id", "speed", "strata")
for(i in 2:nrow(trial)){
x <- trial[i,] %>% # Take first row i of BirdsID j
ungroup() %>% # Ungroup the df
st_buffer(., 20000) %>% # Buffer that point by 20 km
st_intersection(., st_as_sf(water_poly)) %>% # Intersect the buffer by water polygon
st_sample(., 19, type = "random", exact = TRUE) %>% # Create 19 random points w/in buffer
st_cast(., "POINT") %>% # Make multipoint into point
as(., "Spatial") %>% # Then make it spatialpoints
as.data.frame(.) %>% # Then make it dataframe
mutate(used = 0, # Create column used and give randoms = 0
x = coords.x1, # Get rid of ugly column coords
y = coords.x2, # Calculate euclidean distance below
distance = sqrt((coords.x1 - trial$x[i-1])^2 + (coords.x2 - trial$y[i-1])^2)) %>%
dplyr::select(-coords.x1, -coords.x2) %>% # Officially get rid of ugly columns
bind_cols(trial[i,] %>% # Bind columns of row i to trial columns
dplyr::select(BirdsID, tod_, time, tdif, heading, sex, month, date, hunting, dist.km, hunting2, diel)) %>%
bind_rows(trial[i,] %>%
mutate(used = 1)) %>% # Bind "used" coords and give it = 1
mutate(strata = paste(i-1, BirdsID, sep = "_")) # New column called strata to keep track
temp[((i-2)*20+1):((i-1)*20),] <- x # Dump all of x into empty "temp" df
}
# end with the indexing temp2
temp2[((j-2)*20+1):((j-1)*20),] <- trial # Dump all of trial into "temp2"
}
正在寻找索引temp2以存储每个BirdsID的所有随机和使用位置的可能帮助。有401只动物,但每只动物都有不同数量的位置,但对于每个位置,都会生成相同数量的随机位置(即19个(。如有任何建议,不胜感激。
我已经解决了自己的问题。将所有结果编译成temp2
数据帧的问题解决如下:
# Create an empty data.frame to hold "random" and "used" points FOR EACH BIRD----
temp2 <- data.frame(matrix(NA, nrow = 20 * ((nrow(dat_sf_utm)) - (length(unique(dat_sf_utm$BirdsID)))),
ncol = 20))
colnames(temp2) <- c("used", "x", "y", "distance", "BirdsID", "tod_", "time",
"tdif", "heading", "sex", "month", "date", "hunting", "dist.km",
"hunting2", "diel", "geometry", "id", "speed", "strata")
for(j in unique(dat_sf_utm$BirdsID)){
trial <- dat_sf_utm %>%
group_by(BirdsID) %>% # Group by BirdsID
filter(BirdsID == j) %>% # filter BirdsID j
bind_cols(do.call(rbind, # Bind cols
(.$geometry)) %>% # Of "geometry"
as_tibble() %>% # And make them a tibble
setNames(c("x", "y")) # And call them xy
)
temp <- data.frame(matrix(NA, nrow = 20 * (nrow(trial)-1), # Make empty df to hold
ncol = 20)) # "random" and "used" locs
colnames(temp) <- c("used", "x", "y", "distance", "BirdsID", "tod_", "time",
"tdif", "heading", "sex", "month", "date", "hunting", "dist.km",
"hunting2", "diel", "geometry", "id", "speed", "strata")
for(i in 2:nrow(trial)){
x <- trial[i,] %>% # Take first row i of BirdsID j
ungroup() %>% # Ungroup the df
st_buffer(., 20000) %>% # Buffer that point by 20 km
st_intersection(., st_as_sf(water_poly)) %>% # Intersect the buffer by water polygon
st_sample(., 19, type = "random", exact = TRUE) %>% # Create 19 random points w/in buffer
st_cast(., "POINT") %>% # Make multipoint into point
as(., "Spatial") %>% # Then make it spatialpoints
as.data.frame(.) %>% # Then make it dataframe
mutate(used = 0, # Create column used and give randoms = 0
x = lon, # Get rid of ugly column coords
y = lat, # Calculate euclidean distance below
distance = sqrt((lon - trial$x[i-1])^2 + (lat - trial$y[i-1])^2)) %>%
dplyr::select(-lon, -lat) %>% # Officially get rid of ugly columns
bind_cols(trial[i,] %>% # Bind columns of row i to trial columns
dplyr::select(BirdsID, tod_, time, tdif, heading, sex, month, date, hunting, dist.km, hunting2, diel)) %>%
bind_rows(trial[i,] %>%
mutate(used = 1)) %>% # Bind "used" coords and give it = 1
mutate(strata = paste(i-1, BirdsID, sep = "_")) # New column called strata to keep track
temp[((i-2)*20+1):((i-1)*20),] <- x # Dump all of x into empty "temp" df
}
# end with the indexing temp2
temp2[first(which(is.na(temp2$used))):(first(which(is.na(temp2$used))) + nrow(temp)-1),] <- temp