按R中的任务年份汇总调查数据

时间:2017-03-31 15:56:19

标签: r

我正在尝试将调查数据(分析单位是受访者)汇总到边际:每年每个问题的答复百分比。有21个问题(专栏)。这是我尝试过的,但它没有得到所需的输出。

#Place each year into a list element
yr.list = by(data=data, INDICES = data$year, FUN = list)
summary(yr.list)

#Within each element, place responses to all questions into one vector
data.form = function(x){
  n = nrow(x)
  k = ncol(x) - 2
  id = rep(1:n, times = k)
  item = sort(rep(1:k, times=n))
  y = c(unlist(x[,3:ncol(x)]))
  out = data.frame("year"=x$year, "id"=id, "item"=item, "y"=y)
  return(out)
}

yr.list = lapply(X = yr.list, FUN = data.form)

yr.vector = do.call(rbind, yr.list)
yr.vector$occurrences = 1
yr.aggregated = aggregate(occurrences ~ year + item, data = yr.vector, FUN =       sum)

summary(yr.aggregated)

0 个答案:

没有答案