Archivo | Informática y relacionados RSS for this section

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)
}
Anuncios

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:

result-download-file

Now we check the existence of the new file using the File Explorer:

camerascsv

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)

matrix

R Cheatsheet: Installing packages

#By command:

install.packages("ggplot2")

#Or in R Studio

install_package

R Cheatsheet: Anonymous functions

#An anonymous function is a function which is defined in the call of another function:

#For example we have a previously defined function called “evaluate”

evaluate

#This function has two arguments: the name of another function and a variable to make calculations with.

#The first time we write an “inline” new function which returns the last element of this vector

evaluate(function(x){x[length(x)]},c(8,4,0))
[1] 0

#Now we write an “inline” new function which returns the first element of this vector

> evaluate(function(x){x[1]},c(8,4,0))
[1] 8

 

R Cheatsheet: Logical Operators

#Equal to

==

#Non equal to

!=

#Less than

<

#Less than or equal to

<=

#Greater than

<

#Greater than or equal to

<=

#Negation (NOT operator), or to negate a boolean expression:

!(b==c)

#AND (Conjunction)

#we can use both ‘&’ and ‘&&’ the difference between this two symbols is that the first evaluates ALL the elements of a vector, and the second only the first element of a vector.

and_operator

Leer Más…