Last active
September 27, 2016 17:53
-
-
Save ashutoshnanda/20d335f2c317d54b478c8fec4e742239 to your computer and use it in GitHub Desktop.
Code from Introduction to Programming in R (9/14)
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
3 * 8 / 39 | |
pizza <- 10 | |
pizza | |
View(pizza) | |
pizza <- 5 | |
pizza ^ 5 | |
c(1, 7, 13, 10) | |
c(1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10) | |
c(1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10) | |
c(1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1) | |
c(1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1) | |
c(1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1, 7, 13, 10, 1) | |
#Ctrl-L clears the console | |
#Ctrl-U clears the line | |
c(1, 7, 10) * 4 | |
c(1, 7, 10, 13) * c(1, 2) | |
c(1, 2, 3, 4) * c(1, 2, 3, 4) | |
c(1, 2) * c(1, 2, 3, 4) | |
c(1, 2, 3) * c(10, 20, 30, 40, 50) | |
?c | |
power.of.two <- function(x) {x ^ 2} | |
power.of.two(7) | |
power.of.two(7) | |
power.of.two(x = 7) | |
multiply.us <- function(x, y) {x * y ^ 2} | |
multiply.us(1, 4) | |
multiply.us(y = 1, x = 4) | |
multiply.us(y = 10, x = 4) | |
source('~/multiply.R') | |
source('~/multiply.R') | |
hi <- function(x) {x ^ 2} | |
rm(hi) | |
?rm | |
?rm | |
rm(pizza) | |
ls() | |
rm(list = ls()) | |
rm(list = ls()) #Clears all environment variables (including functions), so be careful! | |
q() | |
numbers.between.one.and.ten <- 1:10 | |
# Tab is for auto-complete | |
numbers.between.one.and.ten | |
?seq | |
seq(1, 10) | |
seq(from = -10, to = 10, by = 0.5) | |
seq(from = -10, to = 10, by = 0.5)[7] | |
seq(from = -10, to = 10, by = 0.5)[1:5] | |
a <- c(1, 2, 3) | |
b <- c(4, 5, 6) | |
c(a, b) | |
?rep | |
rep(0, 100) | |
rep(0, 100) | |
rep(NA, 100) | |
rep("a", 100) | |
?NA | |
?length | |
length(1:10) | |
length("abc") | |
length(c("abc", "def")) | |
length(1:10) | |
if(length(1:10) < 5) { | |
print("small") | |
} | |
if(length(1:10) < 5) { | |
print("small") | |
} else {} | |
if(length(1:10) < 5) { | |
print("small") | |
} else { | |
print("large") | |
} | |
T | |
F | |
?ifelse | |
a <- 1:10 | |
a | |
ifelse(a < 5, 5, a) | |
a < 5 | |
ifelse(a < 5, 99, a) | |
rep(c(T, F), 5) | |
ifelse(rep(c(T, F), 5), 99, a) | |
a | |
ifelse(rep(c(T, F), 5), c(1, 2), a) | |
ifelse(rep(c(T, F), 5), c(100, 200), a) | |
a <- 1:20 | |
for(i in 1:10){print(i)} | |
#Hitting up/down arrow moves through history | |
#Hitting up/down arrow moves through history | |
for(i in 1:10){cat(i)} | |
# <keyword: if, for, etc.> (<condition/"index in <vector>") {code that will be executed} | |
function(x, y, z) {x * y * z} | |
?rgamma | |
rgamma(10, 0.5, 5) | |
rpois(100, lambda = 10) | |
table(rpois(100, lambda = 10)) | |
table(rpois(100, lambda = 10)) | |
table(c(T, F, T, F, T)) | |
table(rnorm(100)) | |
c(50, 58, 62, 47) <= 50 | |
table(c(50, 58, 62, 47) <= 50) | |
unique(rpois(100, lambda = 10)) | |
?rnorm | |
hist(rnorm(100)) | |
hist(rnorm(1000, sd = 100)) | |
hist(rnorm(1000, sd = 100), breaks = 20) | |
hist(rnorm(1000, sd = 100), breaks = 100) | |
hist(rnorm(1000, sd = 100), breaks = 50) | |
?title | |
title("My Plot Title Here") | |
hist(rnorm(1000, sd = 100), breaks = 100) | |
hist(rnorm(1000, sd = 100), breaks = 100, main = "My Plot Title Here") | |
hist(rnorm(1000, sd = 100), breaks = 100, main = "My Plot Title Here", xlab = "Value", ylab = "Frequency") | |
hist(rnorm(1000, sd = 100), breaks = 100, main = "My Plot Title Here", xlab = "Value", ylab = "Frequency", col = "blue") | |
hist(rnorm(1000, sd = 100), breaks = 100, main = "My Plot Title Here", xlab = "Value", ylab = "Frequency", col = "blue", border = "green") | |
hist(rnorm(1000, sd = 100), breaks = 10, main = "My Plot Title Here", xlab = "Value", ylab = "Frequency", col = "blue", border = "yellow") | |
x <- rnorm(100) | |
x <- runif(100, min = -5, max = 5) | |
y <- 3 * x + 5 + rnorm(100, sd = 0.4) | |
plot(x, y) | |
abline(a = 5, b = 3) | |
x <- runif(100, min = -5, max = 5) | |
y <- 3 * x + 5 + rnorm(100, sd = 0.4) | |
plot(x, y) | |
abline(a = 5, b = 3) | |
points(x, 4 * x + 7 + rnorm(100, sd = 0.3)) | |
lines(x, y) | |
lines(1:10, rnorm(10)) | |
plot(1:10, rnorm(10)) | |
b <- rnorm(10) | |
plot(1:10, b) | |
lines(1:10, b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment