Skip to content

Commit 77701a5

Browse files
Chris R. AlbonChris R. Albon
authored andcommitted
Additional snippits.
1 parent bdd307a commit 77701a5

13 files changed

+233
-0
lines changed

apply-lapply-sapply.r

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Using apply, sapply, and lapply
2+
# Original source: http://www.r-bloggers.com/using-apply-sapply-lapply-in-r/
3+
4+
## Create some data
5+
data <- matrix(data=cbind(rnorm(100, 0), rnorm(100, 25), rnorm(100, 50)), nrow=100, ncol=3)
6+
7+
## Apply
8+
9+
### The apply function performs some action on a block of values. Note, all the values must be of the same data type (e.g. numeric, string, etc.). If they contain both numeric and string, the numeric values will be converted into strings before the function is applied.
10+
11+
### Find mean value of each row (denoted by "1")
12+
apply(data, 1, mean)
13+
14+
### Find mean value of each column (denoted by "2")
15+
apply(data, 2, mean)
16+
17+
#### Find mean of reach additional value. That is, apply the mean() functions to each individual observation instead of entire rows or entire columns (denoted by "1:2")
18+
apply(data, 1:2, mean)
19+
20+
## Sapply
21+
22+
### Sapply applies a function over each individual element in an object, returning a vector
23+
sapply(data, mean)
24+
25+
## Lapply
26+
27+
### Lapply applies a function of each individual element in an object, returning a list
28+
29+
### Create a list with two elements
30+
l <- list(a = 1:10, b = 11:20)
31+
32+
### Apply the sum function to each list
33+
lapply(l, sum)

boxplots.r

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Boxplots
2+
## Original Source: http://www.statmethods.net/graphs/boxplot.html
3+
4+
## Create some fake data about different types of casualties in a war
5+
deaths <- c(234,234,643,74,323,67,34,78,434)
6+
population <- factor(c("civilian", "soldier", "rebel"))
7+
war <- cbind(deaths, population)
8+
9+
## Create a boxplot of the number of deaths, seperated by populaton type, with teach boxplot labelled with the factor name, and colored different shades of red
10+
boxplot(deaths~population,data=war, main="Deaths By Type", xlab="Type of Victims", ylab="Number Of Deaths", names=population, col=c("red3","indianred4","orangered1"))
11+

change-object-class.r

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Change The Class Of An Object
2+
3+
# Changing the class of an object is called "casting".
4+
5+
# Create a character string object
6+
number.of.casualties <- "929040"
7+
8+
# Convert it into a numeric object
9+
number.of.casualties <- as.numeric(number.of.casualties)

classes.r

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Classes
2+
3+
# All variables have a class. Variables also have modes and storage types, but those are legacy and don't worry about them.
4+
5+
# Find a variable's class
6+
class(TRUE)
7+
8+
# R has three classes of numbers
9+
10+
# Numeric
11+
class(sqrt(3))
12+
13+
# Complex
14+
class(3i)
15+
16+
# Integer (add L to make a number an integer)
17+
class(3L)
18+
19+
# Integer
20+
class(3:33)
21+
22+
# R Also Has Other Classes
23+
24+
# Characters (Strings, like text)
25+
class(c("Arizona", "Maryland"))
26+
27+
# Factors (like unordered categories)
28+
class(factor(c("male", "female")))
29+
30+
# Factors have both values (i.e. a label) and a level (i.e. a numeric ID number)
31+
gender <- factor(c("male", "female"))
32+
33+
# View the values
34+
levels(gender)
35+
36+
# View the number of levels
37+
nlevels(gender)
38+
39+
# View the levels of each element of a factor
40+
as.integer(gender)
41+
42+
# View the values of a factor as character strings
43+
as.character(gender)

element-names.r

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Element Names
2+
3+
# Each element in a vector can have a name assigned to it
4+
5+
# Create a vector with names for values
6+
percent.sms <- c(high = 94, low = 23, 50)
7+
8+
# List names of a vector
9+
names(percent.sms)

high-res-plots.r

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Create High-Res Plots In R
2+
3+
## Generate 100,000 normally distributed observations
4+
x <- rnorm(100000)
5+
6+
# Create a 1600x1600 png at 300px/in
7+
png("100kPoints300dpi.png", units = "px", width=1600, height=1600, res=300)
8+
9+
# Plot the 100 points on the png
10+
plot(x, main="100,000 points", col=adjustcolor("black", alpha=0.2))
11+
dev.off()

indexing.r

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Indexing
2+
3+
# Create a variables of five elements
4+
x <- c(1, 4, 9, 16, 25)
5+
y <- c(1, 4, 4, 36, 24)
6+
m <- data.frame(x, y)
7+
8+
# Select the first, third, and fifth elements
9+
x[c(1,3,5)]
10+
11+
# Select of all EXCEPT the second and fourth elements
12+
x[c(-2, -4)]
13+
14+
# Select of all EXCEPT the second and fourth elements
15+
x[c(TRUE, FALSE, TRUE, FALSE, TRUE)]
16+
17+
# Select the entire first row of a matrix, array, or dataframe
18+
m[1, ]
19+
20+
# Select an the entire first column of a matrix, array, or dataframe
21+
m[, 1]

lists.r

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Lists
2+
3+
# Lists are objects whose elements can be anything, from matrices to functions.
4+
5+
# Create a numeric vector
6+
x <- runif(50)
7+
8+
# Create a character string vector
9+
y <- state.name
10+
11+
# Create a matrix
12+
m <- matrix(c(3, 2, 4, 3), nrow= 2)
13+
14+
# Create a list containing all of the above
15+
l <- list(x, y, m)
16+
17+
# You can name each element in a list
18+
names(l) <- c("percent voted", "state", "vote matrix")

pie-chart.r

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Pie Chart
2+
# Original source: http://www.statmethods.net/graphs/pie.html
3+
4+
# Create some casualty numbers
5+
slices <- c(10, 12, 4, 16, 8)
6+
7+
# Create labels that correspond to the casualty numbers
8+
lbls <- c("US", "UK", "Australia", "New Zealand", "France")
9+
10+
# Create Percents For Each Slice
11+
pct <- round(slices/sum(slices)*100)
12+
13+
# Add Percents To Labels
14+
lbls <- paste(lbls, pct)
15+
16+
# Add The Percent Symbol To Labels
17+
lbls <- paste(lbls,"%",sep="")
18+
19+
# Create a pie chart with labels, with each slice colored by the terrain color pallete
20+
pie(slices,labels = lbls, col=terrain.colors(length(lbls)), main="Casualties")

random-numbers.r

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Random Numbers
2+
3+
# Generate 3000 random numbers between 0 and 1
4+
runif(3000)
5+
6+
# Generate 3000 random numbers between 5 and 1-
7+
runif(3000, 5, 10)
8+
9+
# Generate 3000 random integers between 1 and 10, with replacement
10+
sample(1:10, 3000, replace=T)
11+
12+
# Select 3 random integers from a pool of 100 integers that range between 1 and 100
13+
sample(1:100, 6, replace=F)
14+
15+
# Select three state names witout replacement
16+
sample(state.name, 3)

0 commit comments

Comments
 (0)