Advanced R 学习笔记 - Chapter 1: Programming Basics

新型冠状病毒让一个本来要提前去学校实习&写毕业论文的武汉大学生无限期地留守在家,实在受不了每天无所事事的自己了,还是学点什么充实一下生活,趁着情人节下定决心要开始好好学习男朋友不香吗 ,不管,今天就是一个执行学习计划的黄道吉日。

Why R?

其实很早之前,大概大一大二的时候,就上暑期课程学过一点R,但语言这种东西,不常使用就只能记个大概。从现在的形势来看,学好学精R,对我来说还是很有必要的。毕竟参加了学校的面试以后,面试官都有问我会不会R这样,所以还是重新开始系统地学习一下。

Why Advanced R?

之前的学习过程都是断断续续看《R语言实战》或者直接找别人的R语言学习笔记来完成需要R语言的作业的。上次看一篇博客说这本书还挺好的,刚好又是英文版,想着学R的过程中还能顺便学学英语,所以就这本咯~至于是不是比《R语言实战》更好,这里先不做评价噢,先看看我是不是能学完吧…

好了,话不多说,开始学习!

Introduction

Chapters 1-6: advanced programming techniques

Chapters 7-10: data management measures

Chapters 11-14: cloud computing with R (Amazon cloud)

Chapter 15: techniques in dynamic documents and reports

Chapter 1: Programming Basics

-Advanced R Software Choices
R and RStudio
Java


-Reproducing Results

install.pacackages("checkpoint")
library(checkpoint)
checkpoint("2020-02-14", R.version = "3.4.4")
library(data.table)

因为R的contributor有很多,为了后续保持版本相同这里使用checkpoint。但是我的R运行了老半天还报错,所以我没有用这个。


-Types of Objects
*Logical objects 布尔值
TRUE = 1
FALSE = 0

TRUE
FLASE

*intergers 整数
capital L suffix

42L

*double 双精度实数

1.5

*Complex numbers 复数
with i suffix

2+3i

*Nominal-level data 名义尺度数据
stored via the character alss and designated with quotation marks:

"a" ## character

*Missing values 缺省值

NA
NA_integer_
NA_real_
NA_character_
NA_complex_

*Factors 因子
A factor variable indicates that a variale should be treated discretely. Factors are stored as integers, with labels to indicate the original value.

factor(1:3)
factor(c("a", "b", "c"))
factor(letters[1:3])

*vector 向量

c(1,2,3)

*scalar 标量

c(1)

*matrix 矩阵

matrix(c(1:6), nrow=3, ncol=2)

*list 列表
A list is a vector of objects, in which each element of the list may be a different type.

list(
	  c("a"),
	  c(1, 2, 3),
	  matrix(c(1:6), nrow=3, ncol=2)
)

*data frame 数据框
Data frame is a particular type of list, in which each element of the list is identical in length.

data.frame(1:3, 4:6)
data.frame(1:3, letters[1:3])

*data table 数据表
Data tables are similar to data frames, but are designed to be more memory eficient and faster.

library(data.table)
data.table(1:3, 4:6)

这里data.frame和data.table的结果是不同的:
在这里插入图片描述


-Base Operators and Functions
*assignment 赋值
= and <- are both assignment operators, but for clarity’s sake, <- is recommended for general assignment.

x <- 5
y = 3 

使用函数check一下:
在这里插入图片描述
从这里可以看到,使用<-赋值得到的是向量,而=赋值得到的是实数。

*index 索引
R starts indexing at 1.

x <- c("a", "b", "c")
x[1]

*subsetting 取子集

x2 <- matrix(c(1:6), nrow=3, ncol = 2)
x2[1, 2]          ## row 1, column 2
x2[1, ]           ## all row 1
x2[, 1]           ## all column 1
x2[c(1,2), ]      ## all rows 1 and 2

x[-2]             ## drop element two
x2[, -2]          ## drop column two
x2[-1, ]          ## drop row 1

*accessing 取值
A single index in a single bracket returns the entire element at that spot. Using double brackets returns the object within that element of the list.
在这里插入图片描述
Named data frames and lists can use the $ operator to access components.

x3 <- data.frame(A = 1:3, B = 4:6)
y2 <- list(C = c("a"), D = c(1, 2, 3))

x3$A
y2$C
x3[["A"]]
y2[["C"]]

x3[1, "A"]
x3[, "A"]

每行和每列也可以有单独的命名,后续可以用这个命名来取值:

rownames(x3) <- c("first", "second", "third")
x3["second", "B"]

在data tables中,取行值与上述一样,但是取列值的时候可以不需要引号:

x4 <- data.table(A = 1:3, B = 4:6)
x4[1, ]
x4[, A]

需要用多个命名取值的时候可以用 .()操作符:

x4[1:2, .(A, B)]

如果要使用引号取值时,要加上option:with = FALSE:

x4[1, "A", with = FALSE]

当然,我试了一下不加这个option,并没有报错。而且用is.matrix(), is.vector()以及is.charactor()检验了一下,结果全都是FALSE。

这一段后面还讲了 “[” “]” 其实也是函数,只不过没有名字而已bracket好惨,不配拥有姓名
还介绍了一些其他的取值方式,真是令人大开眼界

'['(x, 1)
'['(x3, "second", "A")

但我感觉取值这样,还是按照自己的习惯就好了,搞太多花式取值也难为别人…

*inherits()
这个函数就是为了补充is.class()函数的:

inherits(x3, "data.frame")
inherits(x2, "matrix")

结果就是布尔值,TRUE或者FALSE。

*coercion 强制转换
强制转换有风险,使用需谨慎。

as.integer(3.8)
as.integer(3.2)

输出结果一样,都是3,实数强制转换为整数时只取整数部分。

as.character(3)
as.numeric(3)
as.complex(3)
as.factor(3)
as.matrix(3)
as.data.frame(3)
as.list(3)
as.logical("a")

输出结果为“NA”

as.logical(3)

输出结果为TRUE,除0外所有实数,输出结果均为TRUE

as.logical(0)

输出结果为FALSE

as.numeric("a")

输出结果为“NA”,并且含有warning,警告因为强制转换出现缺省值


-Mathematical Operators and Functions
*comparison 比大小

4 > 4
4 >= 4
4 < 4
4 == 4
4 != 4

这里注意 “=” 是赋值, “==”是比较是否相等。

由于不同编程环境的浮动小数显示不同,所以我们可以限定精度(tolerance):

all.equal(1.0002, tolerance = .01)

*logic operators 逻辑运算符
& for AND
| for OR

TRUE | FALSE
TRUE & FALSE

c(TRUE, TRUE) | c(TRUE, FALSE)
c(TRUE, TRUE) & c(TRUE, FALSE)

如果只想要一个输出结果,可以用 “&&” “||”:
在这里插入图片描述

any(c(TRUE, TRUE, FALSE))
all(c(TRUE, FALSE, TRUE))

*mathematical operators 数学运算符

运算符 含义
+
-
*
/
^ 乘方
%/% 取商
%% 取余数
%*% 矩阵相乘

还有一些函数:

sqrt(3)
abs(-3)
exp(1)
log(2.71)

可以用 ?Arithmetic 来查询一些数学运算符。

三角函数:

cos(3.1415)
sin(3.1415)

可以用 ?Trig 来查询三角函数。

矩阵运算:

t(x2)                   ##将x2转置
crossprod(x2)           ##内积
tcrossprod(x2)          ##外积

Chapter 1愉快地结束啦~~

发布了1 篇原创文章 · 获赞 0 · 访问量 12

猜你喜欢

转载自blog.csdn.net/weixin_43054092/article/details/104313960