combn(rownames(my.square.matrix), m=2, FUN=paste, collapse = "-", simplify = T)

Advertisements

combn(rownames(my.square.matrix), m=2, FUN=paste, collapse = "-", simplify = T)

Advertisements

If your working directory is C:Parent/Child

Then to read a file from Parent use ..

file="../MY_FILE.RData"

https://stackoverflow.com/questions/24319168/how-to-move-backward-parent-folder

R intersect polygons and points

```
library(sf)
library(tidyverse)
# example data from raster package
soil <- st_read(system.file("external/lux.shp", package="raster")) %>%
# add in some fake soil type data
mutate(soil = LETTERS[c(1:6,1:6)]) %>%
select(soil)
# field polygons
field <- c("POLYGON((6 49.75,6 50,6.4 50,6.4 49.75,6 49.75))",
"POLYGON((5.8 49.5,5.8 49.7,6.2 49.7,6.2 49.5,5.8 49.5))") %>%
st_as_sfc(crs = st_crs(soil)) %>%
st_sf(field = c('x','y'), geoms = ., stringsAsFactors = FALSE)
# intersect - note that sf is intelligent with attribute data!
pi <- st_intersection(soil, field)
plot(soil$geometry, axes = TRUE)
plot(field$geoms, add = TRUE)
plot(pi$geoms, add = TRUE, col='red')
# add in areas in m2
attArea <- pi %>%
mutate(area = st_area(.) %>% as.numeric())
# for each field, get area per soil type
attArea %>%
as_tibble() %>%
group_by(field, soil) %>%
summarize(area = sum(area))
```

na.collapse<-function(x) { x.<-unique(x[!is.na(x)]) if(length(x.)==0) { return(NA) } else { if(length(x.)==1){ return(x.) } else { return(paste(x.,collapse="|")) } } } na.collapse(x)

wer<-SpatialPointsDataFrame(data.frame(x.subset[,"longitude"],x.subset[,"latitude"]),data=data.frame(x.subset)) n <- length(wer) nearest <- character(n) for (i in seq_along(nearest)) { nearest[i] <- names(polys)[which.min(gDistance(wer[i,], polys, byid=TRUE))] }

This may give you warnings if your code is not projected into a planar coordinate system

http://stackoverflow.com/questions/26308426/how-do-i-find-the-polygon-nearest-to-a-point-in-r

rasterToPoints(stacked) #this also gives the cell values of the raster layer or layers