Version Elegante

Lectura del dataset y su estructura

# leo el archivo ar_properties 
library(readr) # libreria con la funcion read_csv
datos1a <- read_csv("ar_properties.csv") # Acá completen con su propio PATH al archivo
Rows: 388891 Columns: 24
── Column specification ──────────────────────────────────────
Delimiter: ","
chr  (12): id, ad_type, l1, l2, l3, l4, l5, currency, pric...
dbl   (8): lat, lon, rooms, bedrooms, bathrooms, surface_t...
lgl   (1): l6
date  (3): start_date, end_date, created_on

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
datos1a # veo la base

Aplicando filtros

Se seleccionan aquellos registros que pertenecen a Argentina y Capital Federal, cuyo precio esta en dolares (USD), el tipo de propiedad corresponde a Departamento y el tipo de operacion sea Venta.

library(tidyverse) # carga varios paquetes: dplyr, ggplot2, purr, rtingr, etc.
── Attaching packages ───────────────────── tidyverse 1.3.0 ──
✓ ggplot2 3.3.5     ✓ dplyr   1.0.7
✓ tibble  3.1.4     ✓ stringr 1.4.0
✓ tidyr   1.1.3     ✓ forcats 0.5.1
✓ purrr   0.3.4     
── Conflicts ──────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
datos1b <- datos1a %>% 
                   # Me quedo con los que pertenecen a Argentina y Capital Federal
            filter(l1 == "Argentina", 
                   l2 == "Capital Federal", 
                   # cuyo precio este en dolares 
                   currency == "USD", 
                   # propiedad tipo Departamento
                   property_type %in% c("Departamento"),
                   # operaciones de venta
                   operation_type == "Venta") %>% select(l3,surface_covered,price,lat,lon) %>% na.omit() %>% mutate(precio=price,barrio=l3,sup=surface_covered,pm2=precio/sup )
# chequeo si el filtro se refleja correctamente en mi nuevo dataset datos1b
datos1b 
attach(datos1b) # pongo las variables en memoria

¿ Cual es el punto geográfico con departamentos con mayor precio por metro cuadrado en CABA ?

Dada una cantidad de vecinos \(N\), para cada depto (lon,lat) tomo los \(N\) deptos más cercanos y calculo el precio mediano por \(m^2\).

library(nabor) # Libreria para calcular vecinos mas cercanos 
radio.vecinos<-100 # cant de vecinos
N<-nrow(datos1b) # cant de deptos
coordenadas<-cbind(lon,lat) # matriz de coordenadas
vecinos<-knn(coordenadas,coordenadas,k=radio.vecinos)$nn.idx # busca vecinos 
dimen<-dim(vecinos) # N X radio.vecinos
pm2.vec<-matrix(pm2[vecinos],dimen[1],dimen[2]) # re-agrupo los pm2 de los KNN
pm2.med<-apply(pm2.vec,1,median) # pm2 mediano e los knn de cada depto
# Las 3 zonas de interes
mas.caro<-which.max(pm2.med) # indice del depto de la zona mas cara
mas.barato<-which.min(pm2.med) # indice del depto de la zona mas barata
promedio<-order(pm2.med)[(N+1)/2] # indice del depto de la zona prototipica
head(coordenadas)
           lon       lat
[1,] -58.46259 -34.54307
[2,] -58.42435 -34.60152
[3,] -58.42435 -34.60152
[4,] -58.42435 -34.60152
[5,] -58.42435 -34.60152
[6,] -58.42435 -34.60152
pm2.vec[1:10,1:5]
          [,1]     [,2]     [,3]     [,4]     [,5]
 [1,] 3524.590 4607.843 3684.211 3868.421 4545.455
 [2,] 2675.676 2613.636 2511.973 2270.270 2511.973
 [3,] 2675.676 2613.636 2511.973 2270.270 2511.973
 [4,] 2675.676 2613.636 2511.973 2270.270 2511.973
 [5,] 2675.676 2613.636 2511.973 2270.270 2511.973
 [6,] 2675.676 2613.636 2511.973 2270.270 2511.973
 [7,] 2675.676 2613.636 2511.973 2270.270 2511.973
 [8,] 2675.676 2613.636 2511.973 2270.270 2511.973
 [9,] 3000.000 3000.000 3000.000 3000.000 3000.000
[10,] 2675.676 2613.636 2511.973 2270.270 2511.973
dim(pm2.vec)
[1] 47517   100
length(pm2.med)
[1] 47517

Mapa de GeoRef de Deptos

Rapido sin labels

library("leaflet") # Libreria en JavaScript interctiva para hacer mapas
Registered S3 method overwritten by 'htmlwidgets':
  method           from         
  print.htmlwidget tools:rstudio
## Leaflet map with raster

leaflet() %>% addTiles() %>%
addCircleMarkers(lng=lon[vecinos[mas.caro,]], lat=lat[vecinos[mas.caro,]],fillOpacity=0.5,
              ,radius=3/2,color="green",stroke = FALSE) %>% addCircleMarkers(lng=lon[vecinos[promedio,]], lat=lat[vecinos[promedio,]],fillOpacity=0.5,
              ,radius=3/2,color="blue",stroke = FALSE) %>% addCircleMarkers(lng=lon[vecinos[mas.barato,]], lat=lat[vecinos[mas.barato,]],fillOpacity=0.5,
              ,radius=3/2,color="red",stroke = FALSE)
NA

Todos los deptos

leaflet() %>% addTiles() %>%
addCircleMarkers(lng=lon, lat=lat,fillOpacity=0.5,
              ,radius=1/2,color="black",stroke = FALSE)

Agrego Raster library

library(raster)
Loading required package: sp

Attaching package: ‘raster’

The following object is masked from ‘package:dplyr’:

    select

The following object is masked from ‘package:tidyr’:

    extract

Calculo de raster de Precio Promedio

library(rgdal)
# raster vacio
ras<-raster(nrows=100, ncols=100, xmn=min(lon), xmx=max(lon),ymn=min(lat),ymx=max(lat))
# raster con precios promedios
colores <- c('red', 'blue', 'green') # Paleta de colores
at <- seq(min(pm2.med), max(pm2.med), length.out = 20) # puntos de corte de categorias
cb <- colorBin(palette = colores, bins = at, domain = at,na.color="#00000000") # colores
ras.pm2<-rasterize(coordenadas,ras,field=pm2.med,fun=mean) # raster de valores promedios de pm2.med
leaflet() %>% addTiles() %>% addRasterImage(ras.pm2,opacity = 0.75,colors = cb) %>% addLegend(pal = cb, values = at) # graf leaflet + raster + leyenda
Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crsDiscarded datum World Geodetic System 1984 in Proj4 definitionDiscarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crsDiscarded datum World Geodetic System 1984 in Proj4 definition
LS0tCnRpdGxlOiAiQW5hbGlzaXMgZXhwbG9yYXRvcmlv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