# load data.table library
library(data.table)
# clean session
rm(list=ls())
# Please include the following line of code as well
set.seed(0)
# create vectors from which to build d
V1 <- rep(month.abb[1:8], each=2)
V2 <- rep(letters[1:8], 2)
V3 <- rnorm(length(V1))
# create a data.table named d
d <- data.table(month=V1, event=V2, obs=V3)
ans_1a <- d[, .N]
ans_1b <- d[month=='Jan']
ans_1c <- d[month=='Feb' | event=='c']
ans_1d <- d[month=='Feb' & event=='c']
ans_1e <- d[month %in% c('Jan', 'Mar', 'May', 'Jul')]
ans_2a <- d[, month]
ans_2b <- d[, .(month)]
ans_2c <- d[, .(month, event)]
ans_2d <- d[, .(MONTH=month, EVENT=event)]
ans_2e <- d[, mean(obs)]
ans_2f <- d[, .(obs_mean=mean(obs), obs_sd=sd(obs))]
ans_3a <- d[, .(obs_mean=mean(obs)), .(event)]
ans_3b <- d[, .(obs_mean=mean(obs)), .(event, month)]
ans_4a <- d[1:8, .(obs_median=median(obs)), .(month, event)]
d5a = data.table(d)
d5a_copy = copy(d5a)
d5a_copy[, new_zeros := 0]
ans_5a <- d5a_copy
d5b = data.table(d)
d5b_copy = copy(d5b)
d5b_copy[, obs := NULL]
ans_5b <- d5b_copy
ans_5c <- 'option 3'
ans_5d <- 'option 1'
# 6
s2 <- "ind2 obs
Apr -0.15579551
Apr -1.47075238
May -0.47815006
May 0.41794156
Jun 1.35867955
Jun -0.10278773
Jul 0.38767161
Jul -0.05380504
Aug -1.37705956"
DT <- data.table(s2)
# 7
# long
# wide
# melt
# dcast
# 8
ans_8a <- fread('https://crossley.github.io/book_stats/data/criterion_learning/crit_learn.csv')
ans_8b <- ans_8a[cnd=="Delay" | cnd=="Long ITI"]
ans_8c <- ans_8b[, .(cnd, sub, t2c)]
ans_8d <- ans_8c[, .(t2c_mean = mean(t2c)), .(cnd, sub)]