# R language learning notes

Learning notes are mainly written for your own use. If you have any mistakes, please point them out

# 01 data type

## Declare variables

Unlike python, variables can take names with English

```a<-100
score.Tony<-99 # No mistake.
```

## Type of assignment operator

Common ones are < -, =, - >

```1+1;2+3;6^2 # There are multiple commands in a line, separated by
# Assignment Operators
x<-100
x
x=200
x
300->x
x
```

Super assignment operators < -, - > >

```x<<-400
500->>x # The latter two methods are super assignment operations, and can access variables outside the function
cities<- c('Beijing','Shanghai','Guangzhou','Shenzhen') # Create vector
ls() # See all assigned variable names
rm(a);remove(x) # Two ways to clear variables, rm and remove, are the same
```

Note: the vector subscript of R language starts from 1

```cities[1]# Print out 'Beijing'
mode(cities);typeof(cities)
```

mode(x);typeof(x)
First display mode, large type value; second, small type double precision

```x<-c(10,32,53,14,5)
mode(x);typeof(x)
x<-as.integer(x) # Convert x to int instead of double
length(x) # Length of vector
nchar(cities) # Length of each string in a vector
sentences='Mother said:"Wash your hands and eat!"'
print(sentences,quote='F')
# Remove the double quotation marks on both sides when printing, F is FALSE
flags<-c(TRUE,FALSE,F,F,T)
mode(flags)
weight<-cities=='Beijing' # Compared with each other in cities to get logical type vector
```

## Two ways to create a complex type

1. Direct declaration
2. Using the complex() function
```com1=2+3i;com2=complex(real = 2,imaginary = 5)
com=c(com1,com2)
Re(com) # Get the real part of all complex numbers
Im(com) # Get the imaginary parts of all complex numbers
Conj(com1) # Calculating conjugate complex of complex
```

## factor() converts a vector to a factor type

Integer representation of classified variables to speed up processing

```sex<-factor(c('Male','Male','Female','Male','Female'))
sex # Without double quotation marks, it essentially takes Female as 1 and Male as 2
#Print results:
'''
> sex
[1] Male   Male   Female Male   Female
Levels: Female Male
'''
```

## View the basic properties of an object

```class(sex);mode(sex);typeof(sex);str(sex)
# View the object's large class, internal small class, small class and data structure respectively
#Print results:
'''
> class(sex);mode(sex);typeof(sex);str(sex)
[1] "factor"
[1] "numeric"
[1] "integer"
Factor w/ 2 levels "Female","Male": 2 2 1 2 1
'''
dim(x)# Return NULL of one dimension
x<- matrix(1:12,nrow = 3)
x
dim(x)# Two dimensional return 3 4
```

## Functions to distinguish and transform data types:

```a<-c('100','251','13','46')
a<-as.numeric(a)# Convert to double type
a<-as.integer(a)# Convert to integer type
```

## Special variables in R language:

variable Meaning
Inf Infinity; 1/0=Inf
NaN Uncertain; 0/0=NaN
NA Missing values
NULL Empty object

# 02 operator

## Arithmetic operation:

• Basic:
Add, subtract, multiply, divide, multiply + - * /^
• Especial:
Divisibility:% /%
Remainder:%%
Matrix multiplication:% *%
In R language, + and so on are actually operator functions
````+`(1,5)
''' Output 6'''
`/`(6,2)
''' Output 3'''
```

## matrix multiplication

```c(1:6)*c(2,2,2,2,2,2)
'''
> c(1:6)*c(2,2,2,2,2,2)
[1]  2  4  6  8 10 12
'''
a2<-c(1:6)%*%c(2,2,2,2,2,2)
a2
'''
> a2
[,1]
[1,]   42
'''
```

## Cyclic complement

If two vectors of different length are used for operation, R language will automatically complement the short vectors to adapt to the operation

```# 5 is not a multiple of 2, so warning
> c(1,2,3)+c(10,10,10,10,10)
[1] 11 12 13 11 12
'''Warning message:
In c(1, 2, 3) + c(10, 10, 10, 10, 10) :
longer object length is not a multiple of shorter object length'''

> c(1,2,3)+c(10,10,10,10,10,10)
[1] 11 12 13 11 12 13
'''
```

