Write your first R code

A short tutorial to write your first R code

Rstats
Author

Binod Jung Bogati

Published

October 15, 2018

Running code in R is easy (if copy/paste). Our motive here is not to teach you R. However, you’ll catch with basic R code in no-time.

Run the code below:

# printing 
print("Hello World")
[1] "Hello World"
#assignment 
 a <- 10 
 b <- 15

#add 
 a + b
[1] 25
#add + assign 
  c <- a + b

# print 1 to 100 
  1:10
 [1]  1  2  3  4  5  6  7  8  9 10
# sum from 1 to 100 
sum(1:100)
[1] 5050
# mean / average 
mean(c(10, 15, 20, 25))
[1] 17.5
# median / central value 
median(c(5, 10, 15, 25, 30))
[1] 15
# square root 
sqrt(64)
[1] 8
# function 
addition <- function(x, y){ x + y }

# calling function 
addition(10, 15)
[1] 25