<- (a.k.a. “gets”)a <- 4a. What is that object? It’s a number. Specifically, it’s the number 4. We know that because we took that 4 and we shoved it into a.b <- sqrt(16)+10a## [1] 4
b## [1] 14
3## [1] 3
a+b## [1] 18
#What does a look like if we take the square root of it?
sqrt(a)## [1] 2
#What does it look like if we add 1 to it?
a + 1## [1] 5
#If we look at it, do we see a number?
is.numeric(a)## [1] TRUE
#We looked at what a looked like with 1 added, but a itself wasn't changed
a## [1] 4
#Let's save a+1 as something else
b <- a + 1
#And let's overwrite a with its square root
a <- sqrt(a)
a## [1] 2
b## [1] 5
a, b was already set using the old value of a, and so was still 4+1=5, not 2+1=3.a basically got reassigned with <-. That’s how we got it to be 2a was a number. But what else could it be? What other kinds of variables are there?'hello'TRUE or FALSE (or T or F)'left handed', 'right handed', or 'ambidextrous''' or ""address <- '321 Fake St.'
address## [1] "321 Fake St."
is.character(address)## [1] TRUE
c <- TRUE
is.logical(c)## [1] TRUE
is.character(a)## [1] FALSE
is.logical(is.numeric(a))## [1] TRUE
a > 100## [1] FALSE
a > 100 | b == 5## [1] TRUE
& is AND, | is OR, and to check equality use ==, not =. >= is greater than OR equal to, similarly for <=TRUE=1 and FALSE=0 which comes in handy.as functions to change one object type to another (although for T=1, F=0 it does it automatically)as.numeric(FALSE)## [1] 0
TRUE + 3## [1] 4
is.logical(is.numeric(FALSE))
is.numeric(2) + is.character('hello')
is.numeric(2) & is.character(3)
TRUE | FALSE
TRUE & FALSEe <- as.factor('left-handed')
levels(e) <- c('left-handed','right-handed','ambidextrous')
e## [1] left-handed
## Levels: left-handed right-handed ambidextrous
c() (concatenate) to put a bunch of objects of the same type together in a vectord <- c(5,6,7,8)
c(is.numeric(d),is.vector(d))## [1] TRUE TRUE
d[2]## [1] 6
mean(d)## [1] 6.5
c(sum(d),sd(d),prod(d))## [1] 26.000000 1.290994 1680.000000
d + 1## [1] 6 7 8 9
d + d## [1] 10 12 14 16
d > 6## [1] FALSE FALSE TRUE TRUE
continents <- as.factor(c('Asia','Asia','Asia',
'N America','Europe','Africa','Africa'))
table(continents)## continents
## Africa Asia Europe N America
## 2 3 1 1
continents[4]## [1] N America
## Levels: Africa Asia Europe N America
3 %in% c(3,4)## [1] TRUE
c('Nick','James') %in% c('James','Andy','Sarah')## [1] FALSE TRUE
1:8
rep(4,3)
rep(c('a','b'),4)
numeric(5)
character(6)
sample(1:20,3)
sample(c("Heads","Tails"),6,replace=TRUE)## [1] 1 2 3 4 5 6 7 8
## [1] 4 4 4
## [1] "a" "b" "a" "b" "a" "b" "a" "b"
## [1] 0 0 0 0 0
## [1] "" "" "" "" "" ""
## [1] 9 7 15
## [1] "Heads" "Tails" "Tails" "Tails" "Tails" "Heads"
f <- c(2,3,4,5), then what will the output of these be?f^2
f + c(1,2,3,4)
c(f,6)
is.numeric(f)
mean(f >= 4)
f*c(1,2,3)
length(f)
length(rep(1:4,3))
f/2 == 2 | f < 3
as.character(f)
f[1]+f[4]
c(f,f,f,f)
f[f[1]]
f[c(1,3)]
f %in% (1:4*2)'Male' or 'Female' people.paste0() to turn c('a','b') into 'ab'[] to turn h <- c(10,9,8,7) into c(7,8,10,9) and call it jfloor() or %% to count how many multiples of 4 there are between 433 and 899