【发布时间】:2017-11-04 17:50:54
【问题描述】:
以下部分代码对我来说很好用。 Assessments_jobSmart 是一个导入的 csv 文件。我正在尝试重塑多列的数据,但我无法通过使用 tapply 来实现。所以我尝试通过匹配user_id来绑定矩阵,并添加id不存在的行id。
individual_assess <- filter(assessments_jobSmart, !is.na(assessments_jobSmart$submitted))
quiz_i = filter(individual_assess, assessment_type == 'quiz')
checkin_i = filter(individual_assess, assessment_type == 'checkin')
groupQuiz_i <- group_by(quiz_i, user_id, program_id, name)
summaryQuiz_i <- summarize(groupQuiz_i, maxQuiz = max(moderated_score))
q1 <- with(summaryQuiz_i, tapply(maxQuiz, list(user_id, name) , I))
q2=matrix(NA, nrow = nrow(q1), ncol = ncol(q1)+1)
colnames(q2) = c("user_id", paste("assess_q", colnames(q1)))
q2[1:nrow(q2),1] = rownames(q1)
q2[1:nrow(q2),2:ncol(q2)] = q1[1:nrow(q1),1:ncol(q1)]
lla = merge(lla, q2, by = 'user_id', all=TRUE)
但是对于下一部分,它在最后一行的末尾给了我错误。我参考了很多链接,但仍然不知道为什么。 c1 和 c2 的行数相同。
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names =
TRUE, : arguments imply differing number of rows: 1, 2
错误代码:
groupCheckin_i <- group_by(checkin_i, user_id, program_id, name)
summaryCheckin_i <- summarize(groupCheckin_i, countCheckin = n())
c1 <- with(summaryCheckin_i, tapply(countCheckin, list(user_id, name) , I))
c1[c1=="NULL"]=NA
c2=matrix(NA, nrow = nrow(c1), ncol = ncol(c1)+1)
colnames(c2) = c("user_id", paste("assess_c", colnames(c1)))
c2[1:nrow(c2),1] = rownames(c1)
c2[1:nrow(c2),2:ncol(c2)] = c1[1:nrow(c1),1:ncol(c1)]
lla = merge(lla, c2, by = 'user_id', all=TRUE)
可重复的示例,不确定我是否正确复制它。我将从导入、分组和汇总的数据框开始。
install.packages("dplyr")
install.packages("reshape2")
install.packages('ggplot2', dep = TRUE)
library("dplyr")
library(reshape2)
library(ggplot2)
head = c("user_id", "program", "assessment", "type", "marks")
content = c("111", "program A", "quiz 1", "quiz", "1", "112", "program A", "quiz 1", "quiz", "0.5", "112", "program A", "quiz 2", "quiz", "0.75", "113", "program B", "quiz 2", "quiz", "0.8", "110", "program B", "survey 1", "survey", "1", "113", "program B", "survey 1", "survey", "1")
M = as.dataframe(matrix(content, nrow=5, ncol=5)) #kinda replicate my imported csv file.
s = filter(M, type == 'survey')
q = filter(M, type == 'quiz')
groupS = group_by(s, user_id, program, assessment)
groupQ = group_by(q, user_id, program, assessment)
summaryS <- summarize(groupS, maxMarks = max(marks)) # take only maximum marks if there are duplicate entries
s1 <- with(summaryS, tapply(maxMarks, list(user_id, assessment) , I))
s2 = matrix(NA, nrow=nrow(s1), ncol=ncol(s1)+1)
colnames(s2) = c("user_id", colnames(s1))
s2[q:nrow(s2), 1] = rownames(s1) # everything works alright till here
s2[1:nrow(s2),2:ncol(s2)] = s1[1:nrow(s1),1:nrow(s1)]
summaryQ <- summarize(groupQ, count = n()) # it makes more sense to count survey done
q1 <- with(summaryQ, tapply(count, list(user_id, assessment) , I))
q2 = matrix(NA, nrow=nrow(q1), ncol=ncol(q1)+1)
colnames(q2) = c("user_id", colnames(q1))
q2[q:nrow(q2), 1] = rownames(q1) # everything works alright till here
q2[1:nrow(q2),2:ncol(q2)] = q1[1:nrow(q1),1:nrow(q1)] #q2 becomes a list :(
b = merge(b, a2, by = 'user_id', all+TRUE)
# | program | assessment | marks
# ---------------------------------------
# 111 | program A | quiz 1 | 1
# 112 | program A | quiz 1 | 0.5
# 112 | program A | quiz 2 | 0.75
# 113 | program B | quiz 2 | 0.8
# Then I used tapply to reshape data to get something like:
# | assessment 1 | assessment 2
# -------------------------------------
# 111 | 1 | NA
# 112 | 0.5 | 0.75
# 113 | NA | 0.8
这样的表有很多,因为提取出来的结果不同,所以想在最后合并一下,把所有的结果合并起来。我不希望 user_id 出现两次,每次用于单独的表。我想将结果与 user_id 进行比较,但缺少该列的标题,因为 user_id 被视为行名。所以我创建了一个更大的矩阵来复制所有内容并包含 user_id 列名:
# Let's say a1 is the matrix after tapply, a2 is the new dataframe I want to create, b is the successful new dataframe created using the exact same method on same csv file exported.
我想在合并后得到这样的东西:
user_id | survey 1 | survey 2 | assessment 1 | assessment 2
-------------------------------------------------------------
110 | 1 | NA | NA | NA
111 | NA | NA | 1 | NA
112 | NA | NA | 0.5 | 0.75
113 | 1 | NA | NA | 0.8
【问题讨论】:
-
如果您可以添加reproducible example,人们会更容易提供帮助。
-
我已经添加了示例。请让我知道这是否足够
-
很高兴看到您添加的示例,但人们无法轻松地将其加载到 R 中以制定解决方案。您应该查看我之前提供的链接中的示例,并尝试使用
dput()例如。 -
完成。但不确定它们是否与我导入的 csv 文件足够接近