# To create/see data summary.
skimr::skim(gss_cat)
Name | gss_cat |
Number of rows | 21483 |
Number of columns | 9 |
_______________________ | |
Column type frequency: | |
factor | 6 |
numeric | 3 |
________________________ | |
Group variables | None |
Variable type: factor
skim_variable | n_missing | complete_rate | ordered | n_unique | top_counts |
---|---|---|---|---|---|
marital | 0 | 1 | FALSE | 6 | Mar: 10117, Nev: 5416, Div: 3383, Wid: 1807 |
race | 0 | 1 | FALSE | 3 | Whi: 16395, Bla: 3129, Oth: 1959, Not: 0 |
rincome | 0 | 1 | FALSE | 16 | $25: 7363, Not: 7043, $20: 1283, $10: 1168 |
partyid | 0 | 1 | FALSE | 10 | Ind: 4119, Not: 3690, Str: 3490, Not: 3032 |
relig | 0 | 1 | FALSE | 15 | Pro: 10846, Cat: 5124, Non: 3523, Chr: 689 |
denom | 0 | 1 | FALSE | 30 | Not: 10072, Oth: 2534, No : 1683, Sou: 1536 |
Variable type: numeric
skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
---|---|---|---|---|---|---|---|---|---|---|
year | 0 | 1.00 | 2006.50 | 4.45 | 2000 | 2002 | 2006 | 2010 | 2014 | ▇▃▇▂▆ |
age | 76 | 1.00 | 47.18 | 17.29 | 18 | 33 | 46 | 59 | 89 | ▇▇▇▅▂ |
tvhours | 10146 | 0.53 | 2.98 | 2.59 | 0 | 1 | 2 | 4 | 24 | ▇▂▁▁▁ |
# Two strings are created to show the problem.
x1 <- c("Dec", "Apr", "Jan", "Mar")
x2 <- c("Dec", "Apr", "Jam", "Mar")
# Create a list of the valid levels.
month_levels <- c(
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
# Create factors for both strings.
y1 <- factor(x1, levels = month_levels)
y1
## [1] Dec Apr Jan Mar
## Levels: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
sort(y1)
## [1] Jan Mar Apr Dec
## Levels: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
y2 <- factor(x2, levels = month_levels)
# Typos are automatically converted to NA.
y2
## [1] Dec Apr <NA> Mar
## Levels: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
# Use parse_factor to receive a warning that there is a problem.
y2 <- parse_factor(x2, levels = month_levels)
## Warning: 1 parsing failure.
## row col expected actual
## 3 -- value in level set Jam
# Transform data: Calculate average tv hours by religion.
tvhours_by_relig <- gss_cat %>%
group_by(relig) %>%
summarise(avg_tvhours = mean(tvhours, na.rm = TRUE))
tvhours_by_relig
## # A tibble: 15 × 2
## relig avg_tvhours
## <fct> <dbl>
## 1 No answer 2.72
## 2 Don't know 4.62
## 3 Inter-nondenominational 2.87
## 4 Native american 3.46
## 5 Christian 2.79
## 6 Orthodox-christian 2.42
## 7 Moslem/islam 2.44
## 8 Other eastern 1.67
## 9 Hinduism 1.89
## 10 Buddhism 2.38
## 11 Other 2.73
## 12 None 2.71
## 13 Jewish 2.52
## 14 Catholic 2.96
## 15 Protestant 3.15
# Plot
tvhours_by_relig %>%
ggplot(aes(x = avg_tvhours, y = relig)) +
geom_point()
tvhours_by_relig %>%
ggplot(aes(x = avg_tvhours, y = fct_reorder(.f = relig, .x = avg_tvhours))) +
geom_point() +
# Labeling
labs(y = NULL, x = "Mean Daily TV Hours Watched")
tvhours_by_relig %>%
ggplot(aes(x = avg_tvhours,
y = fct_reorder(.f = relig, .x = avg_tvhours) %>% fct_relevel("Don't know"))) +
geom_point() +
# Labeling
labs(y = NULL, x = "Mean Daily TV Hours Watched")
gss_cat %>% distinct(partyid)
## # A tibble: 10 × 1
## partyid
## <fct>
## 1 Ind,near rep
## 2 Not str republican
## 3 Independent
## 4 Not str democrat
## 5 Strong democrat
## 6 Ind,near dem
## 7 Strong republican
## 8 Other party
## 9 No answer
## 10 Don't know
gss_cat %>% count(partyid)
## # A tibble: 10 × 2
## partyid n
## <fct> <int>
## 1 No answer 154
## 2 Don't know 1
## 3 Other party 393
## 4 Strong republican 2314
## 5 Not str republican 3032
## 6 Ind,near rep 1791
## 7 Independent 4119
## 8 Ind,near dem 2499
## 9 Not str democrat 3690
## 10 Strong democrat 3490
gss_cat %>% distinct(race)
## # A tibble: 3 × 1
## race
## <fct>
## 1 White
## 2 Black
## 3 Other
# Recode
gss_cat %>%
# Rename levels
mutate(race_rev = fct_recode(race, "POC" = "Black")) %>%
select(race, race_rev) %>%
filter(race == "Black")
## # A tibble: 3,129 × 2
## race race_rev
## <fct> <fct>
## 1 Black POC
## 2 Black POC
## 3 Black POC
## 4 Black POC
## 5 Black POC
## 6 Black POC
## 7 Black POC
## 8 Black POC
## 9 Black POC
## 10 Black POC
## # ℹ 3,119 more rows
# Collapse multiple levels into one.
gss_cat %>%
mutate(race_col = fct_collapse(race, "Minority" = c("Black", "Other"))) %>%
select(race, race_col) %>%
filter(race != "White")
## # A tibble: 5,088 × 2
## race race_col
## <fct> <fct>
## 1 Black Minority
## 2 Black Minority
## 3 Black Minority
## 4 Other Minority
## 5 Black Minority
## 6 Other Minority
## 7 Black Minority
## 8 Other Minority
## 9 Black Minority
## 10 Black Minority
## # ℹ 5,078 more rows
# Lump small levels into other levels.
gss_cat %>% count(race)
## # A tibble: 3 × 2
## race n
## <fct> <int>
## 1 Other 1959
## 2 Black 3129
## 3 White 16395
gss_cat %>% mutate(race_lump = fct_lump(race)) %>% distinct(race_lump)
## # A tibble: 2 × 1
## race_lump
## <fct>
## 1 White
## 2 Other
# To see class
"2024-06-19" %>% ymd() %>% class()
## [1] "Date"
# From Strings
"2024-06-19" %>% ymd()
## [1] "2024-06-19"
"2024/06/19" %>% ymd()
## [1] "2024-06-19"
# From Numbers
20240619 %>% ymd()
## [1] "2024-06-19"
# Time - POSIXct and POSIXt are also names for a date/time object.
"2024-06-19 07-18-51" %>% ymd_hms()
## [1] "2024-06-19 07:18:51 UTC"
flights %>%
select(year:day, hour, minute) %>%
mutate(departure = make_datetime(year = year, month = month, day = day, hour = hour, min = minute))
## # A tibble: 336,776 × 6
## year month day hour minute departure
## <int> <int> <int> <dbl> <dbl> <dttm>
## 1 2013 1 1 5 15 2013-01-01 05:15:00
## 2 2013 1 1 5 29 2013-01-01 05:29:00
## 3 2013 1 1 5 40 2013-01-01 05:40:00
## 4 2013 1 1 5 45 2013-01-01 05:45:00
## 5 2013 1 1 6 0 2013-01-01 06:00:00
## 6 2013 1 1 5 58 2013-01-01 05:58:00
## 7 2013 1 1 6 0 2013-01-01 06:00:00
## 8 2013 1 1 6 0 2013-01-01 06:00:00
## 9 2013 1 1 6 0 2013-01-01 06:00:00
## 10 2013 1 1 6 0 2013-01-01 06:00:00
## # ℹ 336,766 more rows
# From date to date-time
as_datetime(today())
## [1] "2024-06-19 UTC"
today() %>% as_datetime()
## [1] "2024-06-19 UTC"
# From date-time to date
now() %>% as_date()
## [1] "2024-06-19"
make_datetime_100 <- function(year, month, day, time) {make_datetime(year, month, day, time %/% 100, time %% 100)}
flights_dt <- flights %>%
filter(!is.na(dep_time), !is.na(arr_time)) %>% mutate(
dep_time = make_datetime_100(year, month, day, dep_time),
arr_time = make_datetime_100(year, month, day, arr_time),
sched_dep_time = make_datetime_100(year, month, day, sched_dep_time),
sched_arr_time = make_datetime_100(year, month, day, sched_arr_time)) %>%
select(origin, dest, ends_with("delay"), ends_with("time"))
flights_dt
## # A tibble: 328,063 × 9
## origin dest dep_delay arr_delay dep_time sched_dep_time
## <chr> <chr> <dbl> <dbl> <dttm> <dttm>
## 1 EWR IAH 2 11 2013-01-01 05:17:00 2013-01-01 05:15:00
## 2 LGA IAH 4 20 2013-01-01 05:33:00 2013-01-01 05:29:00
## 3 JFK MIA 2 33 2013-01-01 05:42:00 2013-01-01 05:40:00
## 4 JFK BQN -1 -18 2013-01-01 05:44:00 2013-01-01 05:45:00
## 5 LGA ATL -6 -25 2013-01-01 05:54:00 2013-01-01 06:00:00
## 6 EWR ORD -4 12 2013-01-01 05:54:00 2013-01-01 05:58:00
## 7 EWR FLL -5 19 2013-01-01 05:55:00 2013-01-01 06:00:00
## 8 LGA IAD -3 -14 2013-01-01 05:57:00 2013-01-01 06:00:00
## 9 JFK MCO -3 -8 2013-01-01 05:57:00 2013-01-01 06:00:00
## 10 LGA ORD -2 8 2013-01-01 05:58:00 2013-01-01 06:00:00
## # ℹ 328,053 more rows
## # ℹ 3 more variables: arr_time <dttm>, sched_arr_time <dttm>, air_time <dbl>
datetime <- ymd_hms ("2024-06-19 07:55:22")
year(datetime)
## [1] 2024
month(datetime)
## [1] 6
# Day of the month
mday(datetime)
## [1] 19
# Day of the year
yday(datetime)
## [1] 171
# Day of the week
wday(datetime)
## [1] 4
# To return abbreviated names.
month(datetime, label = TRUE)
## [1] Jun
## 12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
# To return full names
month(datetime, label = TRUE, abbr = FALSE)
## [1] June
## 12 Levels: January < February < March < April < May < June < ... < December
flights_dt %>%
count(week = floor_date(dep_time, "week")) %>% ggplot(aes(week, n)) +
geom_line()
(datetime <- ymd_hms("2024-06-19 08:21:56"))
## [1] "2024-06-19 08:21:56 UTC"
year(datetime) <- 2024
datetime
## [1] "2024-06-19 08:21:56 UTC"
month(datetime) <- 05
datetime
## [1] "2024-05-19 08:21:56 UTC"
hour(datetime) <- hour(datetime) + 1
datetime
## [1] "2024-05-19 09:21:56 UTC"
# To set multiple values at once.
update(datetime, year = 2024, month = 06, mday = 19, hour = 16)
## [1] "2024-06-19 16:21:56 UTC"
flights_dt %>%
mutate(dep_hour = update(dep_time, yday = 1)) %>% ggplot(aes(dep_hour)) +
geom_freqpoly(binwidth = 300)
# To find age
h_age <- today() - ymd(20100511)
h_age
## Time difference of 5153 days
as.duration(h_age)
## [1] "445219200s (~14.11 years)"
# To add years
dyears(1) + dweeks(12) + dhours(15)
## [1] "38869200s (~1.23 years)"
# To multiply years
2 * dyears(1)
## [1] "63115200s (~2 years)"
# To add and subtract durations to and from days
tomorrow <- today() + ddays(1)
last_year <- today() - dyears(1)
one_pm <- ymd_hms("2024-06-19 17:00:00", tz = "America/New_York")
one_pm
## [1] "2024-06-19 17:00:00 EDT"
one_pm + days(1)
## [1] "2024-06-20 17:00:00 EDT"
# To multiply periods
10 * (months(6) + days(1))
## [1] "60m 10d 0H 0M 0S"
# To add periods
days(50) + hours(25) + minutes(2)
## [1] "50d 25H 2M 0S"
flights_dt %>%
filter(arr_time < dep_time)
## # A tibble: 10,633 × 9
## origin dest dep_delay arr_delay dep_time sched_dep_time
## <chr> <chr> <dbl> <dbl> <dttm> <dttm>
## 1 EWR BQN 9 -4 2013-01-01 19:29:00 2013-01-01 19:20:00
## 2 JFK DFW 59 NA 2013-01-01 19:39:00 2013-01-01 18:40:00
## 3 EWR TPA -2 9 2013-01-01 20:58:00 2013-01-01 21:00:00
## 4 EWR SJU -6 -12 2013-01-01 21:02:00 2013-01-01 21:08:00
## 5 EWR SFO 11 -14 2013-01-01 21:08:00 2013-01-01 20:57:00
## 6 LGA FLL -10 -2 2013-01-01 21:20:00 2013-01-01 21:30:00
## 7 EWR MCO 41 43 2013-01-01 21:21:00 2013-01-01 20:40:00
## 8 JFK LAX -7 -24 2013-01-01 21:28:00 2013-01-01 21:35:00
## 9 EWR FLL 49 28 2013-01-01 21:34:00 2013-01-01 20:45:00
## 10 EWR FLL -9 -14 2013-01-01 21:36:00 2013-01-01 21:45:00
## # ℹ 10,623 more rows
## # ℹ 3 more variables: arr_time <dttm>, sched_arr_time <dttm>, air_time <dbl>
flights_dt <- flights_dt %>%
mutate(overnight = arr_time < dep_time, arr_time = arr_time + days(overnight * 1), sched_arr_time = sched_arr_time + days(overnight * 1))
flights_dt %>%
filter(overnight, arr_time < dep_time)
## # A tibble: 0 × 10
## # ℹ 10 variables: origin <chr>, dest <chr>, dep_delay <dbl>, arr_delay <dbl>,
## # dep_time <dttm>, sched_dep_time <dttm>, arr_time <dttm>,
## # sched_arr_time <dttm>, air_time <dbl>, overnight <lgl>
# To find out how many days in a year. This answer is different than the one in the text because this is 2024.
next_year <- today() + years(1)
(today() %--% next_year) / ddays(1)
## [1] 365
# To find the current time zone.
Sys.timezone()
## [1] "America/New_York"
# To find out the time difference in another time zone.
(x1 <- ymd_hms("2015-06-19 12:00:00", tz = "America/New_York"))
## [1] "2015-06-19 12:00:00 EDT"
(x2 <- ymd_hms("2015-06-19 18:00:00", tz = "Europe/Dublin"))
## [1] "2015-06-19 18:00:00 IST"
x1 - x2
## Time difference of -1 hours
x2 - x1
## Time difference of 1 hours