Let’s create the simplest object in R, a vector x, that consists of the numbers 1, 2, and 4:
x <- c(1,2,4)
The c() operator concatenates the three one-element vectors 1, 2, and 4. Also, note the assignment operator <- -it works much like =, but there are special circumstances where the two are different so we want to make sure we always use the former. Now, let’s see what happens when we concatenate vectors that contain more than one element:
q <- c(x,x,8)
q
## [1] 1 2 4 1 2 4 8
What type of R object is q? > Vector q is a numeric vector
The dimension (or length) of a vector corresponds to its number of elements. The dimension of x is 3. What is the dimension of q?
There are 7 elements in the numeric vector q
A vector is a special case of a matrix. Think of a matrix for now as an object whose rows (or columns) are made up by vectors of the same length-each of the vectors is a row (or a column) in the matrix. When we describe the dimensions of a matrix, we indicate the number of rows and columns as: rxc. What are the dimensions of y? Of z?
y <- cbind(x,x)
z <- rbind(x,x)
dim(y)
## [1] 3 2
The dimension of y is a matrix of 3 rows and 2 columns
dim(z)
## [1] 2 3
The dimension of z is a matrix of 2 rows and 3 columns
One of the datasets is called Orange and contains data on the growth of orange trees. Let’s investigate it:
names(Orange) ## What are the variable names in the dataset?
names(Orange)
## [1] "Tree" "age" "circumference"
Tree, age, circumference are the variables in dataset orange
Orange$age ## How to indicate a variable that is in a dataset
Orange$age
## [1] 118 484 664 1004 1231 1372 1582 118 484 664 1004 1231 1372 1582
## [15] 118 484 664 1004 1231 1372 1582 118 484 664 1004 1231 1372 1582
## [29] 118 484 664 1004 1231 1372 1582
attach(Orange) ## In short, so I can just ask for ‘age’ instead of having ## to tell R both the dataset name and the variable name
attach(Orange)
mean(age) ## Asking for the mean of the variable ‘age’
mean(age)
## [1] 922.1429
The mean age is 922.1429
sd(age) ## Standard deviation of ‘age’
sd(age)
## [1] 491.8645
The standard deviation is 491.8645
hist(age) ## Plot a histogram of the data
hist(age)
We can make this basic graph look a little better by using some of R’s graphical parameters. I’m going to change the label of the x-axis:
hist(age, xlab="Age (in years)")
Is a vector of integers starting at 5 and endind at 1 in descending order, and assign them as x:
x <- 5:1
print(x)
## [1] 5 4 3 2 1
Print after dividing the elements of the vector by half:
x/2
## [1] 2.5 2.0 1.5 1.0 0.5
Print the squares the elements of the vector:
x^2
## [1] 25 16 9 4 1
Print the result of matrix multiplication for vector x by vector x:
x%*%x
## [,1]
## [1,] 55
Print the square root of each element in x:
sqrt(x)
## [1] 2.236068 2.000000 1.732051 1.414214 1.000000
Print the minimum value in the vector:
min(x)
## [1] 1
Print the second element of vector x:
x[2]
## [1] 4
Print the second and third elements of vector x:
x[2:3]
## [1] 4 3
Print the second and third elements of vector x:
x[c(2,3)]
## [1] 4 3
Print vector x without the third element:
x[-3]
## [1] 5 4 2 1
Print a vector x with the smallest element first ordered to the largest element:
sort(x)
## [1] 1 2 3 4 5
Is vector of integers from 20 to 40; assign them to x:
x <- 20:40
print(x)
## [1] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
Print the first 5 elements of vector x:
x[1:5]
## [1] 20 21 22 23 24
Print vector x without the first 5 elements:
x[-(1:5)]
## [1] 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
Print the concatenated vector, add 1 to the first element, and procude a warning for varying vector length:
c(1,2,3) + 1
## [1] 2 3 4
Print the addition of the two vectors together:
c(1,2,3) + c(0,1)
## Warning in c(1, 2, 3) + c(0, 1): longer object length is not a multiple of
## shorter object length
## [1] 1 3 3
Print the addition of the two vectors together:
c(1,2,3) + c(1,2)
## Warning in c(1, 2, 3) + c(1, 2): longer object length is not a multiple of
## shorter object length
## [1] 2 4 4
Print the addition of two vectors:
c(1,2,3,4) + c(0,1)
## [1] 1 3 3 5
Print the addition of the two vectors together:
c(1,2,3,4) + c(1,2,3)
## Warning in c(1, 2, 3, 4) + c(1, 2, 3): longer object length is not a
## multiple of shorter object length
## [1] 2 4 6 5
Print the modulo operation:
5%%2
## [1] 1
Print the modulo operation:
6%%2
## [1] 0
Print the modulo operation:
7%%2
## [1] 1
Print the modulo operation:
10%%4
## [1] 2
Print the modulo operation:
10%%5
## [1] 0
Print the modulo operation:
10%%6
## [1] 4
Assign vector <1,2,4> to x:
x <- c(1,2,4)
print(x)
## [1] 1 2 4
Print the names for vector x:
names(x)
## NULL
Assign (a, b, ab) as the names for vector x:
names(x) <- c("a","b","ab")
print(names(x))
## [1] "a" "b" "ab"
Print x with the names for a given element:
x
## a b ab
## 1 2 4
Print the names of vector x:
names(x)
## [1] "a" "b" "ab"
Print the name and element assigned to that name:
x["b"]
## b
## 2
Assign elements from 1 to 10 to vector x:
x <- c(1:10)
print(x)
## [1] 1 2 3 4 5 6 7 8 9 10
Assign elements from 10 to 1 to vector y:
y <- c(10:1)
print(y)
## [1] 10 9 8 7 6 5 4 3 2 1
Print a matrix with columns of 1 for the length of elements in x, elements of x, elements of y as the values of the columns:
cbind(1,x,y)
## x y
## [1,] 1 1 10
## [2,] 1 2 9
## [3,] 1 3 8
## [4,] 1 4 7
## [5,] 1 5 6
## [6,] 1 6 5
## [7,] 1 7 4
## [8,] 1 8 3
## [9,] 1 9 2
## [10,] 1 10 1
Print a matrix with rows of 1 for the length of elements in x, elements of x, elements of y as the values of the rows:
rbind(1,x,y)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## 1 1 1 1 1 1 1 1 1 1
## x 1 2 3 4 5 6 7 8 9 10
## y 10 9 8 7 6 5 4 3 2 1
Create a list of lists for 1, elements of x, elements of y, and assign them to variable x:
x <- list(1,x,y)
print(x)
## [[1]]
## [1] 1
##
## [[2]]
## [1] 1 2 3 4 5 6 7 8 9 10
##
## [[3]]
## [1] 10 9 8 7 6 5 4 3 2 1
Create a 2x2 matrix with column vectors (1,2) and (3,4), and assign them to A:
A <- matrix(nrow=2,ncol=2,c(1:4))
print(A)
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
Create a 2x2 matrix with column vectors (1,2) and (3,4), and assign them to B:
B <- matrix(nrow=2,ncol=2,c(1:4))
print(B)
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
Create a vector (2,2) and assign them to variable x:
x <- c(2,2)
print(x)
## [1] 2 2
Print a matrix with column vectors (2,4) and (6,8) through matrix addition:
A + B
## [,1] [,2]
## [1,] 2 6
## [2,] 4 8
Print a matrix with column vectors (0,0) and (0,0) through matrix subtraction:
A - B
## [,1] [,2]
## [1,] 0 0
## [2,] 0 0
Print a transpose of matrix A:
t(A)
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
Print matrix A through cancelling transpose operations:
t(t(A))
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
Print a matrix with column vectors (1,4) and (9,16) through element-wise multiplication:
A*B
## [,1] [,2]
## [1,] 1 9
## [2,] 4 16
Print a matrix with column vectors (7,10) and (15,22) through matrix multiplication:
A%*%B
## [,1] [,2]
## [1,] 7 15
## [2,] 10 22
Print a matrix with column vector (8,12) through matrix multiplication with a matrix of small dimension:
A%*%x
## [,1]
## [1,] 8
## [2,] 12
Print a matrix with column vector (40) through matrix multiplication with two matrix of small dimension:
x%*%A%*%x
## [,1]
## [1,] 40
Print the inverse of A:
solve(A)
## [,1] [,2]
## [1,] -2 1.5
## [2,] 1 -0.5
Create a 4x4 matrix with column vectors (1,2,3,4),(5,6,7,8),(9,10,11,12), and (13,14,15,16) and assign them to C:
C <- matrix(nrow=4,ncol=4,c(1:16))
print(C)
## [,1] [,2] [,3] [,4]
## [1,] 1 5 9 13
## [2,] 2 6 10 14
## [3,] 3 7 11 15
## [4,] 4 8 12 16
Assign the first row of matrix A to a1:
a1 <- A[1,]
print(a1)
## [1] 1 3
Assign the second row of matrix A to a2:
a2 <- A[2,]
print(a2)
## [1] 2 4
Assign the first column of matrix B to b1:
b1 <- B[,1]
print(b1)
## [1] 1 2
Assign the second column of matrix B to b1:
b2 <- B[,2]
Print the second and third columns of matrix C:
c_c <- C[,2:3]
print(c_c)
## [,1] [,2]
## [1,] 5 9
## [2,] 6 10
## [3,] 7 11
## [4,] 8 12
Print the first and second rows of matrix C:
c_r <- C[1:2,]
print(c_r)
## [,1] [,2] [,3] [,4]
## [1,] 1 5 9 13
## [2,] 2 6 10 14
Create a 2x4 matrix with every element being 1, and assign it to variable C[c(1,3),]:
C[c(1,3),] <- matrix(nrow=2,ncol=4,c(rep(1,8)))
print(C[c(1,3),])
## [,1] [,2] [,3] [,4]
## [1,] 1 1 1 1
## [2,] 1 1 1 1
Create a vector (4,5,2,3) and transforms it into a 2x2 matrix with columns (4,5) and (2,3), and assign it to y:
y <- matrix(c(4,5,2,3), nrow = 2)
print(y)
## [,1] [,2]
## [1,] 4 2
## [2,] 5 3
Print the first row of matrix y:
y[-2,]
## [1] 4 2
Create a 3x2 matrix with column vectors (1,2,3),(4,5,6), and assign it to x:
x <- matrix(nrow=3,ncol=2,c(1:6))
print(x)
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
Print the addition of vector (0,1) staring at [1,1] and wrapping through the second column:
x + c(0,1)
## [,1] [,2]
## [1,] 1 5
## [2,] 3 5
## [3,] 3 7
Create a 3x2 matrix with column vectors (1,2,3),(4,5,6), and assign it to z:
z <- matrix(nrow=3,ncol=2,c(1:6))
print(z)
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
Print the mean of each row:
apply(z,1,mean)
## [1] 2.5 3.5 4.5
Print the mean of each column:
apply(z,2,mean)
## [1] 2 5
Create a character vector (“abc”) and assign it to y:
y <- "abc"
print(y)
## [1] "abc"
Print the length of vector y:
length(y)
## [1] 1
Print the storage type of the object:
mode(y)
## [1] "character"
Create a character vector (“abc”, “29 88”) and assign it to z:
z <- c("abc", "29 88")
print(z)
## [1] "abc" "29 88"
Print the length of vector z which is 2:
length(z)
## [1] 2
Print the storage type of the object:
mode(z)
## [1] "character"
Add elements together in on string and assigns to w_w:
w_w <- paste("Wonder", "Woman")
print(w_w)
## [1] "Wonder Woman"
Splits elements into separate strings and assigns to list ww:
ww <- strsplit(w_w, " ")
print(ww)
## [[1]]
## [1] "Wonder" "Woman"
Print a logical operator for list:
is.list(ww)
## [1] TRUE
Print the location of the matching elements to the input string:
grep("West", c("North","South","South East","North West","West"))
## [1] 4 5
Print the count of characters in the string:
nchar("You're gonna need a bigger boat.")
## [1] 32
Print the the substring of the desired characters:
substr("Jaws",2,3)
## [1] "aw"
Print the regular expression format for character matching inside a string:
regexpr("aw","Jaws")
## [1] 2
## attr(,"match.length")
## [1] 2
## attr(,"useBytes")
## [1] TRUE
Print the split of the string into elements by delimited value:
strsplit("6-16-2011", split="-")
## [[1]]
## [1] "6" "16" "2011"
Assign the value 10 to vector n:
n <- 10
print(x)
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
Print a vector of elements from 0 up to vector n but not n:
1:n-1
## [1] 0 1 2 3 4 5 6 7 8 9
Print a vector of elements from 1 up to vector n but not n:
1:(n-1)
## [1] 1 2 3 4 5 6 7 8 9
Create a vector with sequence from -10 to 10 by 2 and assign it to sequence.1:
seq(-10, 10, by = 2) -> sequence.1
print(sequence.1)
## [1] -10 -8 -6 -4 -2 0 2 4 6 8 10
Create a vector with sequence from -10 by 2 for ll elements and assign it to sequence.2:
sequence.2 <- seq(length = 11, from = -10, by = 2)
print(sequence.2)
## [1] -10 -8 -6 -4 -2 0 2 4 6 8 10
Create a vector with sequence from 1 to 6 (since x as assigned as vector [3,2]) then replicates five times and assign it to sequence.3:
sequence.3 <- rep(x, 5)
print(sequence.3)
## [1] 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
Create a vector with sequence (5,2,7) then replicates three times and assign it to sequence.4:
sequence.4 <- rep(c(5,2,7),3)
print(sequence.4)
## [1] 5 2 7 5 2 7 5 2 7
Create a vector with sequence (5,2,7) then replicates each element three times inorder and assign it to sequence.5:
sequence.5 <- rep(c(5,2,7),each=3)
print(sequence.5)
## [1] 5 5 5 2 2 2 7 7 7
Create vector (TRUE,TRUE,FALSE,FALSE) and assign it a:
a <- c(TRUE,TRUE,FALSE,FALSE)
print(a)
## [1] TRUE TRUE FALSE FALSE
Create vector (FALSE,TRUE,TRUE,FALSE) and assign it b:
b <- c(FALSE,TRUE,TRUE,FALSE)
print(b)
## [1] FALSE TRUE TRUE FALSE
Print the OR operation on if an element in other list is true:
a|b
## [1] TRUE TRUE TRUE FALSE
Print the AND operation on if an element in other list is true:
a&b
## [1] FALSE TRUE FALSE FALSE
Print the AND operation on if an element in (NOT a and b) list is true:
!a&b
## [1] FALSE FALSE TRUE FALSE
Print the AND operation on if an element in (NOT a and NOT b) list is true:
!a&!b
## [1] FALSE FALSE FALSE TRUE
Print the AND operation on if an element in NOT (a and b) list is true:
!(a&b)
## [1] TRUE FALSE TRUE TRUE
Print the addition of boolean bits for a + b:
a+b
## [1] 1 2 1 0
Assign TRUE values to matrix x if they are greater than 3 otherwise FALSE to dummy.1:
dummy.1 <- x > 3
print(dummy.1)
## [,1] [,2]
## [1,] FALSE TRUE
## [2,] FALSE TRUE
## [3,] FALSE TRUE
Assign TRUE values to matrix x if they are equal 3 otherwise FALSE to dummy.2:
dummy.2 <- x == 3
print(dummy.2)
## [,1] [,2]
## [1,] FALSE FALSE
## [2,] FALSE FALSE
## [3,] TRUE FALSE
Assign a vector from 1 to 10 to x:
x <- 1:10
print(x)
## [1] 1 2 3 4 5 6 7 8 9 10
Print a TRUE if any element is greater than 8:
any(x>8)
## [1] TRUE
Print a TRUE if any element is greater than 88:
any(x>88)
## [1] FALSE
Print a TRUE if all element is greater than 88:
all(x>88)
## [1] FALSE
Print a TRUE if all element is greater than 0:
all(x>0)
## [1] TRUE
Assign a vector from 1 to 3 to x:
x <- 1:3
print(x)
## [1] 1 2 3
Assign a vector (1,3,4) to y:
y <- c(1,3,4)
print(y)
## [1] 1 3 4
Print TRUE if elements between the two vectors match:
x == y
## [1] TRUE FALSE FALSE
Print TRUE if all elements between the two vectors match:
all(x == y)
## [1] FALSE
Print TRUE the two vectors are identical:
identical(x,y)
## [1] FALSE
Assign a vector from 1 to 2 to x:
x <- 1:2
print(y)
## [1] 1 3 4
Assign a vector (1,2) to y:
y <- c(1,2)
print(y)
## [1] 1 2
Print x and y:
x;y
## [1] 1 2
## [1] 1 2
Print TRUE the two vectors are identical:
identical(x,y)
## [1] FALSE
Print the type of vector :
typeof(x)
## [1] "integer"
Print the type of vector:
typeof(y)
## [1] "double"
Assign a vector from 1 to 3 and element NA to z:
z <- c(1:3,NA)
print(z)
## [1] 1 2 3 NA
Assign TRUE if the element is NA to z.dummy:
z.dummy <- is.na(z)
print(z.dummy)
## [1] FALSE FALSE FALSE TRUE
Assign all the elements to NA for a vector of length z to z.dummy2:
z.dummy2 <- z==NA
print(z.dummy2)
## [1] NA NA NA NA
Print NaN for not a number:
0/0
## [1] NaN
Print TRUE for is NA:
is.na(0/0)
## [1] TRUE
Print TRUE for is NaN:
is.nan(0/0)
## [1] TRUE
Print TRUE for any element in z not a number:
is.nan(z)
## [1] FALSE FALSE FALSE FALSE
Print NA since one element is NA:
mean(z)
## [1] NA
Print 2 for the mean of vector z excluding elements of value NA:
mean(z,na.rm=T)
## [1] 2
Assign a vector (88,NULL,12,168,13) to x:
x <- c(88,NULL,12,168,13)
print(x)
## [1] 88 12 168 13
Print mean of vector x which is 70.25 since null is not counted:
mean(x)
## [1] 70.25
Assign null to u:
u <- NULL
print(u)
## NULL
Print 0 since null values have no length:
length(u)
## [1] 0
Assign NA to v:
v <- NA
print(v)
## [1] NA
Print the length vector v:
length(v)
## [1] 1
Create a vector from 1 to 5 and NA replicated 3 times and assign it to x:
x <- c(c(1:5),c(rep(NA,3)))
print(x)
## [1] 1 2 3 4 5 NA NA NA
Print TRUE if the value in vector x is NA:
is.na(x)
## [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE
Print the NA elements in vector x:
x[is.na(x)]
## [1] NA NA NA
Print the not NA elements in vector x:
x[!is.na(x)]
## [1] 1 2 3 4 5
Print TRUE for values greater than 2, FALSE for less than or equal to 2, and NA for NA values:
x > 2
## [1] FALSE FALSE TRUE TRUE TRUE NA NA NA
Print TRUE for values greater than 2, otherwise FALSE:
!is.na(x)&x > 2
## [1] FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE
Print the values greater than 2 from vector x:
x[!is.na(x)&x > 2]
## [1] 3 4 5
Assign a 2x3 matrix with columns (1,2),(3,4),and (5,6) to x:
x <- matrix(1:6,2,3)
print(x)
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
Assign a list with list one (1,2,3,4,5), list two (0.6), and list three (“HI”) to w:
w <- list(one = 1:5, two = 0.6, three = "HI")
print(w)
## $one
## [1] 1 2 3 4 5
##
## $two
## [1] 0.6
##
## $three
## [1] "HI"
Print list one from w:
w$one
## [1] 1 2 3 4 5
Print list one from w:
w[["one"]]
## [1] 1 2 3 4 5
Print list one from w:
w[[1]]
## [1] 1 2 3 4 5
Print the first and third elements from list one from w:
w[[c(1,3)]]
## [1] 3
Print the third element from list one from w:
w[[1]][[3]]
## [1] 3
Print list one and three from w:
w[c(1,3)]
## $one
## [1] 1 2 3 4 5
##
## $three
## [1] "HI"
Assign vector (5,2,-3,8) to z:
z <- c(5,2,-3,8)
print(z)
## [1] 5 2 -3 8
Print z:
z
## [1] 5 2 -3 8
Print TRUE for values greater than 8 after the vector is squared:
z*z > 8
## [1] TRUE FALSE TRUE TRUE
Print the values that when squared are greater than 8:
z[z*z > 8]
## [1] 5 -3 8
Assign the values that when squared are greater than 8 to w:
w <- z[z*z > 8]
print(w)
## [1] 5 -3 8
Assign vector of 6,1,2,3,NA,12 to x:
x <- c(6,1:3,NA,12)
print(x)
## [1] 6 1 2 3 NA 12
Print the values greater than 5 and NA in the vector x:
x[x>5]
## [1] 6 NA 12
Print a subset of values greater than 5 without NA:
subset(x,x>5)
## [1] 6 12
Assign the named numeric(0) to x[x>3]:
x[x>3] <- 0
print(x[x>3])
## [1] NA
Print the location of TRUE values in vector z where the square of the element is greater than 8:
which(z*z > 8)
## [1] 1 3 4
Assign a 3x2 matrix with columns (46,21,50) and (30,25,50) to first_test:
first_test <- matrix(nrow = 3, ncol = 2, c(46,21,50,30,25,50))
print(first_test)
## [,1] [,2]
## [1,] 46 30
## [2,] 21 25
## [3,] 50 50
Assign a 3x2 matrix with columns (46,41,50) and (43,35,50) to second_test:
second_test <- matrix(nrow = 3, ncol = 2, c(46,41,50,43,35,50))
print(second_test)
## [,1] [,2]
## [1,] 46 43
## [2,] 41 35
## [3,] 50 50
Create an array of matrices first_test and second_test and assign it to tests:
tests <- array(data=c(first_test,second_test),dim=c(3,2,2))
print(tests)
## , , 1
##
## [,1] [,2]
## [1,] 46 30
## [2,] 21 25
## [3,] 50 50
##
## , , 2
##
## [,1] [,2]
## [1,] 46 43
## [2,] 41 35
## [3,] 50 50
Print the dimensions of the array tests:
attributes(tests)
## $dim
## [1] 3 2 2
Print the element of the third row, second column for matrix 1 in the array:
tests[3,2,1]
## [1] 50
Print the element of the third row, second column for matrix 2 in the array:
tests[3,2,2]
## [1] 50
Print the array tests:
tests
## , , 1
##
## [,1] [,2]
## [1,] 46 30
## [2,] 21 25
## [3,] 50 50
##
## , , 2
##
## [,1] [,2]
## [1,] 46 43
## [2,] 41 35
## [3,] 50 50
a <- seq(10)
a <- matrix(a,nrow = 5, ncol = 2)
colnames(a) <- c("A","B")
b <- matrix(1:12,nrow = 4, ncol = 3)
rownames(b) <- c("1st Row", "2nd Row", "3rd Row", "4th Row")
b[2,1]; c <- b[,2]
## 2nd Row
## 2
d <- c("James", "Art", "Kate", "Alex"); e <- c(27,42,29,33)
class(d)
## [1] "character"
class(e)
## [1] "numeric"
df<-data.frame(d,e);names(df) <- c("Name", "Age");df
## Name Age
## 1 James 27
## 2 Art 42
## 3 Kate 29
## 4 Alex 33
C <- c(23,30,19,27)
F <- c(C*(9/5)+32)
state <- c(“MO”,“KS”,“KS”,“KS”,“MO”,“NE”,“OK”,“MO”,“KS”,“NE”,“NE”,“OK”,“MO”,“KS”,“KS”)
Use the factor() function to change the character vector state to a factor and produce a frequency table that represents the number of observations in our sample from each state. Use the levels option within factor() to reorder the state categories to: Oklahoma, Kansas, Missouri, Nebraska.
state <- c("MO","KS","KS","KS","MO","NE","OK","MO","KS","NE","NE","OK","MO","KS","KS")
orderState <- factor(state,labels=c("Oklahoma","Kansas","Missouri","Nebreska"))
table(orderState)
## orderState
## Oklahoma Kansas Missouri Nebreska
## 6 4 3 2
prod <- c(2341, 9873, 23933, 1999, 7214, 9089, 10999, 2389, 5435, 5757,2521, 7333, 21909, 15110, 4369)
Use the help to investigate the tapply() function. Write the command line that will produce the mean production for each state using tapply(). Write similar command lines that will produce the minimum and maximum production for each state.
prod <- c(2341,9873,23933,1999,7214,9089,10999,2389,5435,5757,2521,733,21909,15110,4369)
tapply(prod,state,mean)
## KS MO NE OK
## 10119.83 8463.25 5789.00 5866.00
tapply(prod,state,min)
## KS MO NE OK
## 1999 2341 2521 733
tapply(prod,state,max)
## KS MO NE OK
## 23933 21909 9089 10999
All of the statistical analyses in this document will be performed using R version 3.4.1 (2017-06-30). R packages used will be maintained using the packrat dependency management system.
sessionInfo()
## R version 3.4.1 (2017-06-30)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 15063)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=English_United States.1252
## [2] LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] DT_0.2 rmarkdown_1.6 knitr_1.16
##
## loaded via a namespace (and not attached):
## [1] htmlwidgets_0.9 compiler_3.4.1 backports_1.1.0 magrittr_1.5
## [5] rprojroot_1.2 tools_3.4.1 htmltools_0.3.6 yaml_2.1.14
## [9] Rcpp_0.12.10 stringi_1.1.5 stringr_1.2.0 digest_0.6.12
## [13] evaluate_0.10