R fundamentals 1:variables, operators and vectorized operations

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值