R语言入门
Step into the R world
1 Preface
1.1 What is R
- R is a language and environment for statistical computing and graphics.
- R is similar to the S language and environment.
- R is available as Free Software under the terms of the Free Software Foundation’s GNU General Public License in source code form.
- R Website, https://www.r-project.org/, R packages, https://cloud.r-project.org/
- Comprehensive R Archive Network, https://cran.r-project.org/mirrors.html
1.2 What Is RStudio
- RStudio is an integrated development environment (IDE) for R and Python.
- It includes a console, syntax-highlighting editor that supports direct code execution, and tools for plotting, history, debugging, and workspace management.
- RStudio is available in open source and commercial editions and runs on the desktop (Windows, Mac, and Linux).
- RStudio website, https://posit.co/products/open-source/rstudio/
1.3 Install R and RStudio
1.4 Usage of R and RStudio
- Edit multi-line codes in the Source panel.
- Run codes in the Source panel from top to end line by line.
- Check variables in the Environment panel.
- Check print in the Console panel.
1.5 How to Learn R
- Documentation
- Package page (e.g., https://cloud.r-project.org/web/packages/Keng/index.html, https://lavaan.ugent.be/)
- Manuals (e.g., https://cloud.r-project.org/web/packages/Keng/Keng.pdf)
- Vignettes (e.g., https://cloud.r-project.org/web/packages/Keng/vignettes/PRE.html)
- Books, Papers, Videos
2 Value and Type
2.1 Double (real, or decimal)
2.2 Integer
2.3 Character
2.4 Factor
# c() function combines Values into a Vector
x <- c("grade1", "grade2", "grade3")
x
## [1] "grade1" "grade2" "grade3"
x <- factor(x)
x
## [1] grade1 grade2 grade3
## Levels: grade1 grade2 grade3
levels(x)
## [1] "grade1" "grade2" "grade3"
labels(x)
## [1] "1" "2" "3"
as.numeric(x)
## [1] 1 2 3
x <- ordered(x)
x
## [1] grade1 grade2 grade3
## Levels: grade1 < grade2 < grade3
2.5 Date
2.6 Logical
2.7 Complex
2.8 Special values
3 Symbols, signs, and operators
3.1 #
Comment
3.2 :
Generate regular sequences (interaction)
3.3 ;
Separate multiple command in one line,
3.4 <-
Assign
3.5 $
, []
,[[]]
subset, extract
See below.
3.6 -
Negative numbers
3.7 Logic operator
De Morgan’s law of logical operation:
3.8 Arithmetic Operators
4 Variable and Vector
- A vector is similar to a variable.
c()
combines its arguments to form a vector.
4.1 Subset a vector
5 List
5.1 Subset the list
6 Data frame
dat_data.frame <- data.frame(id, gender, depression)
head(dat_data.frame)
## id gender depression
## 1 101 0 1
## 2 102 1 4
## 3 103 0 3
## 4 104 1 2
## 5 105 0 4
## 6 106 1 3
str(dat_data.frame)
## 'data.frame': 10 obs. of 3 variables:
## $ id : num 101 102 103 104 105 106 107 108 109 110
## $ gender : int 0 1 0 1 0 1 0 1 0 1
## $ depression: int 1 4 3 2 4 3 1 3 4 2
View the whole data set:
6.1 rownames and colnames
rownames(dat_data.frame)
## [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
colnames(dat_data.frame)
## [1] "id" "gender" "depression"
(colnames(dat_data.frame) <- c("id", "x", "y"))
## [1] "id" "x" "y"
colnames(dat_data.frame)[2] <- "gender"
colnames(dat_data.frame)[3] <- "depression"
colnames(dat_data.frame)
## [1] "id" "gender" "depression"
6.2 Subset the data.frame
# return a vector
dat_data.frame$gender
## [1] 0 1 0 1 0 1 0 1 0 1
# return a data.frame
dat_data.frame[2]
## gender
## 1 0
## 2 1
## 3 0
## 4 1
## 5 0
## 6 1
## 7 0
## 8 1
## 9 0
## 10 1
# return a vector
dat_data.frame[,2]
## [1] 0 1 0 1 0 1 0 1 0 1
# return a data.frame
dat_data.frame[2,]
## id gender depression
## 2 102 1 4
dat_data.frame[2,2]
## [1] 1
dat_data.frame[1:3, c(1,3)]
## id depression
## 1 101 1
## 2 102 4
## 3 103 3
dat_data.frame[1, "depression"]
## [1] 1
dat_data.frame["1", "depression"]
## [1] 1
dat_data.frame[c(2,4), "depression"]
## [1] 4 2
dat_data.frame[c("2","4"), "depression"]
## [1] 4 2
dat_data.frame[dat_data.frame$gender == 0,]
## id gender depression
## 1 101 0 1
## 3 103 0 3
## 5 105 0 4
## 7 107 0 1
## 9 109 0 4
dat_data.frame[dat_data.frame$depression > 2,]
## id gender depression
## 2 102 1 4
## 3 103 0 3
## 5 105 0 4
## 6 106 1 3
## 8 108 1 3
## 9 109 0 4
7 Matrix
dat_byrow <- c(101, 1, 2, 102, 0, 5, 103, 1, 4, 104, 1, 1, 105, 0, 3,
106, 0, 5, 107, 0, 2, 108, 1, 4, 109, 0, 2, 110, 1, 2)
mat <- matrix(dat_byrow, byrow = TRUE, nrow = 10,
dimnames = list(0:9, c("id", "gender", "depression")))
mat
## id gender depression
## 0 101 1 2
## 1 102 0 5
## 2 103 1 4
## 3 104 1 1
## 4 105 0 3
## 5 106 0 5
## 6 107 0 2
## 7 108 1 4
## 8 109 0 2
## 9 110 1 2
8 Function
8.1 What is a function
- A function is a reusable piece of code designed to perform a specific task.
- A function is like a cooker: Input your ingredient into a function, then the function process them, and finally outputs delicious food.
8.2 Use a function
Search the documents of the function and learn.
8.3 Use a function: An example
- function name:
seq
. - arguments:
from
,to
,by
,length.out
,along.with
,...
. - default arguments:
from = 1
,length.out = NULL
.
8.4 Use a function: An example
- Input: Input your data (numbers, text, vectors, data frames, etc.) through arguments (parameters).
- Processing: It performs a series of operations using the input arguments.
- Output: It returns a result (output). This could be a single value, a vector, a list, a plot, or even nothing (though functions usually do something useful!).
8.5 Two ways of passing arguments
- Through argument names
seq(from = 1, to = 7, by = 0.1)
## [1] 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8
## [20] 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7
## [39] 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6
## [58] 6.7 6.8 6.9 7.0
- Through argument position
8.6 Use default arguments
- You can omit the default arguments.
- Pass other arguments through argument names.
8.7 Common fucntions
8.7.1 Type Checking Functions
8.7.2 Type Conversion Functions
9 Package
9.1 What Are R Packages
Basically, an R package is a collection of functions.
9.2 Install Packages
Official version
Developing version
- The 1st way of using a function
- The 2nd way of using a function
10 Working directory
- Working directory is the file folder R works (read and write files).
getwd()
gets the current working directory,setwd()
sets the working directory.
- The way R and Windows write file path differs, R uses “/”, but Windows uses “\”.
- You could use RStudio’s file panel to :
- Go To Working Directory
- Set As Working Directory
10.1 Advice on the working directory
- Use
getwd()
or RStudio to get and go to R’s working directory. - Create a new folder (directory) named with your name and school ID under this directory (e.g.,
qingyaozhang2024321
). - Go into the
qingyaozhang2024321
file folder. - Use
setwd()
or RStudio to set the new folderqingyaozhang2024321
as your working directory.
11 Import and Export Data
11.1 Use base R
- Export the data using
write.csv()
.
- Import the
.csv
data usingread.csv()
- Step 1, Enter the data into Excel or SPSS.
- Step 2, Save the data as the
.csv
file. - Step 3, Import the data into R using
read.csv()
.
read .csv file from R’s working directory:
read .csv file from a directory other than R’s working directory, e.g.,
11.2 Other file formats and packages
- .xlsx file,
readxl
package,read_excel()
,read_xls()
,read_xlsx()
- .sav file,
haven
package,read_spss()
,read_sav()
,write_sav()
- .dta file,
haven
package,read_stata()
,read_dta()
,write_dta()
12 Recommended Packages
install.packages(pkgs = c(
"afex", # 方差分析
"lavaan","blavaan","semTools","simsem", # 潜变量分析
"BUCSS","car", # 回归分析样本量计划
"effectsize", # 效应量
"emmeans", # 估计边缘平均数,事后比较
"tidyverse","haven","readxl", # 数据读取
"interactions", # 简单斜率分析
"jtools", # 回归分析的结果报告
"Keng", # 回归分析的效应量PRE
"lme4","lmerTest","nlme", # 混合效应模型
"lubridate", # 时间变量的处理
"metafor","metaSEM", # 元分析
"MASS", # Modern Applied Statistics with S
"MBESS", # 信度及其置信区间
"pwr", # 统计检验力
"SBSDiff", # 似然比检验
"jiebaR", # 文本挖掘
"jiebaRD", # 文本挖掘
"text2vec", # 文本挖掘
"tmcn", # 文本挖掘
"wordcloud2", # 文本挖掘
"htmlwidgets", # 文本挖掘
"rmarkdown", # R文档
"kableExtra" # R文档
))
13 Recommended Resources
- R for Data Science, https://r4ds.hadley.nz/
ggplot2
package, https://ggplot2-book.org/psych
package, https://personality-project.org/r/psych/afex
package, https://afex.singmann.science/lavaan
package, https://lavaan.ugent.be/
That’s all. Thanks.