R Cheatsheet: str function

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:


#Or in R Studio


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”


#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

[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:


#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.


Leer Más…

R Cheatsheet: Dates and Times

#R stores a date as the number of days counted from “Zero Day” which is January the first 1970. The same principle used by SAS, but the latter uses another “Start Date” or “Zero Day”

#Dates are represented as a “Date Class”

#On the other hand, times (number of seconds from “Start Date”) are represented by any of the following classes:

#POSIXct (a single integer value representing the time)

#POSIXlt (a list of  values representing the time)

#Defining a Date:


#If we need to see the “value” of a particular date class we have to unclass it:


#So, it means it have passed 7329 since January 25th, 1990, the date stored in bday. Unclass is equivalent to the datepart() function of SAS.

#To obtain the current datetime, equals to TODAY() in SAS



#Of course we can use the POSIXlt to de-construct and obtain a singular value of our date.


#strptime is a useful function you can use to convert time in a POSIXlt or POSIXct format.

R Cheatsheet: Coding Standards

  1. Use text files.
  2. Indent your code.
  3. Limit the width of your code( 80 columns is a good measure)