CSC121 - R Console Monday January 8th, 2018 --------------------------- > # Simplest command is..nothing! The empty command. R does nothing and shows no output. > > > # R as a calculator > 2 [1] 2 > # Don't worry about the [1] for now, we'll talk about it later > 2 + 3 [1] 5 > 11 + 56 [1] 67 > 12 - 2 [1] 10 > 4 * 5 [1] 20 > 2 + 3 + 5 [1] 10 > 2 ^ 3 [1] 8 > 2 ** 3 [1] 8 > # Anything after this # sign, is not evaluated > # this is a comment > # ** is the exponentiation operator > > # Whitespace: R doesn't care how much whitespace you use, but use good style! > 2 + 3 [1] 5 > 1+2 [1] 3 > 2 * 7 [1] 14 > # GOOD STYLE: One space between operators and operands > 4 + 5 [1] 9 > 8 / 3 [1] 2.666667 > 4 / 2 [1] 2 > # Can use 'typeof' to find type of the data > typeof(2.4) [1] "double" > typeof(2) [1] "double" > # 'double' has nothing to do with the number 2. typeof(3) is also double. > > # as.integer returns the closest integer lower than the current number > as.integer(2.4) [1] 2 > as.integer(2.99999) [1] 2 > as.integer(2.99999999999) [1] 2 > # Case when too many trailing 9's can evaluate to higher integer > as.integer(2.99999999999999999999999999) [1] 3 > > as.integer(2.3426) [1] 2 > typeof(as.integer(2.3426)) [1] "integer" > as.double(2.43467) [1] 2.43467 > 8 %% 3 [1] 2 > # %% is the modulor operator > # It gives back the remainder when dividing p / q > 10 %% 2 [1] 0 > 11 %% 2 [1] 1 > > # %/% Is integer division. The result of p / q without the remainder. > > 4 %/% 2 [1] 2 > 8 %/% 3 [1] 2 > 8 / 3 [1] 2.666667 > 7 %/% 2 > > # Negation [1] 3 > -2 [1] -2 > --2 [1] 2 > # Can put lots of negative signs > ------------------------------------------------------2 [1] 2 > 10 - -3 [1] 13 > # 10 "-" -3, this is the subtraction operator > # 10 - "-"3, this is the negation operator > > # a [OPERATOR] b - we call this a binary operator > # [OPERATOR]a - we call this a unary operator > > # Compound Expressions: > 10 + 2 * 3 + 4 [1] 20 > 10 * 2 + 3 * 4 [1] 32 > 10 * (2 + 3) * 4 [1] 200 > > # Precedence of operations > # From high to low > # ^ exponentiation > # - negation > # *, /, %/%, %% - muliplication, division, modulo > # +,- - addition, subtraction > > # We want to store all of these expressions somewhere > # VARIABLES > p Error: object 'p' not found > # Need to 'define' p > p <- 5 > p [1] 5 > # To assign a value to a variable > # variableName <- expression > > p <- 2 ** 3 + 10 > p [1] 18 > q <- p + 10 > q [1] 28 > x <- 10 > x [1] 10 > x <- x + 1 > x [1] 11 > # We first evaluate the expression on the right side > # and then we assign it to the variable on the left side > y <- f + 2 Error: object 'f' not found > # Need to define f before use it in an expression > > x = 3 > # In R, we use the arrow <- to assign variables > # Using '=' or others will make you lose marks for style > q + 10 <- p Error in q + 10 <- p : could not find function "+<-" > # The left side must ONLY have a variable name -- the variable name you want > # to assign the expression on the right side to. > > # Errors: > 2 + 4)*3 Error: unexpected ')' in "2 + 4)" > 2 / 0 [1] Inf > # Infinity is a special number - we'll talk about it later > > # VARIABLE Naming > # A variable name can be any sequence of letters, digits, some symbols > # It CAN'T start with "_", a digit, or with a '.' followed by a digit > # choosing variable names is something you should learn to do well > > # Camel case > myAverage <- (1 + 2 + 10)/3 > # first letter lowercase, all other words uppercase > > # Don't choose single letter words for variables in general > # like q or p > # Have a descriptive name that helps people understand what you are talking about > > # doesn't work: > 1lab <- 2 Error: unexpected symbol in "1lab" > lab1 <- 2 > # works! >