【问题标题】:Functions for finding first and last variables in the vector用于在向量中查找第一个和最后一个变量的函数
【发布时间】:2016-01-25 22:17:53
【问题描述】:

我正在寻找一个R 函数来查找向量中的第一个和最后一个变量,类似于min 的最小值和max 的最大值。我知道我可以计算向量的长度,然后从那里开始,但它不能满足我的需要。

我有以下数据集(实际上要大得多):

a<-(c("2013-02-25","2013-03-13","2013-04-24","2013-05-12","2013-07-12","2013-08-11","actual_exam_date"))
b<-c(300,230,400,NA,NA,NA,"2013-04-30")
c<-c(NA,260,410,420,NA,NA,"2013-05-30")
d<-c(300,230,400,NA,370,390,"2013-08-30")
df<-as.data.frame(rbind(b,c,d))
colnames(df)<-a
rownames(df)<-(c("student 1","student 2","student 3"))
df$student_id <- row.names(df)
library('reshape2')
df2 <- melt(df, id.vars = c('student_id','actual_exam_date'),
                variable.name = 'pretest_date',
                value.name = 'pretest_score')
df2 <- df2[!is.na(df2$pretest_score),]
df2$actual_exam_date <- as.Date(df2$actual_exam_date)
df2$pretest_date <- as.Date(df2$pretest_date)
df2$days_before_exam <- as.integer(df2$actual_exam_date - df2$pretest_date)
df2$pretest_score <- as.numeric(df2$pretest_score)
df2

我计算每个学生最高分数的方法是这样的:

aggregate(pretest_score ~ student_id, df2, max)

现在我正在寻找每个学生的第一个和最后一个预测试分数,以计算它们之间的差异。有没有办法使用聚合来做到这一点?

【问题讨论】:

  • @Jilber 如果我在上面的示例中将max 替换为headtailaggregate(pretest_score ~ student_id, df2, max) 中,它会产生三列而不是一列的预测试分数..不知道为什么跨度>
  • 现在我想我的答案我不知道为什么我猜第一个和最后一个应该等于最小值和最大值......即使你应该更精确地了解第一个和最后一个. “第一”和“最后”与日期或日期集的第一行和最后一行有关?

标签: r time-series


【解决方案1】:

另一个使用data.table 的建议允许您在不指定匿名函数的情况下在一行中同时订购(如果需要)并获得结果

library(data.table)
setDT(df2)[order(pretest_date), diff(pretest_score[c(1, .N)]), keyby = student_id]
#    student_id  V1
# 1:  student 1 100
# 2:  student 2 160
# 3:  student 3  90

【讨论】:

    【解决方案2】:

    首先通过预测试日期对您的数据进行排序

    df2 <- df2[order(df2$pretest_date), ]
    

    然后

    aggregate(pretest_score ~ student_id, df2, function(x) tail(x, 1) - x[1])
      student_id pretest_score
    1  student 1           100
    2  student 2           160
    3  student 3            90
    

    【讨论】:

      【解决方案3】:

      每个学生的第一个值

      > aggregate(pretest_score ~ student_id, df2, head, 1)
        student_id pretest_score
      1  student 1           300
      2  student 2           260
      3  student 3           300
      

      每个学生的最后一个值

        > aggregate(pretest_score ~ student_id, df2, tail, 1)
            student_id pretest_score
          1  student 1           400
          2  student 2           420
          3  student 3           390
      

      【讨论】:

      • ,1headtail 之后是什么意思?
      • @Oposum - 这是head/tail 的第二个参数,通过aggregate() 传递。
      【解决方案4】:

      有几种方法可以满足您的要求。使用dplyr 可以找到最小值和最大值

       df2 %>% group_by(student_id) %>%
               filter(pretest_score == max(pretest_score) | pretest_score == min(pretest_score)) %>%
               mutate(differ = max(pretest_score) - min(pretest_score))
      Source: local data frame [6 x 6]
      Groups: student_id [3]
      
        student_id actual_exam_date pretest_date pretest_score days_before_exam differ
             (chr)           (date)       (date)         (dbl)            (int)  (dbl)
      1  student 1       2013-04-30   2013-03-13           230               48    170
      2  student 2       2013-05-30   2013-03-13           260               78    160
      3  student 3       2013-08-30   2013-03-13           230              170    170
      4  student 1       2013-04-30   2013-04-24           400                6    170
      5  student 3       2013-08-30   2013-04-24           400              128    170
      6  student 2       2013-05-30   2013-05-12           420               18    160
      

      虽然有聚合:

       aggregate(df2$pretest_score, list(df2$student_id), FUN = function(x) max(x) - min(x))
          Group.1   x
      1 student 1 170
      2 student 2 160
      3 student 3 170
      

      【讨论】:

      • 在我看来这与 minmax 无关。
      【解决方案5】:

      我认为使用tapply 会更容易:

      tapply(df2$pretest_score, df2$student_id, function(x) tail(x,1)-head(x,1))
      
      > student 1 student 2 student 3
              100       160        90
      

      【讨论】:

        猜你喜欢
        • 2011-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-21
        • 1970-01-01
        • 2021-12-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多