R--如何争辩“如果Rt1 = N / A,则使用Rt2代替”

时间:2017-06-14 16:57:50

标签: r

我正在尝试计算受试者的时间点之间的增量。 我已经计算了标准增量(即时间2-时间1,时间3 - 时间2),但我还想计算累积增量(即时间X -Time 1)。然而,我遇到了问题,因为并非我的所有主题都有时间1.相反,我想写一个参数从第一个可用的已完成数据中减去(无论是时间1还是时间2。

基本上我问的是如何写“如果Rt1 = N / A,那么改用Rt2”。

我目前用于重新排列数据和计算增量的代码是:

##Reverse all data points### 

pulse<- transmute(pulse, ï..Question, Type, Student, Rt1=6-t1, Rt2=6-t2,Rt3=6-t3, Rt4=6-t4, Rt5=6-t5, Rt6=6-t6, Rt7=6-t7, Rt8=6-t8, Rt9=6-t9, Rt10=6-t10, Rt11=6-t11)

    ### Calculate Deltas ###
  {pulse<- mutate(pulse, d1=Rt2-Rt1)
  pulse<- mutate(pulse, d2=Rt3-Rt2)
  pulse<- mutate(pulse, d3=Rt4-Rt3)
  pulse<- mutate(pulse, d4=Rt5-Rt4)
  pulse<- mutate(pulse, d5=Rt6-Rt5)
  pulse<- mutate(pulse, d6=Rt7-Rt6)
  pulse<- mutate(pulse, d7=Rt8-Rt7)
  pulse<- mutate(pulse, d8=Rt9-Rt8)
  pulse<- mutate(pulse, d9=Rt10-Rt9)
  pulse<- mutate(pulse, d10=Rt11-Rt10)}

  ### Calculate Cumulative Deltas###
   { pulse<- mutate(pulse, cd1=Rt2-Rt1)
     pulse<- mutate(pulse, cd2=Rt3-Rt1)
     pulse<- mutate(pulse, cd3=Rt4-Rt1)
     pulse<- mutate(pulse, cd4=Rt5-Rt1)
     pulse<- mutate(pulse, cd5=Rt6-Rt1)
     pulse<- mutate(pulse, cd6=Rt7-Rt1)
     pulse<- mutate(pulse, cd7=Rt8-Rt1)
     pulse<- mutate(pulse, cd8=Rt9-Rt1)
     pulse<- mutate(pulse, cd9=Rt10-Rt1)
     pulse<- mutate(pulse, cd10=Rt11-Rt1)}

#############################Rearrange Data #######################################


  pulse1<-NULL
  timePeriods<-c("Rt1", "Rt2", "Rt3", "Rt4", "Rt5", "Rt6", "Rt7", "Rt8", "Rt9", "Rt10", "Rt11", 
                 "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "d10","cd1", "cd2", "cd3", "cd4", "cd5", "cd6", "cd7", "cd8", "cd9", "cd10")
  weeks<-c("Jan11","Jan25","Feb1","Feb8", "Feb15", "Mar1", "Mar8", "Mar15","Mar22", "Mar29", "Apr5", "Jan25","Feb1","Feb8", "Feb15", "Mar1", "Mar8", "Mar15","Mar22", "Mar29", "Apr5", "Jan25","Feb1","Feb8", "Feb15", "Mar1", "Mar8", "Mar15","Mar22", "Mar29", "Apr5")
  measureType<-c(rep("Rating", 11), rep("Delta", 10), rep ("Cumulative Deltas", 10))

  for (columnNumber in 1:31)
  {
    temp.data<-data.frame(Student=pulse$Student, Question=pulse$ï..Question, Type=pulse$Type, 
                          measureType= measureType[columnNumber], week=weeks[columnNumber], 
                          Rating=pulse[, columnNumber+3])
    pulse1<-rbind(pulse1, temp.data)  
  }
  head(pulse1)

基本上我需要找出一种方法来从第一个可用时间点计算累积增量。在R中有办法做到这一点吗?

请查看我的附加数据:

> dput(head(pulse))
structure(list(ï..Question = structure(c(1L, 9L, 10L, 2L, 8L, 
3L), .Label = c("Q", "Q10", "Q11_1", "Q11_2", "Q11_3", "Q11_4", 
"Q11_5", "Q12", "Q2", "Q8"), class = "factor"), Type = structure(c(2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("FYS", "SNR"), class = "factor"), 
    Student = c(789331L, 789331L, 789331L, 789331L, 789331L, 
    789331L), t1 = c(2L, 3L, 2L, 2L, 3L, NA), t2 = c(2L, 2L, 
    2L, 2L, 3L, 5L), t3 = c(2L, 2L, 2L, 2L, 2L, 4L), t4 = c(1L, 
    2L, 2L, 2L, 2L, 4L), t5 = c(3L, 3L, 3L, 2L, 2L, 4L), t6 = c(2L, 
    4L, 4L, 5L, 3L, 4L), t7 = c(1L, 4L, 4L, 3L, 3L, 4L), t8 = c(2L, 
    3L, 3L, 3L, 3L, 4L), t9 = c(2L, 3L, 3L, 3L, 3L, 4L), t10 = c(1L, 
    3L, 3L, 3L, 2L, 4L), t11 = c(1L, 3L, 2L, 2L, 2L, 4L)), .Names = c("ï..Question", 
"Type", "Student", "t1", "t2", "t3", "t4", "t5", "t6", "t7", 
"t8", "t9", "t10", "t11"), row.names = c(NA, 6L), class = "data.frame")

干杯!

1 个答案:

答案 0 :(得分:1)

使用if_else来测试Rt1 <{1}}

NA