mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
[R/en] Format R code
This commit is contained in:
parent
a53abc23e2
commit
2313888888
@ -91,8 +91,9 @@ stem(log(rivers)) # Notice that the data are neither normal nor log-normal!
|
||||
# 82 | 2
|
||||
|
||||
# make a histogram:
|
||||
hist(rivers, col="#333333", border="white", breaks=25) # play around with these parameters
|
||||
hist(log(rivers), col="#333333", border="white", breaks=25) # you'll do more plotting later
|
||||
hist(rivers, col = "#333333", border = "white", breaks = 25)
|
||||
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.
|
||||
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
|
||||
# different lengths. Although it can be done,
|
||||
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
|
||||
c(1,2,3,1,2,3) * c(1,2,1,2,1,2)
|
||||
# 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) # 1 4 3 2 2 6
|
||||
|
||||
# CHARACTERS
|
||||
# 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
|
||||
# Here is a longer one:
|
||||
c('alef', 'bet', 'gimmel', 'dalet', 'he')
|
||||
# =>
|
||||
# "alef" "bet" "gimmel" "dalet" "he"
|
||||
# => "alef" "bet" "gimmel" "dalet" "he"
|
||||
length(c("Call","me","Ishmael")) # 3
|
||||
# You can do regex operations on character vectors:
|
||||
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
|
||||
# In R, a "logical" is a boolean
|
||||
|
||||
class(TRUE) # "logical"
|
||||
class(FALSE) # "logical"
|
||||
# Their behavior is normal
|
||||
@ -255,7 +256,7 @@ c('Z', 'o', 'r', 'r', 'o') == "Z" # TRUE FALSE FALSE FALSE FALSE
|
||||
|
||||
# FACTORS
|
||||
# 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"))
|
||||
# blue blue green <NA> blue
|
||||
# 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
|
||||
class(NULL) # NULL
|
||||
parakeet = c("beak", "feathers", "wings", "eyes")
|
||||
parakeet
|
||||
# =>
|
||||
# [1] "beak" "feathers" "wings" "eyes"
|
||||
parakeet # "beak" "feathers" "wings" "eyes"
|
||||
parakeet <- NULL
|
||||
parakeet
|
||||
# =>
|
||||
# NULL
|
||||
parakeet # NULL
|
||||
|
||||
# TYPE COERCION
|
||||
# Type-coercion is when you force a value to take on a different type
|
||||
@ -310,8 +307,9 @@ as.numeric("Bilbo")
|
||||
# VARIABLES
|
||||
# Lots of way to assign stuff:
|
||||
x = 5 # this is possible
|
||||
y <- "1" # this is preferred
|
||||
y <- "1" # this is preferred traditionally
|
||||
TRUE -> z # this works but is weird
|
||||
# Refer to the Internet for the behaviors and preferences about them.
|
||||
|
||||
# LOOPS
|
||||
# We've got for loops
|
||||
@ -405,7 +403,7 @@ mat
|
||||
# [2,] 2 5
|
||||
# [3,] 3 6
|
||||
# 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
|
||||
mat[1, ] # 1 4
|
||||
# Perform operation on the first column
|
||||
@ -728,8 +726,7 @@ summary(linearModel)$coefficients[,4] # the p-values
|
||||
# Logistic regression
|
||||
set.seed(1)
|
||||
list1$success = rbinom(length(list1$time), 1, .5) # random binary
|
||||
glModel <- glm(success ~ time, data = list1,
|
||||
family=binomial(link="logit"))
|
||||
glModel <- glm(success ~ time, data = list1, family=binomial(link="logit"))
|
||||
glModel # outputs result of logistic regression
|
||||
# =>
|
||||
# Call: glm(formula = success ~ time,
|
||||
@ -745,8 +742,10 @@ glModel # outputs result of logistic regression
|
||||
summary(glModel) # more verbose output from the regression
|
||||
# =>
|
||||
# Call:
|
||||
# glm(formula = success ~ time,
|
||||
# family = binomial(link = "logit"), data = list1)
|
||||
# glm(
|
||||
# formula = success ~ time,
|
||||
# family = binomial(link = "logit"),
|
||||
# data = list1)
|
||||
|
||||
# Deviance Residuals:
|
||||
# Min 1Q Median 3Q Max
|
||||
|
Loading…
Reference in New Issue
Block a user