This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
library(sf)
Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed. l
(mun_cas<-read_sf("CAS_MUN.shp"))
Simple feature collection with 19 features and 6 fields
geometry type: POLYGON
dimension: XY
bbox: xmin: -73.0989 ymin: 4.246699 xmax: -69.84787 ymax: 6.250501
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
####ahora identificamos clases del objeto mun_cas
(class(mun_cas))
[1] "sf" "tbl_df" "tbl" "data.frame"
####identificar atributos del objeto mun_cas
(attributes(mun_cas))
$names
[1] "ISO" "NAME_0" "NAME_1" "NAME_2" "TYPE_2" "ENGTYPE_2" "geometry"
$row.names
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
$class
[1] "sf" "tbl_df" "tbl" "data.frame"
$sf_column
[1] "geometry"
$agr
ISO NAME_0 NAME_1 NAME_2 TYPE_2 ENGTYPE_2
<NA> <NA> <NA> <NA> <NA> <NA>
Levels: constant aggregate identity
###CRS (mun_cas)
(st_crs(mun_cas))
Coordinate Reference System:
EPSG: 4326
proj4string: "+proj=longlat +datum=WGS84 +no_defs"
###BOUNDING BOX
(st_bbox(mun_cas))
xmin ymin xmax ymax
-73.098900 4.246699 -69.847870 6.250501
####instalamos 2 librerias adicionales
install.packages("lwgeom")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
Installing package into 㤼㸱C:/Users/usuagro/Documents/R/win-library/3.6㤼㸲
(as 㤼㸱lib㤼㸲 is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/lwgeom_0.1-7.zip'
Content type 'application/zip' length 5200790 bytes (5.0 MB)
downloaded 5.0 MB
package ‘lwgeom’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\usuagro\AppData\Local\Temp\Rtmp2tzl11\downloaded_packages
install.packages("units")
Error in install.packages : Updating loaded packages
library(lwgeom)
Linking to liblwgeom 2.5.0dev r16016, GEOS 3.6.1, PROJ 4.9.3
library(units)
udunits system database from C:/Users/usuagro/Documents/R/win-library/3.6/units/share/udunits
####Calculo de area
(mun_cas$area<-st_area(mun_cas))
Units: [m^2]
[1] 1433367564 328669391 5063032208 152392382 3862105519 720968889 1046837781 4742723897 12435921004 784139464
[11] 179362656 495204298 278663795 3238290659 1105473672 2441874179 3037489236 755109946 2520306897
####cambio unidades de area
set_units(mun_cas$area,km^2)
Units: [km^2]
[1] 1433.3676 328.6694 5063.0322 152.3924 3862.1055 720.9689 1046.8378 4742.7239 12435.9210 784.1395 179.3627
[12] 495.2043 278.6638 3238.2907 1105.4737 2441.8742 3037.4892 755.1099 2520.3069
####llamamos una libreria para leer archivos xl
library("readxl")
(C_DATA<-read_excel("censo_agro.xls"))
####adjuntar tabla a la variable
library(tidyverse)
Registered S3 method overwritten by 'dplyr':
method from
print.rowwise_df
[30m-- [1mAttaching packages[22m --------------------------------------- tidyverse 1.2.1 --[39m
[30m[32mv[30m [34mggplot2[30m 3.2.1 [32mv[30m [34mpurrr [30m 0.3.2
[32mv[30m [34mtibble [30m 2.1.3 [32mv[30m [34mdplyr [30m 0.8.3
[32mv[30m [34mtidyr [30m 1.0.0 [32mv[30m [34mstringr[30m 1.4.0
[32mv[30m [34mreadr [30m 1.3.1 [32mv[30m [34mforcats[30m 0.4.0[39m
[30m-- [1mConflicts[22m ------------------------------------------ tidyverse_conflicts() --
[31mx[30m [34mdplyr[30m::[32mfilter()[30m masks [34mstats[30m::filter()
[31mx[30m [34mdplyr[30m::[32mlag()[30m masks [34mstats[30m::lag()[39m
install.packages("mapview")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
Installing package into 㤼㸱C:/Users/usuagro/Documents/R/win-library/3.6㤼㸲
(as 㤼㸱lib㤼㸲 is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.6/mapview_2.7.0.zip'
Content type 'application/zip' length 2947906 bytes (2.8 MB)
downloaded 2.8 MB
package ‘mapview’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\usuagro\AppData\Local\Temp\Rtmp2tzl11\downloaded_packages
(CAS_DATA<-C_DATA %>% filter(DEPTO=="Casanare"))
(CAS_DATA=rename(CAS_DATA,NAME_2=MUNI))
(CAS_CENSO<- left_join(mun_cas,CAS_DATA))
Joining, by = "NAME_2"
Simple feature collection with 19 features and 14 fields
geometry type: POLYGON
dimension: XY
bbox: xmin: -73.0989 ymin: 4.246699 xmax: -69.84787 ymax: 6.250501
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
library(mapview)
Registered S3 methods overwritten by 'htmltools':
method from
print.html tools:rstudio
print.shiny.tag tools:rstudio
print.shiny.tag.list tools:rstudio
Registered S3 method overwritten by 'htmlwidgets':
method from
print.htmlwidget tools:rstudio
suppressPackageStartupMessages(library(mapview))
(CAS_CENSO%>%mapview(zcol="NAME_2",legend=TRUE,col.regions=sf.colors))
####limpiar basandose en las hectareas de bosque
(CAS_CLEAN<- CAS_CENSO %>% arrange(desc(HA_BOSQUE))%>%select(NAME_2,COD_MUN,HA_BOSQUE,HA_AGRO,HA_NOAGRO,HA_UTIERRA))
Simple feature collection with 19 features and 6 fields
geometry type: POLYGON
dimension: XY
bbox: xmin: -73.0989 ymin: 4.246699 xmax: -69.84787 ymax: 6.250501
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
(CAS_CLEAN%>%mapview(zcol="NAME_2",legend=TRUE,col.regions=sf.colors))