Skip to content

Instantly share code, notes, and snippets.

@natbusa
Last active December 27, 2015 08:59
Show Gist options
  • Save natbusa/7300762 to your computer and use it in GitHub Desktop.
Save natbusa/7300762 to your computer and use it in GitHub Desktop.
Basic R tryouts
library('ggplot2')
#basics
3*4
2^3
1/2 + 1/2*sqrt(5)
#those two are identical
(1+2+3+4)/4
sum(1:4)/4
#load data
data(women)
#data frames basic
ladies = women
#inch is 2.54 cm
inch.for.meter = 2.54/100
#lengths in mt
ladies$lengthM = ladies$height * inch.for.meter
#kg is 2.2046 pounds
pound.for.kg = 1/2.2046
#weight in kg
ladies$weightKg = ladies$weight * pound.for.kg
#quetelet index
ladies$QueteletIdx = ladies$weightKg / ladies$lengthM^2
#statistics
mean(ladies$QueteletIdx)
var(ladies$QueteletIdx)
median(ladies$QueteletIdx)
#scatterplot (weight vs length)
qplot(ladies$lengthM,ladies$weightKg)
#box plot
boxplot(ladies$QueteletIdx)
#basic vector
1:10
5:1
#sum of squares
x = 1:50
sum(x*x)
#bin
bin = 2 ^ (3:0)
bin
#bin to dec
dec = sum(c(1,0,1,1)*bin)
dec
# runif
x = runif(500)
#basic
hist(x)
plot(density(x))
rug(x)
# qplot vs ggplot check:
# http://stackoverflow.com/questions/5322836/choosing-between-qplot-and-ggplot-in-ggplot2
#ggplot2 package: qplot
qplot(x, geom="histogram")
#ggplot2: using straight ggplot (requires conversion to data.frame)
ggplot(data.frame(x), aes(x)) + geom_histogram()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment