Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以R为单位按任务年汇总调查数据_R - Fatal编程技术网

以R为单位按任务年汇总调查数据

以R为单位按任务年汇总调查数据,r,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 vecto

我试图将调查数据(分析单位为受访者)汇总成边缘:每年每个问题的回答百分比。共有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)

你能更准确地说“没有得到期望的输出”吗?这段代码生成每个问题年的回答总数;它不会因响应而中断频率。我本来应该一开始就提到这一点,但回答是明确的。请添加一个供人们使用的。