mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-11-23 06:03:07 +03:00
Merge pull request #4342 from Crystal-RainSlide/patch-1
[R/en] Format R code
This commit is contained in:
commit
8f28c8021b
@ -92,8 +92,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)
|
||||
@ -205,8 +206,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
|
||||
@ -216,8 +217,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 "
|
||||
@ -231,6 +231,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
|
||||
@ -256,7 +257,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
|
||||
@ -274,13 +275,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
|
||||
@ -311,8 +308,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
|
||||
@ -406,7 +404,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" "array"
|
||||
class(mat) # "matrix" "array"
|
||||
# Ask for the first row
|
||||
mat[1, ] # 1 4
|
||||
# Perform operation on the first column
|
||||
@ -730,8 +728,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,
|
||||
@ -747,8 +744,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