# R is a versatile program. At its simplest, it can be # a simple calculator: > 2 + 2 [1] 4 > (2 + 2)^2 [1] 16 # It is possible to create and save variables in R. Here, # for example, we create a variable called "x" which contains # the integers from 1 to 10: > x <- 1:10 > x [1] 1 2 3 4 5 6 7 8 9 10 # If we perform an arithmetic operation on such a variable, R # will apply the operation to every element of the variable: > 2*x [1] 2 4 6 8 10 12 14 16 18 20 > x^2 [1] 1 4 9 16 25 36 49 64 81 100 # Here's another variable: > y <- c(1, pi, 32, 1634) > y [1] 1.000000 3.141593 32.000000 1634.000000 > sqrt(y) [1] 1.000000 1.772454 5.656854 40.422766 # Most R commands are simple, short, and intuitive. For example, # if we want the mean of a variable (i.e., the simple arithmetic # average), the command is mean(): > mean(x) [1] 5.5 > mean(y) [1] 417.5354 # The command for standard deviation is sd(): > sd(x) [1] 3.02765 # Note that R commands are case sensitive. So SD() won't work: > SD(x) Error in SD(x) : could not find function "SD" # It might be reasonable to think that mode() would give us # the most frequently occuring value of a variable. But R actually # means something else by mode(): > mode(x) [1] "numeric" # R is telling us that the variable x consists of numbers. Here, # we create another variable where the values consist of alphabetic # characters: > names <- c("Jack","Kyle") # R knows that these are not numbers: > mode(names) [1] "character" # Because they aren't numbers, it doesn't make sense to try to # do operations such as mean(): > mean(names) [1] NA Warning message: In mean.default(names) : argument is not numeric or logical: returning NA # Another mode that R has is "logical." If I enter names=="Jack" I am asking # R whether each value of names is "Jack". That's true only for the first one: > names=="Jack" [1] TRUE FALSE # And the mode of such a variable (i.e., the one that contains TRUE, FALSE here) # is logical: > mode(names=="Jack") [1] "logical" # We can see what variables we have in our workspace using the ls() command # (short for "list"): > ls() [1] "names" "x" "y" # If I specify a particular working directory, R will save my data space # in that directory in a file called ".Rdata". If I start R next time by # double clicking on that file, I will get the same R space I left, not # including any variables that I have in other R spaces. This is useful for # avoiding clutter and keeping a limited number of variables that are # relevant to a particular project (such as this class). > setwd("c:/users/jvevea/desktop/Classes/105/2021 Spring") >