Skip to content

Instantly share code, notes, and snippets.

@thatseeyou
Last active March 19, 2016 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thatseeyou/d9a639b4c794ede20aca to your computer and use it in GitHub Desktop.
Save thatseeyou/d9a639b4c794ede20aca to your computer and use it in GitHub Desktop.
Exercises from r-exercises.com
# Exercises at http://r-exercises.com/2016/02/14/mode-exercises/
# 1.
mode(c('a', 'b', 'c'))
typeof(c('a', 'b', 'c'))
mode(3.32e16)
typeof(3.32e16)
mode(1/3)
typeof(1/3)
mode(sqrt(-2i))
typeof(sqrt(-2i))
# 2.
pressure
lm
rivers
mode(pressure)
typeof(pressure)
class(pressure)
mode(lm)
typeof(lm)
class(lm)
mode(rivers)
typeof(rivers)
class(rivers)
# 3.
x <- list(LETTERS, TRUE, print(1:10), print, 1:10)
mode(LETTERS)
mode(TRUE)
mode(print(1:10))
mode(print)
mode(1:10)
mode(x)
# 4.
x <- 1:100
if (mode(x) == "numeric") { TRUE } else { FALSE }
# 5.
x <- 1:100
head(x, 5)
x_str1 <- as.character(x)
head(x_str, 5)
mode(x) <- 'character'
head(x, 5)
# 6.
mode(x) <- 'numeric'
head(x, 5)
# 7.
x <- c('1', '2', 'three')
mode(x) <- 'numeric'
x
# 8.
x <- c(TRUE, TRUE, FALSE, TRUE)
mode(x) <- 'numeric'
x
# 9.
x <- c('1', '2', 'three')
y <- x + 1
mode(y)
# 10.
x <- c('1', '2', '3')
mode(x) <- 'numeric'
y <- x * 2
mode(y) <- 'character'
y
# Exercises at http://r-exercises.com/2015/10/09/vector-exercises/
# 1.
x <- c(4,6,5,7,10,9,4,15)
x < 7
# 2.
p <- c (3, 5, 6, 8)
q <- c (3, 3, 3)
p + q
# 3.
Age <- c(22, 25, 18, 20)
Name <- c("James", "Mathew", "Olivia", "Stella")
Gender <- c("M", "M", "F", "F")
# output wanted
## Age Name Gender
## 1 22 James M
## 2 25 Mathew M
subset(data.frame(Age, Name, Gender), Gender == "M")
# 4.
z <- 0:9
mode(z)
typeof(z)
(digits <- as.character(z))
as.integer(digits)
# 5.
x <- c(1,2,3,4)
((x+2)[(!is.na(x)) & x > 0] -> k)
# 6.
# AirPassengers is ts (time-series)
AirPassengers[time(AirPassengers) >= 1949 & time(AirPassengers) < 1950]
# 7.
a <- c(2, 4, 6, 8)
b <- c(TRUE, TRUE, FALSE, TRUE)
sum(a[b])
# 8.
x <- c(34, 56, 55, 87, NA, 4, 77, NA, 21, NA, 39)
length(is.na(x))
sum(is.na(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment