Structure | Explanation |
variables |
The result of most of the R commands can be stored in variables.
The name of a variable is a sequence of letters
and digits, but the first character must be a letter.
Thus hold, holdTheMean, xbar, answer12,
and x1 are all valid variable names.
Please note that R is case-sensitive,
meaning that Xbar and
xbar are two different names,
and thus represent
two different variables.
In R we create variables just by using the name and assigning a value to that name. Also, it is possible to use an underscore in forming a variable name, but only after the initial letter. Thus, x_bar and
hold_the_mean are valid variable names.
This style of variable names is hated by some,
avoided by others, praised
by others, and tolerated by all.
It is the form that I use quite often because, for me, it makes names easier to
recognize.
An alternative form, called cammelCase, tries to make names easy to recognize by using a capital letter to start each word in a variable name. Thus, xBar
and theSampleMean are examples of using camelCase.
|
strings | We form a string of characters into a single value by enclosing
the characters inside quotes.
Thus, "gnrnd4.R"
is a string of eight (8) characters. There are some R
commands that expect to find a string of characters.
|
comments# |
As we construct a R script we may (should) want to include comments
to tell us or some other reader what we are intending to do.
R considers anything
that follows the hash mark, # ,
that is not inside a string to be a comment.
Thus the entire line
# the following commands do problem 17 through 22
is a comment. Furthermore, in the command source("../gnrnd4.R") # load gnrnd4()
into the environment
everything from the # to the end of the line is a comment.
|
Assignment<– |
R uses the two character sequence <– as
the assignment operator. Thus the R command
my_age <- 71
assigns the value on the right side of <– , namely 17,
to the variable on the left side of <– ,
in this case, my_age .
Please note that this is a two character symbol.
There should not be a space between the two characters.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# show off all the internals # the combine function, c() grades <- c(89, 86, 92, 99, 100, 83) #then just give the variable name # to see the values stored in it grades pchisq(4.23,12) pchisq(8.31,15,lower.tail=FALSE) pnorm(-0.83) pnorm(1.68,lower.tail=FALSE) pnorm(22.4, mean=28.3, sd=6.7) L1 <- c(6,1,3,13,-1,8,9) L2 <- c(6,5,4,7,3,5,6) L1 L2 plot(L1,L2, xlim=c(-5,15), ylim=c(-2,10)) abline( h=0,v=0) cor(L1,L2) # make graph paper plot(NULL,NULL,xlim=c(-10,10), ylim=c(-10,10), xlab="", ylab="", las=1) abline(h=seq(-10,10,1), v=seq(-10,10,1), lty="dotted", col="darkgray") abline(h=0,v=0) hold_vals <- c(5, 8, 2, 11, 4) barplot(hold_vals) hold_list <- c(11, 6, 4, 6, 11, 2, 4, 2, 11, 2, 6, 6, 7, 11, 4, 7, 11, 2, 6, 2, 11, 2, 4, 2) barplot(table(hold_list)) # just to be sure that gnrnd4 is in # this environment source("../gnrnd4.R") # run the function as specified gnrnd4(1478134404,11500542) # gnrnd4 puts values into L1, look at them L1 #generate the plot, but horizontally boxplot(L1, horizontal=TRUE) # just to be sure that gnrnd4 is in # this environment source("../gnrnd4.R") # run the function as specified gnrnd4(0572427404,300010) head(L1) head(L1,n=10) # just to be sure that gnrnd4 is in # this environment source("../gnrnd4.R") # run the function as specified gnrnd4(0572427404,300010) tail(L1) tail(L1,n=10) # just to be sure that gnrnd4 is in # this environment source("../gnrnd4.R") # run the function as specified gnrnd4(1372854404,17200502) # that puts the values in L1, look at them L1 # then make a histogram of them hist(L1) hold_list <- c(11, 6, 4, 6, 11, 2, 4, 2, 11, 2, 6, 6, 7, 11, 4, 7, 11, 2, 6, 2, 11, 2, 4, 2) length(hold_list) L1 <- c(6,1,3,13,-1,8,9) L2 <- c(6,5,4,7,3,5,6) lm(L2~L1) lm_hold <- lm(L2~L1) lm_hold L1 <- c(6,1,3,13,-1,8,9) L2 <- c(6,5,4,7,3,5,6) lm_hold <- lm(L2~L1) c_hold <- coefficients(lm_hold) # now look at just the first coefficient c_hold[1] # and then at the second coefficient c_hold[2] # now use them in an expression c_hold[1]+c_hold[2]*6.3 small_list <- c(12, 18, 15, 19, 15, 11,17, 13, 15, 16) max(small_list) new_list <- c(12, 18, 12, 19, 14, 11, 17, 13, 15, 16, 18, 17) mean(new_list) new_list <- c(12, 18, 12, 19, 14, 11, 17, 13, 15, 16, 18, 17) median(new_list) small_list <- c(12, 18, 15, 19, 15, 11,17, 13, 15, 16) min(small_list) small_list <- c(12, 18, 15, 19, 15, 11,17, 13, 15, 16) mode(small_list) getOption("digits") 4/7 options(digits=12) 4/7 options(digits=7) 4/7 L1 <- c(6,1,3,13,-1,8,9) L2 <- c(6,5,4,7,3,5,6) plot(L1,L2) plot(L1,L2, main="Demonstration of plot()", xlim=c(-6,16), ylim=c(-6,10), pch=16, las=1, xaxp=c(-6,16,11), yaxp=c(-6,10,8), cex.axis=0.7, xlab="x values", ylab="y values") abline( h=seq(-6,10,2), v=seq(-6,16,2), col="darkgray", lty="dotted") abline(h=0,v=0,col="blue") pf(0.37,12,32) pf(1.78,32,12,lower.tail=FALSE) pt(-1.83,12) pt(1.94,7,lower.tail=FALSE) qchisq(0.127,11) qchisq(.158,9,lower.tail=FALSE) qf(0.125,13,42) qf(.2,37,8,lower.tail=FALSE) qt(0.085,24) qt(0.09,7,lower.tail=FALSE) qnorm(0.214) qnorm(0.345,lower.tail=FALSE) qnorm(0.12,mean=158,sd=37) x <- c(6.2, 7.3, 5.7, 9.2, 8.3) f <- c( 5, 2, 4, 1, 4 ) new_list <- rep(x,f) new_list L1 <- c(6,1,3,13,-1,8,9) L2 <- c(6,5,4,7,3,5,6) lm_hold <- lm(L2~L1) residuals(lm_hold) seq(3, 35, 4) seq(3, 38, 4) seq( 3, 38, 7.23) new_list <- c(12, 18, 12, 19, 14, 11, 17, 13, 15, 16, 18, 17) sort(new_list) # note that newlist is not changed new_list # now, redo the sort and assign it back new_list <- sort(new_list) # so now it is changed new_list new_list <- c(12, 18, 12, 19, 14, 11, 17, 21, 15, 16, 18, 17) summary(new_list) mean(new_list) options(digits=9) summary(new_list) options(digits=7) L1 <- c(6,1,3,13,-1,8,9, 12, 14) L2 <- c(6,5,4, 7, 3,5,6, 6, 8) lm_hold <- lm(L2~L1) lm_hold summary(lm_hold) alpha_list <- c("c","a","t", "d","o","g", "c","a","g","e", "d","o","d","g","e", "d","i","m") table(alpha_list) source("../gnrnd4.R") gnrnd4(0803273904,200035) L1 table(L1) new_list <- c(12, 18, 12, 19, 14, 11, 17, 13, 15, 16, 18, 17) sd( new_list ) # demo of sample() sample( 1:1500,34, replace=FALSE) # demo of sum() new_list <- c(12, 18, 12, 19, 14, 11, 17, 21, 15, 16, 18, 17) sum(new_list) |