博文

目前显示的是 十一月, 2021的博文

几何体修正

 library(sf) library(tidyverse) library(magrittr) `%nin%` = Negate(`%in%`) xz <- st_read("D:\\乡镇边界\\乡镇边界.shp") %>%    select(Name) %>%    st_make_valid() %>%  #几何体修正几何图形   st_write("D:\\乡镇边界\\乡镇边界.geojson") xz %>%    st_make_valid()  xz %>%    st_is_valid() %>%    as_tibble() -> tst xz %>%    filter(na.omit(st_is_valid(xz)) == TRUE) -> xz #过滤冒犯的几何图形(通过基于结果的空间对象的子集) # empty geometries, using any(is.na(st_dimension(x))) # corrupt geometries, using any(is.na(st_is_valid(x))) # invalid geometries, using any(na.omit(st_is_valid(x)) == FALSE); in case of corrupt and/or invalid geometries, # in case of invalid geometries, query the reason for invalidity by st_is_valid(x, reason = TRUE) # you may be succesful in making geometries valid using st_make_valid(x) or, if st_make_valid is not supported by # st_buffer(x, 0.0) on non-corrupt geometries (but beware of the bowtie example above, where st_buffer removes one half). # After succesful a st_mak

overlap及两层地图

 library(sf) library(tidyverse) library(magrittr) `%nin%` = Negate(`%in%`) ts <- st_read("C:\\Users\\ruiying\\Desktop\\甘肃乡镇\\gansu.TAB") %>%    filter(str_detect(地区编码,"^620502") | str_detect(地区编码,"^620503")) %>%   rename(code=地区编码) %>%    filter(row_number() %nin% c(44,45)) # ts %>%  #   st_transform(4326) # ts <- st_read("C:\\Users\\ruiying\\Desktop\\ts.geojson") # bl <- read.csv("C:\\Users\\ruiying\\Desktop\\报告卡2021-11-03(8时).csv") # bl %<>% #   mutate(xiang=str_sub(bl$现住详细地址,10,12)) %>% #   group_by(xiang) %>% #   summarise(n=n()) xiang <- read.csv("C:\\Users\\ruiying\\Desktop\\xiang.csv") %>%   select(-X) ts %>%    left_join(xiang,by=c("NAME"="NAME")) %>%    ggplot() +   geom_sf(aes(fill=n))+   scale_fill_gradientn(breaks = c(2,4,6),                        colors=c("gray", "yellow", "orange", "red"))+   coord_sf(xlim =

purrr

 library(tidyverse) dat = data.frame(y1 = rnorm(10),y2 = rnorm(10)+10) re = list() #1 for(i in 1:2){ #2   re[[i]] = mean(dat[,i]) #3 }  map(dat,mean) map_dbl(dat,mean) map_df(dat,mean) map_df(dat,~mean(.)) iris %>%    split(.$Species) %>%    map(.,~aov(Sepal.Length ~ Sepal.Width,data=.) %>% summary) # aov(Sepal.Length ~ Sepal.Width,data = iris) mtcars %>%    split(.$cyl) %>%    map(.,~lm(mpg~wt+hp+drat,data = .)) %>%    map(coef) %>%    map_df('wt') mtcars %>%    split(.$cyl) %>%    map(.,~lm(mpg~wt+hp+drat,data = .)) %>%    map(coef) %>%    as.data.frame() x <- list(   list(-1, x = 1, y = c(2), z = "a"),   list(-2, x = 4, y = c(5, 6), z = "b"),   list(-3, x = 8, y = c(9, 10, 11)) ) # select by name map_dbl(x,'x') # select by position map_dbl(x,1) # select by name and position map_dbl(x,list('y',1)) wt <- c(5,  5,  4,  1)/15 x <- c(3.7,3.3,3.5,2.8) weighted.mean(x, wt)  # 3.7*0.3333+3.3*0.3333+3.5*0.2