-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR_basic.Rmd
120 lines (94 loc) · 1.53 KB
/
R_basic.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
---
title: "R basics"
author: "Neil Zhang"
date: "January 15, 2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
R has five main types of objects to store data: vector, factor, multi-dimensional array, data.frame and list.
Data type: character, numeric, logical.
##Vector:
```{r}
c(1:5)
```
```{r}
seq(1,8,2)
seq(0, 1, length.out=3)
rep(0, 5)
```
```{r}
a=c(1,3,2,5,6)
rev(a)
rank(a)
sort(a)
sort(a,decreasing = TRUE)
```
```{r}
a[rank(a)]
```
```{r}
range(a)
```
```{r}
a[1:4]
a[c(1,3,5)]
a[c(-1,-5)] #Remove the first and fifth number
```
##Matrix
```{r}
vector=c(1,2,3,4,5,6)
matrix(vector,2,3)
matrix(vector,2,3,byrow = FALSE)
```
```{r}
m1 = matrix(vector, 2, 3, byrow=TRUE)
colnames(m1)=c('a','b','c')
rownames(m1)=c('x','y')
m1
```
```{r}
apply(m1,1,sum)
apply(m1,2,sum)
```
```{r}
rownames(m1)
```
```{r}
m1=matrix(c(1,2),1,2)
m2=matrix(c(1,2),2,1)
m1
m2
m1 %*% m2 #Matrix multification
```
##Character
```{r}
chr = 'this is a string'
chr1 = "this is a string"
class(chr)
class(chr1)
```
```{r}
toupper(chr)
tolower(chr)
```
```{r}
nchar(chr)
```
```{r}
substr('123456789',1,5)
substring('123456789',5)
```
```{r}
as.numeric("12")
strsplit('2019-10-1','-')
sub('-','/',"2019-10-1") #sub only substitutes the first match
gsub('-','/',"2019-10-1") #gsub substitutes all matches
```
```{r}
grep('a',c('abc','bb','c','a'))
```
```{r}
paste('a','b','c',sep = ' to ')
```