There are 2 types of replication:
Vector-wise: repeat the whole vector in the same original order:
rep( value/vector , #time-repeat )
# generate vector score including: 10, 10, 10
score <- rep(10, 3)
score
## [1] 10 10 10
# generate vector weekdays 5 times
weekdays_rep <- rep(weekdays, 5)
weekdays_rep
## [1] "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Monday"
## [7] "Tuesday" "Wednesday" "Thursday" "Friday" "Monday" "Tuesday"
## [13] "Wednesday" "Thursday" "Friday" "Monday" "Tuesday" "Wednesday"
## [19] "Thursday" "Friday" "Monday" "Tuesday" "Wednesday" "Thursday"
## [25] "Friday"
Component-wise: repeat EACH component at a time before moving on the the next component.
rep( value/vector , each= #time-repeat )
# repeat weekdays component-wise 3 times
weekdays_component.wise <- rep(weekdays, each= 3)
weekdays_component.wise
## [1] "Monday" "Monday" "Monday" "Tuesday" "Tuesday" "Tuesday"
## [7] "Wednesday" "Wednesday" "Wednesday" "Thursday" "Thursday" "Thursday"
## [13] "Friday" "Friday" "Friday"
seq( from= , to= , by= )
- `from=1` by DEFAULT or the number you want to start with.
- `to=1` by DEFAULT or the number you want to end INCLUSIVELY.
- `by=1` by DEFAULT or the number you want to step UP/DOWN.
# DEFAULT
seq()
## [1] 1
# from 0 to 9, 2 step up at a time
seq(0, 9, 2)
## [1] 0 2 4 6 8
# from 43 to 34, step down 2.5 at a time
seq(43, 34, -2.5)
## [1] 43.0 40.5 38.0 35.5
SPECIAL CASE: length=
seq( from= , to= , length= )
Note that: we cannot use `length=` and `by=` at the same time.
# from 12 to 35 increasingly and consisting of 5 values
seq(12, 35, length= 5)
## [1] 12.00 17.75 23.50 29.25 35.00
Factors = CATEGORICAL vector containing QuaLitative data (NOMINAL).
Ordered Factors = ranked factors (ORDINAL).
QuaLitative data = values that have NO TRUE NUMERIC
MEANING.
QuaNtitative data = values that have TRUE NUMERIC MEANING.
factor( vector )
If the values are characters, the levels will be listed in an
ALPHABETICAL ORDER.
If the values are numeric, the levels will be listed INCREASINGLY.
weekdays
## [1] "Monday" "Tuesday" "Wednesday" "Thursday" "Friday"
# convert weekdays into a factor
factor(weekdays)
## [1] Monday Tuesday Wednesday Thursday Friday
## Levels: Friday Monday Thursday Tuesday Wednesday
weekdays_rep <- factor(weekdays_rep)
weekdays_rep
## [1] Monday Tuesday Wednesday Thursday Friday Monday Tuesday
## [8] Wednesday Thursday Friday Monday Tuesday Wednesday Thursday
## [15] Friday Monday Tuesday Wednesday Thursday Friday Monday
## [22] Tuesday Wednesday Thursday Friday
## Levels: Friday Monday Thursday Tuesday Wednesday
factor( vector , order= TRUE, levels = c() )
weekdays_component.wise
## [1] "Monday" "Monday" "Monday" "Tuesday" "Tuesday" "Tuesday"
## [7] "Wednesday" "Wednesday" "Wednesday" "Thursday" "Thursday" "Thursday"
## [13] "Friday" "Friday" "Friday"
weekdays_component.wise <- factor(weekdays_component.wise, order = TRUE, levels = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday"))
str(weekdays_rep)
## Factor w/ 5 levels "Friday","Monday",..: 2 4 5 3 1 2 4 5 3 1 ...
str(weekdays_component.wise)
## Ord.factor w/ 5 levels "Monday"<"Tuesday"<..: 1 1 1 2 2 2 3 3 3 4 ...