R 数据框重新设计:将2行合并到一行中-并按值重命名

R 数据框重新设计:将2行合并到一行中-并按值重命名,r,R,我是R的新手。为了找到这个问题的优雅答案,我做了大量的研究和测试。我尝试了重塑、t、熔化等。我也在为变量的名称而挣扎。 我被这样的数据框困住了。我们有一个时间在问题1之前提问,然后在第二行我们有一个时间记录了答案 Time Logs 446.6204 Question1 452.7516 4 452.7516 Question2 458.1999 3 458.1999 Question3

我是R的新手。为了找到这个问题的优雅答案,我做了大量的研究和测试。我尝试了重塑、t、熔化等。我也在为变量的名称而挣扎。 我被这样的数据框困住了。我们有一个时间在问题1之前提问,然后在第二行我们有一个时间记录了答案

    Time            Logs
    446.6204    Question1
    452.7516    4
    452.7516    Question2
    458.1999    3
    458.1999    Question3
    460.2342    5
我希望所有内容都在一行中,并在日志中使用值命名变量。幸运的是,我的模式是恒定的,所以与切片工作可以是好的

Respondent TimeQ1   Question1   TimeA1  TimeQ2  Question2   TimeA2  TimeQ3  Question3   TimeA3
Respondent1 446.6204    4   452.7516    452.7516    3   458.1999    458.1999    5   460.2342

谢谢你的帮助

我为受访者添加了一列,并为多个受访者添加了数据。以下是示例数据集:

DF <- structure(list(Respondent = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Respondent 1", 
"Respondent 2", "Respondent 3"), class = "factor"), Time = c(446.6204, 
452.7516, 452.7516, 458.1999, 458.1999, 460.2342, 535.94448, 
543.30192, 543.30192, 549.83988, 549.83988, 552.28104, 443.2204, 
449.3516, 449.3516, 454.7999, 454.7999, 456.8342), Logs = structure(c(6L, 
4L, 7L, 3L, 8L, 5L, 6L, 5L, 7L, 2L, 8L, 3L, 6L, 1L, 7L, 4L, 8L, 
5L), .Label = c("1", "2", "3", "4", "5", "Question1", "Question2", 
"Question3"), class = "factor")), .Names = c("Respondent", "Time", 
"Logs"), row.names = c(NA, -18L), class = "data.frame")

如果您真的想要,您必须修改列顺序,但一般来说这应该可以做到

欢迎来到SO!这不是代码生成服务。到目前为止,您尝试过哪些R代码,哪些不起作用?谢谢您的回答mrp。我将继续致力于一个解决方案,将把一切放在一行。你看,我必须为每个答辩人写一行。我认为这可能会起作用。再次感谢-@MartinTavarez每个受访者回答的问题都相同吗?是的,问卷要求他们回答所有相同的问题。@MartinTavarez那么你还应该有另一个带有受访者ID号的栏?例如,被调查者1、被调查者2、被调查者3等等?@MartinTavarez我编辑了我的回复,以产生你想要的东西
 newDF <- data.frame(respondent = DF$Respondent[grep("Question", DF$Logs)],
                question = as.character(DF$Logs[grep("Question", DF$Logs)]),
                questionTime = DF$Time[grep("Question", DF$Logs)],
                responseValue = DF$Logs[-grep("Question", DF$Logs)],
                responseTime = DF$Time[-grep("Question", DF$Logs)])
newDF

 #   respondent  question questionTime responseValue responseTime
 # Respondent 1 Question1     446.6204             4     452.7516
 # Respondent 1 Question2     452.7516             3     458.1999
 # Respondent 1 Question3     458.1999             5     460.2342
 # Respondent 2 Question1     535.9445             5     543.3019
 # Respondent 2 Question2     543.3019             2     549.8399
 # Respondent 2 Question3     549.8399             3     552.2810
 # Respondent 3 Question1     443.2204             1     449.3516
 # Respondent 3 Question2     449.3516             4     454.7999
 # Respondent 3 Question3     454.7999             5     456.8342
 qTime <- dcast(newDF, respondent ~ question, value.var = "questionTime")
names(qTime)[2:length(names(qTime))] <- paste0("TimeQ", seq(1,length(names(qTime))-1,1) )

rValue <- dcast(newDF, respondent ~ question, value.var = "responseValue")

rTime <- dcast(newDF, respondent ~ question, value.var = "responseTime")
names(rTime)[2:length(names(rTime))] <- paste0("TimeA", seq(1,length(names(rTime))-1,1) )

finalDF <- cbind(qTime, rValue[,-1], rTime[,-1])

finalDF

#     respondent   TimeQ1   TimeQ2   TimeQ3 Question1 Question2 Question3   TimeA1   TimeA2   TimeA3
#   Respondent 1 446.6204 452.7516 458.1999         4         3         5 452.7516 458.1999 460.2342
#   Respondent 2 535.9445 543.3019 549.8399         5         2         3 543.3019 549.8399 552.2810
#   Respondent 3 443.2204 449.3516 454.7999         1         4         5 449.3516 454.7999 456.8342