# a random vector containing 1, 3, 4, 6, 9
random <- c(1, 3, 4, 6, 9)
random
## [1] 1 3 4 6 9
Insert new value to the random vector: 2 and 10
# R can overwrite
random <- c(random, 2, 10)
random
## [1] 1 3 4 6 9 2 10
length() - check for the number of components within a variable.
# check for the number of components within vector random.
length(random)
## [1] 7
sum() - take the sum of all of the components/value within that vector.
# find the sum of ALL values within random.
# 1 + 3 + 4 + 6 + 9 + 2 + 10
1 + 3 + 4 + 6 + 9 + 2 + 10
## [1] 35
sum(random)
## [1] 35
# square EACH component within the vector
random^2
## [1] 1 9 16 36 81 4 100
# sum of the squared number
sum(random^2)
## [1] 247
# random2 containing 3, 2, 5, 1, 3, 4 , 8
random2 <- c(3, 2, 5, 1, 3, 4, 8)
random2
## [1] 3 2 5 1 3 4 8
random + random2
## [1] 4 5 9 7 12 6 18
var_name2. VALID
var_name% INVALID
2var_name INVALID
var name INVALID
var.name VALID
.2var_name INVALID
_var_name INVALID
rep() -replicate a value/vector
VECTORWISE REP: rep( vector/value , #time-repeated )
COMPONENT WISE REP: rep( vector/value , each= )
# fruit containing lemon, strawberry, peach.
fruit <- c("lemon", "strawberry", "peach")
fruit
## [1] "lemon" "strawberry" "peach"
# repeat vector fruit 3 times (vector wise)
rep(fruit, 3)
## [1] "lemon" "strawberry" "peach" "lemon" "strawberry"
## [6] "peach" "lemon" "strawberry" "peach"
# repeat vector fruit component-wise 3 times
rep(fruit, each=3)
## [1] "lemon" "lemon" "lemon" "strawberry" "strawberry"
## [6] "strawberry" "peach" "peach" "peach"
sort() - sort numeric number in order.
ASCENDING/INCREASING sort( vector , decreasing = FALSE )
decreasing = FALSE is default --> so we can ignore it if we sort increasing.
DESCENDING/DECREASING sort( vector , decreasing = TRUE )
# sort random from small - larger value (Ascending)
random <- sort(random)
random
## [1] 1 2 3 4 6 9 10
# sort random2 from large to small (Descending)
sort(random2, decreasing= T)
## [1] 8 5 4 3 3 2 1
mean() - find mean for the set of score
mean(random2)
## [1] 3.714286
round() - round to a specified decimal places.
round( value , #places )
# round 7.123456 to 2 decimal places
round( 7.123456 , 2)
## [1] 7.12
# round the result of the mean for random2 to 1 decimal place
round( mean(random2) , 1 )
## [1] 3.7
unique() - display the unique value within a vector.
unique(random2)
## [1] 3 2 5 1 4 8
# gender: F, F, M , F, M, M, M,F, F ,F
gender <- c("F", "F", "M" , "F", "M", "M", "M", "F", "F" ,"F")
gender
## [1] "F" "F" "M" "F" "M" "M" "M" "F" "F" "F"
length(gender)
## [1] 10
unique(gender)
## [1] "F" "M"
typeof(gender)
## [1] "character"
Factor - is a QUALITIVE vector, meaning that it caries CATEGORICAL values. In other words, factor is a special case of vector where we want to specify that the values within it DO NOT have mathematical meaning.
For eg.: gender, fruit.
factor() - QUALITATIVE + UNranked vector.
factor( vector , labels = c( "name1" , "name2" , ... ) )
ordered() - QUALITATIVE + RANKED vector.
Example: A waitress records her drink order as a set of value
: 3, 4, 2, 2, 3, 4, 4, 2, 4, 3, 4, 2.
Based on the price of each type of drink: 2 is for lemonade, 3
is for Sprite, 4 is for Redbull.
# generating vector `drink` containing the raw data.
drink <- c(3, 4, 2, 2, 3, 4, 4, 2, 4, 3, 4, 2)
# factor `drink`
drink <- factor(drink)
# labeling the factor
drink <- factor(drink, labels = c("Lemon" , "Sprite" , "Redbull" ) )
drink
## [1] Sprite Redbull Lemon Lemon Sprite Redbull Redbull Lemon Redbull
## [10] Sprite Redbull Lemon
## Levels: Lemon Sprite Redbull
str() - structure of the object.
str(drink)
## Factor w/ 3 levels "Lemon","Sprite",..: 2 3 1 1 2 3 3 1 3 2 ...
# generating vector `Drink` containing the raw data.
Drink <- c(3, 4, 2, 2, 3, 4, 4, 2, 4, 3, 4, 2)
# convert the vector to a ranked factor
Drink <- ordered(Drink)
# labeling the levels
Drink <- ordered(Drink, labels = c("Lemon" , "Sprite" , "Redbull" ))
Drink
## [1] Sprite Redbull Lemon Lemon Sprite Redbull Redbull Lemon Redbull
## [10] Sprite Redbull Lemon
## Levels: Lemon < Sprite < Redbull
# checking on structure
str(Drink)
## Ord.factor w/ 3 levels "Lemon"<"Sprite"<..: 2 3 1 1 2 3 3 1 3 2 ...