Day_1 Part_2 Basic commands calculation & operation in R

1. Assignment

请添加图片描述
推荐使用 variable <- 666这种赋值方法

2. Calculation

符号运算
+
-
*
/除(正常除)
^乘方
log()log computes logarithms, by default natural logarithms, log10 computes common (i.e., base 10) logarithms, and log2 computes binary (i.e., base 2) logarithms. The general form log(x, base) computes logarithms with base base.
exp(x)e 的x次方
%%取余数
%/%取整(x%/%y 即求x/y的整数部分)

NB:和python的区别:
(1)python和R中乘都是用*表示。但是python中乘方是**,而R中是^
(2)python 3和R中/都可以表示除法,但python中//表示地板除而R不可以。
(3)python中exp() pi log()等都需要调用math包,然而R不要
(4)python中%表示取余数,而R中%%表示取余数%/%表示取整数。
Example:
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

3. rounding

函数简述详述 from R
floor(x)向下取整floor takes a single numeric argument x and returns a numeric vector containing the largest integers not greater than the corresponding elements of x.
ceiling(x)向上取整ceiling takes a single numeric argument x and returns a numeric vector containing the smallest integers not less than the corresponding elements of x.
round(x, digits = 0)四舍五入取整round rounds the values in its first argument to the specified number of decimal places (default 0). See ‘Details’ about “round to even” when rounding off a 5.
trunc(x, …)向0取整trunc takes a single numeric argument x and returns a numeric vector containing the integers formed by truncating the values in x toward 0.
signif(x, digits=6)保留给定位数的精度signif rounds the values in its first argument to the specified number of significant digits.

Arguments:

Argumentsdescription (from R)
xa numeric vector. Or, for round and signif, a complex vector.
digitsinteger indicating the number of decimal places (round) or significant digits (signif) to be used. Negative values are allowed (see ‘Details’).
arguments to be passed to methods.

Example:
请添加图片描述

4. Operating vectors

a. Generating

请添加图片描述

b. indexing

>vec4 <- seq(from = 1, to = 12, by = 1.333)
>vec4
[1]  1.000  2.333  3.666  4.999  6.332  7.665  8.998 10.331 11.664

请添加图片描述
请添加图片描述
NB: (1)区别于python和其他语言,R的是从1开始索引
(2)在R中index的负号表示出去该index。比如vec4[-4],表示vec4中除去第四个元素的其他所有元素。而python中-号表示倒数,vec4[-4]会表示倒数第四个元素(python倒着索引从-1开始)。

c. comparison

请添加图片描述

5. Operating list

vec1 <- c(1, 2, 3)
vec2 <- 1:11
vec3 <- rep(x = 4, 7)

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值