You are on page 1of 3

http://rstatistics.

net/r-tutorial-exercise-for-beginners/

Calculate square root of 729


sqrt (729)

Create a new variable 'b' with value 1947.0


b <- 1947.0

Convert 'b' from previous exercise to character


b <- as.character(b)
print (b)

Setup your working directory to a new 'work' folder in your desktop


setwd ("path/to/my/desktop/work")
getwd()

Create a numeric vector

one_to_six <- c(1, 2, 3, 4, 5, 6)


class(one_to_six)

Create a vector containing following mixed elements {1, 'a', 2, 'b'} and find out its
class
one_a <- c(1, "a", 2, "b")
class(one_a) # character

Initialise Character vector

charHundred <- character(26)


charHundred

Assign the character 'a' to the first element in above vector.


charHundred[1] <- "a"

Vector Operations

Create a vector with some of your friend's names


myFriends <- c("alan", "bala", "amir", "tsong", "chan")

Get the length of above vector


length(myFriends)

Get the first two friends from above vector


myFriends[1:2]
Get the 2nd and 3rd friends
myFriends[c(2,3)]

Sort your friends by names using 2 methods


sort(myFriends)
myFriends[order(myFriends)]

Reverse direction of the above sort


sort(myFriends, decreasing=TRUE)
myFriends[rev(order(myFriends))]

Create with rep and seq: 'a','a',1,2,3,4,5,7,9,11


out <- c(rep('a', 2), seq(1, 5), seq(7, 11, by=2))

Remove missing value from c(1, 2, NA, 4)


myVec <- c(1, 2, NA, 4)
myVec[!is.na(myVec)]

Pick 50 random numbers between 1 to 100


mySample <-sample(1:100, 50)

Check the class of mySample


class (mySample)

Apply the below functions and inspect results on 'iris' (a base R dataframe)
class (iris) # get class
sapply (iris, class) # get class of all columns
str (iris) # structure
summary (iris) # summary of airquality
head (iris) # view the first 6 obs
fix (iris) # view spreadsheet like grid
rownames (iris) # row names
colnames (iris) # columns names
nrow (iris) # number of rows
ncol (iris) # number of columns

Get the last 2 rows in last 2 columns from iris dataset


numRows <- nrow(iris)
numCols <- ncol(iris)
iris[(numRows-1):numRows, (numCols-1):numCols]

Get rows with Sepal.Width > 3.5 using which() from iris
iris[iris$Sepal.Width > 3, ]
iris[which(iris$Sepal.Width > 3), ]

Get the rows with 'versicolor' species using subset() from iris
subset(iris, Species == "versicolor")

How would you specify the 'by' variables if the two data frames to be merged
have different 'by' variables.
merge(Df1, Df2, by ="var1") # both DFs have a common variable name'var1'
merge(Df1, Df2, by.x="var1", by.y="var2") # both DFs have different names for by variables .

Problem Logic for Q18, 19 and 20: Create a character vector with
length of number-of-rows-of-iris-dataset, such that, each element gets a
character value greater than 5 if the corresponding Sepal.Length > 5,
else it should get lesser than 5.
Q18. Create a logic for the above problem using a For-loop

Exercise 18: Make the logic for above


problem statement using a 'for-loop' and Hide

a 'if-else' statement

output <- character(nrow(iris))

for(i in c(1:nrow(iris))){

if (iris$Sepal.Length[i] > 5){

output[i] <- "greater than 5"

} else {

output[i] <- "lesser than 5"

Make the logic for above problem statement using a ifelse() function
output <- ifelse(iris$Sepal.Length > 5, "greater than 5", "lesser than 5")

You might also like