`# Marco Plebani 18 May 2018`

`##########################################`

##### OUR FIRST R SCRIPT #################

##########################################

`# you run code by selecting lines and pressing control+R (windows) or cmd+enter (mac)`

# lines starting with an ash (like this one) don't "run".

# Useful for writing comments or for deactivating bits of code without deleting them.

`# R is first of all a powerful calculator!`

# try:

`3*2`

10/15

sqrt(2) # square root of two

# sqrt() is the first R function you see! Functions apply to what's between the brackets

sqrt() # no argument between the brackets? R gets angry. Don't anger R.

pi # useful

2^3 # two to the third power

exp(1) # this equals to e^1

log(1) # natural log

log(exp(1))

log10(10) # decimal log

# you can save results. Just give it a name as follows:

myfirstRobject <- 10/15

myfirstRobject

mysecondone <- 11*11

myfirstRobject + mysecondone

`# want to apply a calculation to more than one number at a time?`

# save some numbers in an R object called "vector":

somenumbers <- c(1,2,3,5,7,11)

somenumbers*2

`# a bunch of vectors together is called a matrix.`

# unless you give each vector a name. In that case they form an object called a "data frame" (namely a dataset). Hese's how you create one:

`myfirstdf <- data.frame(mynumbers = somenumbers)`

# now create a new column for the data.frame:

myfirstdf$timestwo <- myfirstdf$mynumbers * 2

# Do you want to export a data.frame? Easy peasy!

write.csv(myfirstdf, "~/Desktop/output.csv")

# I am working on a Mac. You may have to type directories in a slightly different way in Windows.