Professional Documents
Culture Documents
net/r-tutorial-exercise-for-beginners/
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
Vector Operations
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 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
a 'if-else' statement
for(i in c(1:nrow(iris))){
} else {
Make the logic for above problem statement using a ifelse() function
output <- ifelse(iris$Sepal.Length > 5, "greater than 5", "lesser than 5")