Chimborazo
Amigos de la blogosfera, tenía muchas ganas de escribir este post. La idea original nació hace algún tiempo, en los años universitarios, tras leer el fantástico libro Travels amongst the great Andes of the Equator del Alpinista británico Edward Whymper pero por innumerables eventos (principalmente el abandono de este blog) nunca se concretó y hoy con el consecuente giro temático del mismo y contando la inspiración necesaria, me pongo manos a la obra.
Por cuestiones de trabajo suelo volar hasta la ciudad de Quito en numerosas ocasiones del año. Es una corta travesía que suele tomar entre 45 y 60 minutos dependiendo de las condiciones climáticas. Cuando estas son benévolas, el trayecto es tranquilo y permite disfrutar una vista a los colosos que se alinean a lo largo de la Avenida de los Volcanes, pero esto no es así siempre, siendo los Andes unos gigantes celosos, se suelen envolver en un manto nuboso que los distancia de ojos mortales.
R Cheatsheet: Reading XLSX files
#Use the xlsx library
#By default it is not available in base R so we must install package
install.packages("xlsx")
library(xlsx)
#If we use the read.xlsx with only the filename parameter it will cause an error, because we must provide the sheetindex and point out wheter the sheet contains headers for each column
readexcelfile<-function(){ library(xlsx) localcopy<-"./data/cameras.xlsx" cameraXlsx<-read.xlsx(localcopy) head(cameraXlsx) }
#Correct use of read.xlsx
readexcelfile<-function(){ library(xlsx) localcopy<-"./data/cameras.xlsx" cameraXlsx<-read.xlsx(localcopy, sheetIndex = 1, header = TRUE) head(cameraXlsx) }
R Cheatsheet: Files and Directories
Check whether a directory exists, if true then nothing happen
if(!file.exists("test")){ dir.create("test")}
Next Step, write a function that downloads a csv file from the internet:
downloadfileurl<-function(){ #Check if a directory exists, otherwise we create if(!file.exists("data")){ dir.create("data")} #Next step, download a file from the internet. #First we create a variable with the url which contains the data: fileURl<-"https://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv?accessType=DOWNLOAD" #Next variable containts the location of local copy of downloaded file localcopy<-"./data/cameras.csv" #In order to obtain an online file we must use the download.file() function #Since we are working from a Windows terminal the third parameter (method) should work #with the default value. If you're working from a Mac, then you must specify its value to "curl" #because that file is available via https protocol download.file(fileURl,destfile = localcopy) #we check the files in that directory files<-list.files("./data") print(files) #Finally we print the date we downloaded that file. This is very important specially because you need #to be able to keep track of that file. datedownloaded<-date() print(datedownloaded) }
After executing this function we obtain as a result:
Now we check the existence of the new file using the File Explorer:
R Cheatsheet: str function
>summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
-5.48800 -0.02978 1.96200 2.20200 4.37300 11.39000
> str(x)
num [1:100] 9.02 7.57 5.15 -3.65 -3.89 …
R Cheatsheet: Create a Matrix
x<- matrix(c(1:28), nrow = 4, ncol = 7)
R Cheatsheet: Installing packages
#By command:
install.packages("ggplot2")
#Or in R Studio