R for Absolute Beginners

 

On Tuesday I gave a workshop at the Data Journalism UK conference, run by Paul Bradshaw.

This was the worked example for absolute beginners that we went through.

If you’ve never looked at R before and want to run some R code, load up this page , copy the following in step by step and hit ‘Run’:

#1. Print Hello world to the console
print("Hello, world!")

#2. Do some maths

2+2
4*5
(4+5) / 3

#3. Set some variables

var <- "My variable!"
var
my_name <- "Nicholas"

my_number <- 2
second_number <- 5
product <- my_number * second_number
product

#4. take a look at a data frame

mtcars
str(mtcars)
rownames(mtcars)

mtcars$cyl

mtcars[1,]
mtcars[1,5]

#5. perform some functions

#a) create a data frame of 100 random numbers

our_df <- data.frame(runif(n = 100))

#b) name the rows
colnames(our_df) <- "number"

#c) round the numbers

our_df$number <- round(our_df$number,2)

#d find the average of the numbers
mean(our_df$number)


#6. plot a graph of the numbers
#install the package if you haven't already
install.packages("ggplot2")
library(ggplot2)

p <- ggplot(data = our_df, aes(x = number, y = number)) + geom_point()
p

p2<- ggplot(data = our_df, aes(x = number, y = runif(n = 100))) + geom_point() + ggtitle("100 random numbers")
p2

p3 <- ggplot(data = our_df, aes(x = number, y = runif(n = 100), color = runif(n = 100))) + geom_point() + ggtitle("100 random numbers")
p3

There were presentations from various people in data journalism in Britain, which you can take a look at here.

Leave a Reply

Your email address will not be published. Required fields are marked *