Advanced R study notes - Chapter 1: Programming Basics

Let a novel coronavirus was going to go ahead writing his thesis & internship school students in Wuhan indefinitely stay at home, doing nothing really can not stand every day on their own, or learn something about fulfilling life, determined to take advantage of Valentine's Day a good start learning boyfriend is not fragrant it , no matter, today is the implementation of a program of study auspicious.

Why R?

In fact, a long time ago, probably freshman sophomore year, on the last summer course I learned a little R, but the language this kind of thing, do not use can only remember a ballpark. From the current situation, learn learn precision R, for me, it is still very necessary. After all, in the school after the interview, the interviewer asked me if I would not have such R, so it is still to learn about the system is restarted.

Why Advanced R?

Before learning process is off to see the "real language R" or R language study notes directly to people's need to complete the job of the R language. Last look at a blog that this book pretty good, just another English version, in the process of thinking about science R but also the way to learn English, so this is not more than slightly ~ As for "R practical language." better, here to do the evaluation Oh, look I was not able to finish it ... learn

Well, I did not talk much, start learning!

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)

Because there are many contributor R, for subsequent versions of the same holding herein checkpoint. But I also waited a long time to run the R error, so I did not use this.


Objects of -Types
* the Logical Objects Boolean value
TRUE. 1 =
FALSE = 0

TRUE
FLASE

*intergers 整数
capital L suffix

42L

* Double double precision real

1.5

*Complex numbers 复数
with i suffix

2+3i

* Nominal Nominal Scale-Level Data Data
stored via the character alss and designated with quotation marks:

"a" ## character

* Missing values defaults

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])

* The Vector vector

c(1,2,3)

* Scalar Scalar

c(1)

* The Matrix 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)

The results data.frame and data.table here is different:
Here Insert Picture Description


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

x <- 5
y = 3 

Using the function check at:
Here Insert Picture Description
As can be seen here, the <- assignment is a vector, and = real number assignment is obtained.

*index 索引
R starts indexing at 1.

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

* The subsetting subsetted

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.
Here Insert Picture Description
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"]

Each row and each column can also have a separate name, this name can be used to follow-up values:

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

In the data tables, the value and the rows take the same value but take the time column may not require quotes:

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

Needed when a plurality of values ​​can be named () operator:

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

If you want to use quotation marks value, to add option: with = FALSE:

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

Of course, I tried it without this option, and there is no error. And with is.matrix (), is.vector () and is.charactor () examined, the results are all FALSE.

This section also talked about behind "[" "]" is actually a function, but it did not name only bracket wreck, do not deserve the name
also describes some other way value is really an eye-opener

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

But I feel the value of this, or get your way just fine, do not do anything too fancy values ​​also others ...

* The Inherits ()
function is to supplement is.class () function:

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

The result is a Boolean value, TRUE or FALSE.

* Coercion cast
cast at risk, need to be cautious.

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

As output, it is 3, the real number to an integer cast takes only the integer part.

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")

Output is "NA"

as.logical(3)

The output is TRUE, all real numbers other than zero, the output of TRUE

as.logical(0)

The output is FALSE

as.numeric("a")

Output is "NA", and containing a warning, a warning because the default value occurs cast


-Mathematical Operators and Functions
*comparison 比大小

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

Note here that the "=" is an assignment, "==" is more equality.

Since different programming floating decimal display different environments, so we can define the accuracy (tolerance):

all.equal(1.0002, tolerance = .01)

* Logic Operators Logical Operators
& for the AND
| for OR

TRUE | FALSE
TRUE & FALSE

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

If you want to output a result, you can use "&&" "||":
Here Insert Picture Description

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

* Mathematical Operators Mathematical Operators

Operators meaning
+ plus
- Less
* Multiply
/ except
^ power
%/% Juniper
%% Take the remainder
%*% Matrix multiplication

There are a number of functions:

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

It can be used ? Arithmetic to query some of the mathematical operators.

Trigonometric functions:

cos(3.1415)
sin(3.1415)

It can be used ? Trig to query trigonometric functions.

Matrix Operations:

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

Chapter 1 ends happily friends ~ ~

Published an original article · won praise 0 · Views 12

Guess you like

Origin blog.csdn.net/weixin_43054092/article/details/104313960