#=====Task 1=====# obs <- c("f7", "f8", "m1", "m2", "m3", "f3", "m4", "f1", "m7", "m7", "f4", "m5", "f5", "m6","f6", "m8", "m9", "f9", "m10", "f10", "f2") split.obs = function(obs){ chars <- as.factor(substr(obs, 1, 1)) ints <- as.numeric(substr(obs, 2, 10)) levels(chars) <- c("female", "male") df <- data.frame(ints = ints, chars = chars) df } split.obs(obs) #====Task 2====# load("ragged.rda") #How many people have 1,2,3.. obs obs.nums <- table(table(ragged$id)) obs.nums #Create new variable that numbers the obs for each person #max of 16 obs - make factors visit <- c("1st", "2nd", "3rd", paste(4:16, "th", sep = "")) ragged <- ragged[order(ragged$id),] vis <- table(ragged$id) make.vis <- function(times){ visit[1:times] } ragged$visit <- unlist(sapply(table(ragged$id), make.vis)) #Given the name of a variable create a new one showing the value it ahd at a previous time #====Task 3====# x <- 1:5 reduce = function(x, op){ paste(x, op) do.call(op) } cum <- x[] reduce(x, "+") n <- length(x) expression(cat(paste(x, op))