### Logical operators

Greater than >; less than >;
Greater than or equal to > =; less than or equal to<=
Equal to = =; not equal to=

Or and not: | &!

### Logic function

1. all() to determine whether all elements are true
2. any() determines whether at least one element is true
3. which() gets which subscripts in the logical vector are true
```> all(c(T,T,T,F,T,T))
[1] FALSE
> any(c(T,T,T,F,T,T))
[1] TRUE
> which(c(T,T,T,F,T,T))
[1] 1 2 3 5 6
```

### Judge missing data

```# Judge missing data
score=c(66,88,76,95,NA,65,82)
> is.na(score)
[1] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE

> any(is.na(score))
[1] TRUE

```

Remove missing values

```score<-score[which(!is.na(score))]
```

# 03 generate regular data

Be careful!!!
1:n-1 is not a vector of 1 to n-1, but a vector of 1 to n minus 1

## Arithmetic progression

### seq() function

parameter Meaning
from Start value of sequence
to Termination value
by step
length.out Total length of sequence
along.with Consistent with the length of another vector
```> seq(from=2,to=20,by=4)
[1]  2  6 10 14 18
> seq(from=0,to=20,length.out = 6)
[1]  0  4  8 12 16 20
```

### rep() function

parameter Meaning
times Represents the number of repetitions of vector x
each Represents the number of occurrences of each element
length.out Length of the generated vector after repetition
```> rep(1:4,times=2,each=3)#2 for 1-4 overall repeat twice
[1] 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4
> rep(1:4,length.out=10)
[1] 1 2 3 4 1 2 3 4 1 2
```

### Evenly distributed random number runif()

parameter Meaning
n Random number (observation data)
min Minimum (lower limit)
max Maximum (upper limit)
```> runif(10,0,1)
[1] 0.57219133 0.75797086 0.63737904 0.08635545 0.83004433 0.37391578
[7] 0.12021769 0.48147331 0.59559635 0.36853139
> runif(5,10,20)
[1] 15.55731 14.62614 19.34402 19.77089 16.20681
> x<-runif(2000,1,10)
> plot(x)
```

### rnorm()

parameter Meaning
n Random number
mean mean value
sd standard deviation
```score<-rnorm(1000,85,6)
any(score>100)
score<-pmin(score,100)#Take 100 for all scores larger than 100
```

### sample() sampling function

parameter Meaning
x Sampled vector
size Number of data generated
replace Allow duplicate sampling
prob Take the probability of each individual
```> sample(0:100,10) # Simplest sampling
[1] 15 28 35 71 59 75 13 94 80 14
> sample(1:5,20,replace = T) # Sampling times > overall
[1] 5 1 1 2 2 3 4 4 5 2 4 4 3 3 5 2 1 4 4 5
> sex<-sample(c('Male','Female'),200,replace = T, prob = c(0.3,0.7))
> #Probability of given extraction
> table(sex)#Conduct statistics
sex
Female   Male
141     59
```

### Connect to string function paste()

paste() default sep = 'space‘
paste0() default sep = '

```> students<- paste('Student',1:10)
> students
[1] "Student 1"  "Student 2"  "Student 3"  "Student 4"  "Student 5"  "Student 6"  "Student 7"
[8] "Student 8"  "Student 9"  "Student 10"

> A<- paste('A',1:10,sep = '-')
> A
[1] "A-1"  "A-2"  "A-3"  "A-4"  "A-5"  "A-6"  "A-7"  "A-8"  "A-9"  "A-10"

> paste(c('A','B','C'),100:108,'@qq.com',sep = '')
[1] "A100@qq.com" "B101@qq.com" "C102@qq.com" "A103@qq.com" "B104@qq.com"
[6] "C105@qq.com" "A106@qq.com" "B107@qq.com" "C108@qq.com"

> paste0(c('A','B','C'),100:108,'@163.com')
[1] "A100@163.com" "B101@163.com" "C102@163.com" "A103@163.com"
[5] "B104@163.com" "C105@163.com" "A106@163.com" "B107@163.com"
[9] "C108@163.com"
```
Published 2 original articles, praised 0 and visited 13

Tags: R Language less Python

Posted on Wed, 11 Mar 2020 08:15:43 -0400 by GimbaL