Created
March 24, 2017 09:30
-
-
Save selva86/1fc85e63136fa9ffc76b6412d256e48c to your computer and use it in GitHub Desktop.
Solutions for Final Test of Learn R By Intensive Practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Solutions for Final Test of Learn R By Intensive Practice | |
Q1. | |
```{r} | |
#1 | |
sqrt (729) | |
#2 | |
1203 %% 22 | |
#3 | |
d1 <- as.Date('2013-10-04') | |
d2 <- as.Date('2015-11-03') | |
d2 - d1 | |
``` | |
Q2. | |
```{r} | |
b <- 1947.0 | |
class(b) | |
object.size(b) | |
``` | |
Q3. | |
```{r} | |
a <- as.character(b) | |
object.size(a) | |
``` | |
Q4. | |
```{r} | |
getwd() | |
setwd ("path/to/my/desktop/work") | |
getwd() | |
``` | |
Q5. | |
```{r} | |
# 5a. | |
myEnv <- new.env() | |
# 5b. | |
assign('b', 3, envir = myEnv) | |
# 5c. | |
get('b', envir=myEnv) | |
# 5d. | |
rm(b, envir=myEnv) | |
``` | |
Note: The ‘envir’ argument was common in ‘assign’, ‘get’ and ‘rm’ functions. Also note that the assign and get functions take in the variable name as a string while the rm function took the variable object itself. | |
Q6. | |
```{r} | |
# 6a. | |
one_to_six <- c(1, 2, 3, 4, 5, 6) | |
class(one_to_six) | |
# 6b. | |
one_a <- c(1, "a", 2, "b") | |
class(one_a) # character | |
``` | |
Q7. | |
```{r} | |
# 7a. | |
charHundred <- character(26) | |
charHundred | |
# 7b. | |
charHundred[1] <- "a" | |
``` | |
Q8. | |
```{r} | |
# 8a. | |
myFriends <- c("alan", "bala", "amir", "tsong", "chan") | |
# 8b. | |
length(myFriends) | |
# 8c. | |
myFriends[1:2] | |
# 8d. | |
myFriends[c(2,3)] | |
# 8e. | |
# method 1 | |
sort(myFriends) | |
# method 2 | |
myFriends[order(myFriends)] | |
# 8f. | |
# method 1 | |
sort(myFriends, decreasing=TRUE) | |
# method 2 | |
myFriends[rev(order(myFriends))] | |
``` | |
Q9. | |
```{r} | |
out <- c(rep('a', 2), seq(1, 5), seq(7, 11, by=2)) # because of the presence of 'a' character, the numbers are converted to characters as well. | |
``` | |
Q10. | |
```{r} | |
myVec <- c(1, 2, NA, 4, 5, 6, 7, NA, 10) | |
myVec[!is.na(myVec)] | |
na.omit(myVec) | |
``` | |
Q11. | |
```{r} | |
a <- 1:180 | |
vec1 <-sample(a, 50, replace=T) | |
vec2 <-sample(a, length(a)*.25, replace=F) | |
``` | |
Q12. | |
```{r} | |
vec1[duplicated(vec1)] | |
intersect(vec1, vec2) | |
``` | |
Q13. | |
```{r} | |
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 | |
``` | |
Q14. | |
```{r} | |
# 14a. | |
numRows <- nrow(iris) | |
numCols <- ncol(iris) | |
iris[(numRows-1):numRows, (numCols-1):numCols] | |
# 14b. | |
iris[iris$Sepal.Width > 3, ] | |
iris[which(iris$Sepal.Width > 3), ] | |
# 14c. | |
subset(iris, Species == "versicolor") | |
``` | |
Q15. | |
```{r} | |
set.seed(100) | |
Df1 <- iris[sample(1:nrow(iris), 10), c(1,2,3,5)] | |
Df2 <- iris[sample(1:nrow(iris), 10), c(1,2,4,5) ] | |
# 15.1 | |
merge(Df1, Df2, by="Species", all=FALSE) # inner join | |
merge(Df1, Df2, by="Species", all=TRUE) # outer join | |
merge(Df1, Df2, by="Species", all.x=TRUE) # left join | |
merge(Df1, Df2, by="Species", all.y=TRUE) # right join | |
``` | |
Q16. | |
```{r} | |
paste0 (c(rep("var", 3), rep("pred", 3)), 1:3) | |
``` | |
Q17. | |
```{r} | |
table(iris$Species, iris$Sepal.Width) | |
aggregate(Petal.Width ~ Species, data=iris, FUN = mean) | |
``` | |
Q18. | |
```{r} | |
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" | |
} | |
} | |
``` | |
Q19. | |
```{r} | |
output <- ifelse(iris$Sepal.Length > 5, "greater than 5", "lesser than 5") # works like the 'if' function in MS Excel, except that the condition is checked for every element of iris$Sepal.Length | |
``` | |
Q20. | |
```{r} | |
# Define the function for apply() statement | |
myFunc <- function(x){ | |
if(x['Sepal.Length'] > 5){ | |
"greater than 5" | |
} else { | |
"lesser than 5" | |
} | |
} | |
output <- apply(iris, 1, FUN=myFunc) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment