Week 1 R basics
1.1 Quarantine log
day1_activity = c("breakfast",
"exam prep",
"online course",
"seminar",
"running",
"lunch",
"check email",
"TV series")
day1_is_work = c(FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE)
day1_minutues = c(30,240,120,60,20,45,30)
n = length(day1_activity)
day1_total_hours = sum(day1_minutues)/60
day1_work_hours = sum(day1_minutues[day1_is_work]) / 60
cat("Total time recorded for day 1 : ", day1_total_hours, "hours, over", n, "activities\n")
## Total time recorded for day 1 : 9.083333 hours, over 8 activities
cat("Total time working for day 1 : ", day1_work_hours, "hours \n\n")
## Total time working for day 1 : 7.5 hours
daily_levels = c("connect",
"work",
"workout",
"essential",
"fun",
"hobby")
## day1_classes = factor(
## c("essential","work","work","work","workout","essential","connect","fun"),
## levels = daily_levels
##)
daily_map = c("breakfast" = "essential",
"exam prep" = "work",
"online course" = "work",
"seminar" = "work",
"running" = "workout",
"walk" = "workout",
"lunch" = "essential",
"check email" = "connect",
"TV series" = "fun",
"music" = "hobby",
"R learning" = "hobby",
"lab meeting" = "work"
)
day1_classes = factor(daily_map[day1_activity], levels = daily_levels)
print(day1_classes)
## breakfast exam prep online course seminar running
## essential work work work workout
## lunch check email TV series
## essential connect fun
## Levels: connect work workout essential fun hobby
day1_dates = rep("04-19-2020",length(day1_activity))
day1_dates = as.Date(day1_dates, format = "%m-%d-%y")
1.2 other useful info
Some Useful Vector Operations
- length(): number of elements
- sum(): sum of all element values
- unique(): distinct values
- sort(): sort elements, omitting NAs
- order(): indices of sorted elements, NAs are last
- rev(): reverse the order
- summary(): simple statistics
Handling missing data
tail()
and head()
working with variables
activity <- c("check e-mail", "breakfast", "conference call", "webinar", "walk")
activity[2]
## [1] "breakfast"
index <- c(2, 4)
activity[index]
## [1] "breakfast" "webinar"
is_work <- c(TRUE, FALSE, TRUE, TRUE, FALSE)
activity[is_work]
## [1] "check e-mail" "conference call" "webinar"
minutes <- c(10,30,60,60,30)
work_minutes <- minutes[is_work]
sum(work_minutes)
## [1] 130
!is_work
## [1] FALSE TRUE FALSE FALSE TRUE
non_work_minutes <- minutes[!is_work]
sum(non_work_minutes)
## [1] 60
nchar(activity)
## [1] 12 9 15 7 4
toupper(activity)
## [1] "CHECK E-MAIL" "BREAKFAST" "CONFERENCE CALL" "WEBINAR"
## [5] "WALK"
%in%
a binary operator
- is each of the vector elements on the left-hand side in the set of elements on the right hand side