R Workshop 2024 in UTokyo
UTokyo & Tsinghua University
PhD Candidate in Political Science, Tsinghua University
Visiting Researcher at ISS
Research interests: Political Psychology, Natural Language Processing
Dissertation: How and why Hong Kong uses CCP discourse, and its empirical impact
R → Your Phone
R Studio/VS Code → iOS/Android
R packages → Apps
R Basic → iMessage
packages from other source → Line
function → Message Sending
object → Contact
:::
IDEs (Integrated Development Environments)
and … VS Code/pycharm/Positron
subset()
, merge()
, apply()
t.test()
, lm()
, summary()
plot()
, hist()
, boxplot()
if
, for
, while
filter()
, select()
, mutate()
, summarize()
ggplot()
, geom_point()
, geom_line()
gather()
, spread()
, separate()
, unite()
shinyApp()
, fluidPage()
, server()
mean()
, sum()
, length()
function_name <- function(arg1, arg2) { ... }
my_function
function(arg1, arg2)
{ ... }
return()
return(result)
Hot Potato(“ばくだんゲーム”; “击鼓传花”) but one by one version
<-
Assignment operator, the shorthand for the assign()
command
Syntax: <
variable name
><-
<object
>
<-
?Alt + -
option + -
1stday
)..
and _
(Error: M&M
).X != x
) ! means "not"/"no",
!=` means “not equal to”.list <- c(1:5)
).Hot Keyboard Time!
Hot Keyboard Time!
###Data Types That Can Be Read Directly
.RDS
(single object).RData
(multiple objects).txt
.csv
Syntax: <
name
><-
<read command
>(<data path
>)
Call the package through library
or require
, and then use the commands in it.
# SPSS, Stata, SAS
library(haven)
df_spss <- read_spss("<FileName>.sav")
df_stata <- read_dta("<FileName>.dta")
df_sas <- read_sas("<FileName>.sas7bdat")
# Quick Import of Forms
library(reader)
df_csv <- read.csv("<FileName>.csv")
df_table <- read.table("<FileName>.csv/txt")
# Excel
library(readxl)
df_excel <- read_excel("<FileName>.xls")
df_excel2 <- read_excel("<FileName>.xlsx")
# JSON (JavaScript Object Notation)
library(rjson)
df_json <- fromJSON(file = "<FileName>.json" )
# XML/Html
library(xml)
df_xml <- xmlTreeParse("<url>")
df_html <- readHTMLTable(url, which=3)
Hot Keyboard Time!
The Swiss Army Knife of data reading:rio
Syntax:
( , file = )
load("/Users/adrian/Documents/Yufei_Sun/THU/projects/slides/course/rworkshop_in_UTokyo/wvs7.rda")
wvs7
nrow(wvs7) # Get the number of rows in the data
ncol(wvs7) # Get the number of columns in the data
names(wvs7) # Get the variable/column names
str(wvs7) # Get variable names, types, number of rows and columns
Attributes are attributes of all types of variables.
length(wvs7$age) # Get the length of the variable (here it is the number of rows)
unique(wvs7$age) # Get the unique values of the variable
summary(wvs7$age) # Get all the above information about the year
class(wvs7$age) # Check the structure of the year: vector, matrix, array, dataframe, list
typeof(wvs7$age) # Check the type of the year elements
install.packages()
: Installing packageslibrary()
or require()
: Loading packageshelp()
or ?
: Accessing function documentationreadRDS()
, read.table()
, read.csv()
: Reading various file formatssaveRDS()
, save()
, write.csv()
: Saving in different formatsrio::import()
: The “Swiss Army Knife” for data readingnrow()
, ncol()
: Get number of rows and columns names(): Get variable/column namesstr()
: Get variable names, types, number of rows and columnsdataframe[, "variable"]
or dataframe$variable
: Extracting variablestable()
, summary()
: Examining variable characteristicslevels()
: Understanding factor variableslength()
, unique()
, class()
, typeof()
Github Page: https://github.com/syfyufei
Email: sunyf20@mails.tsinghua.edu.cn
Personal Website:https://syfyufei.github.io/
SUN Yufei(Adrian)