…start using R, from scratch!

Some time ago, since I was able to use R by myself, have found some fellows and other people who wanted to learn R as well. Then I pointed them to help pages, to CRAN repositories… but in some cases they said that didn’t know how to start using those resources. Obviously, the main self-perceived limitation for non-programmers is the use of “commands” -ok, many of the 80’s kids will remember the use of some command lines to access games such as PacMan, Frogger… :).

At the same time, they also wanted to refresh some basic statistics, acquiring a general knowledge of their data before asking for a statistician’s help. An idea to quickly help them was to make some scripts to guide them through basic commands, seeing results on real-time, and being able to recycle them for their own data.

If you have just started using R, maybe they can be useful for you. However, I will recommend that you use some open “plain text” file(s) to paste your favorite commands and clone/modify them to suit your needs. Remember to store the files where you can access them later!

  • Tip: you can change the extension of your mytext.txt file into mytext.R file, telling Windows to open it with the Notepad again. It will be also a plain text document, but some text editors will recognize it as an “R script” and will highlight the content according to that.
  • Apart from the Notepad in Windows, you also have a bunch of other text/code editors which are more pleasant to use. See for example R-studio and Notepad ++.

Copy the Gists below into your own text files, and begin playing with R!


…about R, for beginners

These days I was remembering my beginnings as a linux user few years ago, and how I found R (possibly in a very unlikely way: searching for a SPSS alternative in Linux). For two years, R had been almost impossible for me. I didn’t understand its syntax (I don’t have programming background), and luckily I had installed R Commander (Rcmdr) to perform simple analysis and plots…

Another feature of Rcmdr (that I didn’t use) was that every click to a button remains in a part of Rcmdr’s console, and then it can be copied, used and modified. Indeed, the ‘copypaste‘ thing has revealed to me as the best way to acquire, recyle, modify and run commands.

Have you installed R? You can try this (nice) code:

# First we store an object called ‘object’ in R’s memory (just like a big calculator)

object <- seq(1,100,  by=5)

# ‘Object’ stores the sequence (seq) from 1 to 100, by 5. That is: 1,  6, 11, 16, 21 26, 31… If you type ?seq in the console, you will have a brief help.

# With R, you can calculate big numbers, logarythms and fractions storing its components, with less chance of errors than with an ordinary calculator.

# You can create new variables from old ones:

logobject <- log10(object)

# ‘Logobject’ stores the decimal logarythm of every value inside ‘object’. That is:


# You can also plot the variables with simple functions like plot:


plot(object, logobject)

plot(object, logobject, col = "red", xlab="My X axis", ylab= "This is my Y axis", main="My first plot")

# As you can see, is not very difficult to change the color “argument”, from “red” to “green” or “blue” for instance. The title, instance of “My first R plot” can be changed to “My coloured R plot”, or the labels of the axes. Don’t forget to store these commands and new ones, in a plain Text document, Wordpad etc.. so you can recycle them and fix syntax errors!

# If you feel confident, you can try this. The spoiler is here, by DWin

dat <- data.frame(t=seq(0, 2*pi, by=0.1) )

xhrt <- function(t) 16*sin(t)^3

yhrt <- function(t) 13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t)

dat$y = yhrt(dat$t)

dat$x = xhrt(dat$t)

with(dat, plot(x,y, type="l"))

with(dat, polygon(x,y, col="hotpink"))