…about R, for beginners

April 29, 2013
By

(This article was first published on Tales of R » R, and kindly contributed to R-bloggers)

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:

logobject

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

plot(object)

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"))

To leave a comment for the author, please follow the link and comment on his blog: Tales of R » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.