[R/en] Format R code

This commit is contained in:
Crystal-RainSlide 2022-03-08 16:56:31 +08:00 committed by GitHub
parent a53abc23e2
commit 2313888888
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -91,8 +91,9 @@ stem(log(rivers)) # Notice that the data are neither normal nor log-normal!
# 82 | 2 # 82 | 2
# make a histogram: # make a histogram:
hist(rivers, col="#333333", border="white", breaks=25) # play around with these parameters hist(rivers, col = "#333333", border = "white", breaks = 25)
hist(log(rivers), col="#333333", border="white", breaks=25) # you'll do more plotting later hist(log(rivers), col = "#333333", border = "white", breaks = 25)
# play around with these parameters, you'll do more plotting later
# Here's another neat data set that comes pre-loaded. R has tons of these. # Here's another neat data set that comes pre-loaded. R has tons of these.
data(discoveries) data(discoveries)
@ -204,8 +205,8 @@ c(1,2,3) + c(1,2,3) # 2 4 6
# Except for scalars, use caution when performing arithmetic on vectors with # Except for scalars, use caution when performing arithmetic on vectors with
# different lengths. Although it can be done, # different lengths. Although it can be done,
c(1, 2, 3, 1, 2, 3) * c(1, 2) # 1 4 3 2 2 6 c(1, 2, 3, 1, 2, 3) * c(1, 2) # 1 4 3 2 2 6
# Matching lengths is better practice and easier to read # Matching lengths is better practice and easier to read most times
c(1,2,3,1,2,3) * c(1,2,1,2,1,2) c(1, 2, 3, 1, 2, 3) * c(1, 2, 1, 2, 1, 2) # 1 4 3 2 2 6
# CHARACTERS # CHARACTERS
# There's no difference between strings and characters in R # There's no difference between strings and characters in R
@ -215,8 +216,7 @@ class('H') # "character"
# Those were both character vectors of length 1 # Those were both character vectors of length 1
# Here is a longer one: # Here is a longer one:
c('alef', 'bet', 'gimmel', 'dalet', 'he') c('alef', 'bet', 'gimmel', 'dalet', 'he')
# => # => "alef" "bet" "gimmel" "dalet" "he"
# "alef" "bet" "gimmel" "dalet" "he"
length(c("Call","me","Ishmael")) # 3 length(c("Call","me","Ishmael")) # 3
# You can do regex operations on character vectors: # You can do regex operations on character vectors:
substr("Fortuna multis dat nimis, nulli satis.", 9, 15) # "multis " substr("Fortuna multis dat nimis, nulli satis.", 9, 15) # "multis "
@ -230,6 +230,7 @@ month.abb # "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "D
# LOGICALS # LOGICALS
# In R, a "logical" is a boolean # In R, a "logical" is a boolean
class(TRUE) # "logical" class(TRUE) # "logical"
class(FALSE) # "logical" class(FALSE) # "logical"
# Their behavior is normal # Their behavior is normal
@ -255,7 +256,7 @@ c('Z', 'o', 'r', 'r', 'o') == "Z" # TRUE FALSE FALSE FALSE FALSE
# FACTORS # FACTORS
# The factor class is for categorical data # The factor class is for categorical data
# Factors can be ordered (like childrens' grade levels) or unordered (like colors) # Factors can be ordered (like grade levels) or unordered (like colors)
factor(c("blue", "blue", "green", NA, "blue")) factor(c("blue", "blue", "green", NA, "blue"))
# blue blue green <NA> blue # blue blue green <NA> blue
# Levels: blue green # Levels: blue green
@ -273,13 +274,9 @@ levels(infert$education) # "0-5yrs" "6-11yrs" "12+ yrs"
# "NULL" is a weird one; use it to "blank out" a vector # "NULL" is a weird one; use it to "blank out" a vector
class(NULL) # NULL class(NULL) # NULL
parakeet = c("beak", "feathers", "wings", "eyes") parakeet = c("beak", "feathers", "wings", "eyes")
parakeet parakeet # "beak" "feathers" "wings" "eyes"
# =>
# [1] "beak" "feathers" "wings" "eyes"
parakeet <- NULL parakeet <- NULL
parakeet parakeet # NULL
# =>
# NULL
# TYPE COERCION # TYPE COERCION
# Type-coercion is when you force a value to take on a different type # Type-coercion is when you force a value to take on a different type
@ -310,8 +307,9 @@ as.numeric("Bilbo")
# VARIABLES # VARIABLES
# Lots of way to assign stuff: # Lots of way to assign stuff:
x = 5 # this is possible x = 5 # this is possible
y <- "1" # this is preferred y <- "1" # this is preferred traditionally
TRUE -> z # this works but is weird TRUE -> z # this works but is weird
# Refer to the Internet for the behaviors and preferences about them.
# LOOPS # LOOPS
# We've got for loops # We've got for loops
@ -405,7 +403,7 @@ mat
# [2,] 2 5 # [2,] 2 5
# [3,] 3 6 # [3,] 3 6
# Unlike a vector, the class of a matrix is "matrix", no matter what's in it # Unlike a vector, the class of a matrix is "matrix", no matter what's in it
class(mat) # => "matrix" class(mat) # "matrix"
# Ask for the first row # Ask for the first row
mat[1, ] # 1 4 mat[1, ] # 1 4
# Perform operation on the first column # Perform operation on the first column
@ -728,8 +726,7 @@ summary(linearModel)$coefficients[,4] # the p-values
# Logistic regression # Logistic regression
set.seed(1) set.seed(1)
list1$success = rbinom(length(list1$time), 1, .5) # random binary list1$success = rbinom(length(list1$time), 1, .5) # random binary
glModel <- glm(success ~ time, data = list1, glModel <- glm(success ~ time, data = list1, family=binomial(link="logit"))
family=binomial(link="logit"))
glModel # outputs result of logistic regression glModel # outputs result of logistic regression
# => # =>
# Call: glm(formula = success ~ time, # Call: glm(formula = success ~ time,
@ -745,8 +742,10 @@ glModel # outputs result of logistic regression
summary(glModel) # more verbose output from the regression summary(glModel) # more verbose output from the regression
# => # =>
# Call: # Call:
# glm(formula = success ~ time, # glm(
# family = binomial(link = "logit"), data = list1) # formula = success ~ time,
# family = binomial(link = "logit"),
# data = list1)
# Deviance Residuals: # Deviance Residuals:
# Min 1Q Median 3Q Max # Min 1Q Median 3Q Max