天天看點

R fundamentals 1:variables, operators and vectorized operations

文章目錄

    • variables and environment
      • make custom environment
      • assign new variable in the custom environment
      • get variables from a custom environment
    • operators
      • arithmetic operator
        • exact divison
        • integar division
        • modulus
        • exponent
        • format big numbers without a scientific notation
      • mathematical functions
        • absolute function
        • factorial
        • log function
      • set global settings
    • special numbers
      • benefits
      • data overflow
        • "Inf" for positive infinity
        • check if the calculation is infinite
      • undefined
        • "NaN" for no mathematical sense
        • check if NaN
      • missing value
        • "NA" for missing value
        • check if missing value
    • logical operator
      • "|"for or
    • vectorized operations
      • benefit
      • vector concept
      • vectorized operation FLavor 1:
      • vectorized operation FLavor 2:
      • vectorized operation Flavor 3:

variables and environment

match.score=300

match.score

make custom environment

test=new.env()
           

assign new variable in the custom environment

test$x=300
           

get variables from a custom environment

test$x
           

operators

arithmetic operator

exact divison

10/3
           

integar division

division with an integer outcome

10%/%3
           

modulus

10%%3
           

exponent

10**5
           

format big numbers without a scientific notation

format(10**5,scientific = FALSE)
           

mathematical functions

absolute function

abs(-5)
           

factorial

factorial(3)
           

log function

natural number e as the base

log(2)
           

choose a log base

log(2,base=2)
           

set global settings

lots of default settings in Rstudio

options()
           

change the decimal digits

In default, decimals have 7 digits after the point

options(digits = 10)
           

pi with 10 digits after the point

special numbers

benefits

for data overflow, no mathematical sense, missing data

for program execution or to terminate the adverse program gracefully

data overflow

1/0
           

“Inf” for positive infinity

Inf+1
           

check if the calculation is infinite

is.infinite(1/0)
           

undefined

Inf/Inf
           

“NaN” for no mathematical sense

NaN is short for “not a number”

check if NaN

is.nan(Inf/Inf)
           

missing value

“NA” for missing value

NA+5
           

check if missing value

is.na(NA+5)
           

logical operator

2>3
           

"|"for or

T|F
           

vectorized operations

benefit

explicit coding, no need for loop

vector concept

vector: the one-dimensional set of value of similar type

student.scores=c(1,2,3)
           

vectorized operation FLavor 1:

__ input a vector and output a scalar__

mean(student.scores)
           

vectorized operation FLavor 2:

input a vector and output a vector

final.marks=student.scores+5

final.marks

final.marks>2
           

vectorized operation Flavor 3:

input multiple vectors and output a vector

math.scores=c(1,2,3)
sports.scores=c(2,3,4)
total.scores=math.scores+sports.scores
total.scores