To find the working directory, type in R console:
## [1] "/Users/lol553/OneDrive - University of Saskatchewan/teaching/stat345-2001/rdemo"
# create a vector
x <- 1:10
x <- seq (3,30, by = 2)
a <- c(66.32, 69.87, 70.12, 90.37, 50.08, 61.20, 65.00, 57.65)
d <- a [1]
a [1] <- 85.34
Find the average of numbers in a:
## [1] 68.7
Save the mean of a as another object:
One can also read numbers using scan like this:
## [1] 7 8 9 10 11 12 13 13 14 17 17 45
Read a vector of numbers from a file:
## [,1] [,2]
## [1,] 0 0
## [2,] 0 0
## [3,] 0 0
## [4,] 0 0
## [,1] [,2]
## [1,] 1 5
## [2,] 2 6
## [3,] 3 7
## [4,] 4 8
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
## [3,] 5 6
## [4,] 7 8
## [,1] [,2] [,3] [,4]
## [1,] 1 3 5 7
## [2,] 2 4 6 8
# create another matrix with all entry 0
B <- matrix (0, 100, 50)
# assign a number to B
B[1,4] <- 4.5
B[1,] <- 1:50
head(B)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
## [1,] 1 2 3 4 5 6 7 8 9 10 11 12 13 14
## [2,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## [3,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## [4,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## [5,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## [6,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
## [1,] 15 16 17 18 19 20 21 22 23 24 25 26
## [2,] 0 0 0 0 0 0 0 0 0 0 0 0
## [3,] 0 0 0 0 0 0 0 0 0 0 0 0
## [4,] 0 0 0 0 0 0 0 0 0 0 0 0
## [5,] 0 0 0 0 0 0 0 0 0 0 0 0
## [6,] 0 0 0 0 0 0 0 0 0 0 0 0
## [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38]
## [1,] 27 28 29 30 31 32 33 34 35 36 37 38
## [2,] 0 0 0 0 0 0 0 0 0 0 0 0
## [3,] 0 0 0 0 0 0 0 0 0 0 0 0
## [4,] 0 0 0 0 0 0 0 0 0 0 0 0
## [5,] 0 0 0 0 0 0 0 0 0 0 0 0
## [6,] 0 0 0 0 0 0 0 0 0 0 0 0
## [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50]
## [1,] 39 40 41 42 43 44 45 46 47 48 49 50
## [2,] 0 0 0 0 0 0 0 0 0 0 0 0
## [3,] 0 0 0 0 0 0 0 0 0 0 0 0
## [4,] 0 0 0 0 0 0 0 0 0 0 0 0
## [5,] 0 0 0 0 0 0 0 0 0 0 0 0
## [6,] 0 0 0 0 0 0 0 0 0 0 0 0
Create a list
List the names of components:
## [1] "newa" "newA"
## List of 2
## $ newa: num [1:8] 85.3 69.9 70.1 90.4 50.1 ...
## $ newA: int [1:4, 1:2] 1 2 3 4 5 6 7 8
Look at the component of alist:
## [,1] [,2]
## [1,] 1 5
## [2,] 2 6
## [3,] 3 7
## [4,] 4 8
Modify a component:
# create a dataframe
scores <- c (30, 45, 50)
names <- c("Peter", "John", "Alice")
stat245_scores <- data.frame (names, scores)
stat245_scores$names
## [1] Peter John Alice
## Levels: Alice John Peter
stat245_scores$scores [2] <- 17
stat245_scores$perc <- stat245_scores$score/50 * 100 + 10
stat245_scores
## names scores perc
## 1 Peter 30 70
## 2 John 17 44
## 3 Alice 50 110
## [1] "names" "scores" "perc"
# import myagpop.csv into an R data frame called 'myagpop'
agpop <- read.csv ("data/agpop.csv")
# Now, we can use the data:
# preview agpop
head (agpop)
## county state acres92 acres87 acres82 farms92 farms87 farms82
## 1 ALEUTIAN ISLANDS AREA AK 683533 726596 764514 26 27 28
## 2 ANCHORAGE AREA AK 47146 59297 256709 217 245 223
## 3 FAIRBANKS AREA AK 141338 154913 204568 168 175 170
## 4 JUNEAU AREA AK 210 214 127 8 8 12
## 5 KENAI PENINSULA AREA AK 50810 85712 98035 93 119 137
## 6 AUTAUGA COUNTY AL 107259 116050 145044 322 388 453
## largef92 largef87 largef82 smallf92 smallf87 smallf82 region
## 1 14 16 20 6 4 1 W
## 2 9 10 11 41 52 38 W
## 3 25 28 21 12 18 25 W
## 4 0 0 0 5 4 8 W
## 5 9 18 17 12 18 19 W
## 6 25 32 32 8 19 17 S
## [1] "county" "state" "acres92" "acres87" "acres82" "farms92"
## [7] "farms87" "farms82" "largef92" "largef87" "largef82" "smallf92"
## [13] "smallf87" "smallf82" "region"
## [1] 15
## [1] 3078
## county state acres92 acres87 acres82 farms92 farms87 farms82 largef92
## 2 ANCHORAGE AREA AK 47146 59297 256709 217 245 223 9
## largef87 largef82 smallf92 smallf87 smallf82 region
## 2 10 11 41 52 38 W
## [1] 683533 47146 141338 210 50810 107259 167832 177189 48022 137426
## [11] 144799 96427 73841 109555 121504 99466 67950 61426 68478 47200
## [1] 683533 47146 141338 210 50810 107259 167832 177189 48022 137426
## [11] 144799 96427 73841 109555 121504 99466 67950 61426 68478 47200
## [1] 14 9 25 0 9 25 24 40 6 9 29 18 4 22 24 8 9 13 4 5
## [1] 306677
## [1] 424687
agpop_AK <- agpop [agpop$state == "AK", ]
agpop_AK <- subset (agpop, state == "AK")
agpop_W <- subset (agpop, region == "W")
agpop_largefarm <- subset (agpop, largef92 > 10)
## simple analysis
summary (agpop)
## county state acres92 acres87
## WASHINGTON COUNTY: 30 TX : 254 Min. : -99 Min. : -99
## JEFFERSON COUNTY : 25 GA : 159 1st Qu.: 80903 1st Qu.: 86236
## FRANKLIN COUNTY : 24 KY : 120 Median : 191648 Median : 199864
## JACKSON COUNTY : 23 MO : 114 Mean : 306677 Mean : 313016
## LINCOLN COUNTY : 23 KS : 105 3rd Qu.: 366886 3rd Qu.: 372224
## MADISON COUNTY : 19 IL : 102 Max. :7229585 Max. :7687460
## (Other) :2934 (Other):2224
## acres82 farms92 farms87 farms82 largef92
## Min. : -99 Min. : 0 Min. : 0 Min. : 0 Min. : 0.0
## 1st Qu.: 96397 1st Qu.: 295 1st Qu.: 318 1st Qu.: 345 1st Qu.: 8.0
## Median : 207292 Median : 521 Median : 572 Median : 616 Median : 30.0
## Mean : 320194 Mean : 626 Mean : 678 Mean : 728 Mean : 56.2
## 3rd Qu.: 377065 3rd Qu.: 838 3rd Qu.: 921 3rd Qu.: 991 3rd Qu.: 75.0
## Max. :7313958 Max. :7021 Max. :7590 Max. :7394 Max. :579.0
##
## largef87 largef82 smallf92 smallf87 smallf82
## Min. : 0.0 Min. : 0.0 Min. : 0 Min. : 0 Min. : 0
## 1st Qu.: 8.0 1st Qu.: 8.0 1st Qu.: 13 1st Qu.: 17 1st Qu.: 16
## Median : 27.0 Median : 25.0 Median : 29 Median : 35 Median : 34
## Mean : 54.9 Mean : 52.6 Mean : 54 Mean : 60 Mean : 61
## 3rd Qu.: 70.0 3rd Qu.: 65.0 3rd Qu.: 59 3rd Qu.: 67 3rd Qu.: 67
## Max. :596.0 Max. :546.0 Max. :4298 Max. :3654 Max. :3522
##
## region
## NC:1054
## NE: 220
## S :1382
## W : 422
##
##
##
Produce Plots
Histogram of farming acreages in 1992:
Scatterplot of farming acreages in 1987 versus 1992
## data is a matrix or data.frame
means_col <- function (data)
{
n <- ncol (data)
cmeans <- rep (NA, n)
for (j in 1:n)
{
cmeans[j] <- mean (data[,j])
}
names(cmeans) <- colnames(data)
cmeans
}
## apply function
means_col (agpop[, 3:13])
## acres92 acres87 acres82 farms92 farms87 farms82 largef92 largef87
## 306676.97 313016.38 320193.69 625.50 678.28 728.06 56.18 54.86
## largef82 smallf92 smallf87
## 52.62 54.09 59.54
## acres92 acres87 acres82 farms92 farms87 farms82 largef92 largef87
## 306676.97 313016.38 320193.69 625.50 678.28 728.06 56.18 54.86
## largef82 smallf92 smallf87
## 52.62 54.09 59.54