print("Hello World")
x <- seq(1,10,by=0.1)
y <- x*x
plot(x, y, type="l")
print(class(x))
print(3 < 4)
print(3 < 3)
print(3 <= 3)
print(9 / 4)
print(9 %% 4)
print(10 %% 5)
print(2 ^ 3)
print(2 ** 3)
x <- 2**3
class(x)
x <- "3.14"
print(as.numeric(x) + 1)
x <- 1 / 0
print(x)
sentence <- "Jeff's brother's name is Kevin. Jeff says \"yes\""
print(x/x)
class(x/x)
x <- 10:15
print(x[1])
x <- matrix(c(1,2,3,4), nrow=2, ncol=2, byrow=TRUE)
View(x)
print(x[1, 2])
x <- array(c(1,2,3,4), dim=c(10,10))
View(x)
print(x[1,])
print(x[1:5,10])
y <- x[,1] >= 3
y
x[y,]
c(1,2,3,4) + c(2,3)
x <- data.frame(name=c("Jeff", "Devon", "Sarah"),
age=c(42, 42, 3.14),
gender=as.factor(c("male", "female", "female")))
View(x)
x[2,1]
x[,2]
x$age
hello <- function() {
print("Hello World!")
print("And good bye:(")
42 # if no return statement, then the last expression is the return value
}
x <- hello()
stuff <- function(x1, x2, x3) {
print(x1)
print(x2)
print(x3)
print(min(c(x1,x2,x3)))
print(max(c(x1,x2,x3)))
print(mean(c(x1,x2,x3)))
return(c(x1,x2,x3)) # last statement of the function
print("hi") # this doesn't print
}
# goal, call it like
result <- stuff(1,2,3)
# print max, min, mean
# the following would be an error, x1 doesn't exist out here
#print(x1)
isPrime <- function(n) {
# assume everything is okay
for(i in 2:(n-1)) {
#print(i)
if (n %% i == 0) {
return(FALSE)
}
}
return(TRUE)
}
print(isPrime(2013))