R语言笔记(一)

文章目录

  • [一、R objects](#一、R objects)
  • [二、Types of data](#二、Types of data)
  • 三、Operators
    • 1、Operators
    • [2、Comparison operators](#2、Comparison operators)
    • [3、Logical operators](#3、Logical operators)
  • [四、Check types of data objects](#四、Check types of data objects)
  • [五、Convertion between data objects](#五、Convertion between data objects)
  • [六、R workspace](#六、R workspace)

一、R objects

Two basic types of things/objects: data and functions

  • Data : things like 6, "six", 6.000 6.000 6.000, and [ 6 6 6 6 6 6 ] \left[ \begin{array}{ccc} 6 & 6 & 6 \\ 6 & 6 & 6\end{array}\right] [666666]
  • Functions : things like log, + (takes two arguments), < (two), sum (multiple), and mean (one)

二、Types of data

  • Booleans
  • Integers
  • Characters
  • Floating point numbers : an integer times a positive integer to the power of an integer, as in 3 × 1 0 6 3 \times 10^6 3×106 or 1 × 3 − 1 1 \times 3^{-1} 1×3−1
    • Double precision: 64 bits (8 bytes); Single precision: 32 bits (4 bytes)
  • Missing or ill-defined values : NA, NaN, etc.

三、Operators

1、Operators

  • Unary : take just one argument. E.g., - for arithmetic negation, ! for Boolean negation
  • Binary : take two arguments. E.g., +, -, *, and / (though this is only a partial operator). Also, %% (for mod), and ^ (again partial)
  • 一元运算符:只接受一个参数。例如,负号(-)用于算术取反,感叹号(!)用于布尔取反。
  • 二元运算符:接受两个参数。例如,加号(+)、减号(-)、乘号(*)和除号(/)(尽管除号只是部分运算符)。还有求余运算符(%%),以及幂运算(^)(同样是部分运算符)。
c 复制代码
# %/% 执行整除运算,即只保留商的整数部分,舍弃余数。
# %% 是取模运算符,它返回除法后的余数。
# %/% 和 %% 常配合使用来获取整数商和余数。

10 / 3   # 结果是 3.333333
10 %/% 3 # 结果是 3(商的整数部分)
10 %% 3  # 结果是 1(余数)

2、Comparison operators

These are also binary operators; they take two objects, and give back a Boolean.

c 复制代码
6 > 5
6 < 5
6 >= 7

3、Logical operators

These basic ones are & (and) and | (or).

c 复制代码
(5 > 7) & (6 * 7 == 42)
## [1] FALSE

(5 > 7) | (6 * 7 == 42)
## [1] TRUE

Note: The double forms && and || are different!


四、Check types of data objects

  • The typeof() function returns the data type
  • is.x() functions return Boolean values indicating whether the argument is of type x
c 复制代码
typeof(6)
## [1] "double"
is.double(7)
## [1] TRUE
## 解释:在 R 语言中,数字的默认类型是 双精度浮点数(double)。

is.na(7)
## [1] FALSE
is.na(7/0)
## [1] FALSE
## 解释:跟数学里不同,7/0 = Inf  

is.na(0/0)
## [1] TRUE

五、Convertion between data objects

  • as.x() (tries to) "cast" its argument to type x, to translate it sensibly into such a value
c 复制代码
as.character(5/6)
## [1] "0.833333333333333"

as.numeric(as.character(5/6))
## [1] 0.8333333

6 * as.numeric(as.character(5/6))
## [1] 5

5/6 == as.numeric(as.character(5/6))
## [1] FALSE

六、R workspace

Where do the variables live?

c 复制代码
ls()
## [1] "approx.pi" "circumference" "diameter"

Getting rid of variables:

c 复制代码
rm("circumference")
ls()
## [1] "approx.pi" "diameter"

rm(list=ls()) # Be warned! This erases everything
ls()
## character(0)
相关推荐
牙痛不能吃糖,哭2 分钟前
C++面试复习日记(8)2025.4.25,malloc,free和new,delete的区别
开发语言·c++
健康的猪6 分钟前
golang的cgo的一点小心得
开发语言·后端·golang
夜夜敲码27 分钟前
C语言教程(十六): C 语言字符串详解
c语言·开发语言
帅云毅29 分钟前
Web3.0的认知补充(去中心化)
笔记·学习·web3·去中心化·区块链
豆豆30 分钟前
day32 学习笔记
图像处理·笔记·opencv·学习·计算机视觉
宋康34 分钟前
C语言结构体和union内存对齐
c语言·开发语言
居然是阿宋44 分钟前
Kotlin高阶函数 vs Lambda表达式:关键区别与协作关系
android·开发语言·kotlin
nenchoumi31191 小时前
VLA 论文精读(十六)FP3: A 3D Foundation Policy for Robotic Manipulation
论文阅读·人工智能·笔记·学习·vln
凉、介1 小时前
PCI 总线学习笔记(五)
android·linux·笔记·学习·pcie·pci
Cao1234567893211 小时前
简易学生成绩管理系统(C语言)
c语言·开发语言