【问题标题】:Nested IF THEN statement with MAX condition in RR中带有MAX条件的嵌套IF THEN语句
【发布时间】:2018-03-21 19:48:06
【问题描述】:

我有一个包含以下列的 df:

RowID, UserID, Event

每个用户 ID 有多个行和许多不同的用户 ID。事件将是一个整数 >=0。

我需要 R 查找特定用户 ID 的事件大于 0 的最大行 ID,然后在新列中将该用户 ID 的任何后续行标记为“之后”(否则,将其标记为“之前” )。

例子:

rowID, userID, event, output  
1, 999, 0, before  
2, 999, 1, before  
3, 999, 0, after 

我对 R 完全陌生,所以甚至不知道从哪里开始实现这一点。我知道如何在 Excel 中做得很好,但我的 CSV 太大而无法进行计算。

提前致谢。

【问题讨论】:

    标签: r excel syntax formula


    【解决方案1】:

    这是一项具有基本 R 功能的棘手任务。此解决方案使用 dplyr 包,如果您正在学习 R 编程,我建议您学习它。

    生成一些数据:

    library(dplyr)
    df <- data.frame(rowID = 1:5, userID = c(999,999,999,111,111), event = c(0,1,0,1,1))
    df    
      rowID userID event
    1     1    999     0
    2     2    999     1
    3     3    999     0
    4     4    111     1
    5     5    111     1
    

    将行过滤为仅event 等于 1 的行,按用户 ID 分组,并计算最大行 ID。

    df %>% filter(event == 1) %>% group_by(userID) %>% summarise(maxR = max(rowID))
    
    # A tibble: 2 x 2
      userID  maxR
       <dbl> <dbl>
    1    111     5
    2    999     2
    

    【讨论】:

    • 这似乎没有回答 OPs 的问题和他的预期输出。此外,我不会称在基础 R 中重现您的结果很棘手:do.call(rbind,lapply(split(df, df$userID), function(x) x[order(x$rowID, decreasing = T), ][x$event == 1, ][1, -3])) 但这只是一个旁注。 dplyr 语法肯定更整洁;-)
    【解决方案2】:

    您将在下面逐步找到如何计算output 列的方法。

    请注意,我还添加了一个事件大于 0 的用户,这导致 NA 作为最大值 rowID 并在额外分配中处理。

    > df <- read.table(header = TRUE, sep=",", text = "rowID, userID, event   
    + 1, 999, 0   
    + 2, 999, 1   
    + 3, 999, 0
    + 4, 100, 0
    + 5, 100, 1
    + 6, 100, 0
    + 7, 100, 1
    + 8, 100, 0
    + 9, 100, 0
    + 10, 101, 0
    + 11, 101, 0
    + 12, 102, 1
    + ")
    > 
    > ## filter events
    > df1 <- df[df$event > 0,]
    > ## calculate max rowID per user
    > max <- setNames(aggregate(df1$rowID, by = list(df1$userID), max) , c("userID", "maxRowID"))
    > max
      userID maxRowID
    1    100        7
    2    102       12
    3    999        2
    > 
    > ## merge the max to the dataframe
    > mrg <- merge(x = df, y = max, by = "userID" , all.x = TRUE)
    > ## establish the original order
    > mrg <- mrg[with(mrg, order(rowID)), ]
    > mrg
       userID rowID event maxRowID
    10    999     1     0        2
    11    999     2     1        2
    12    999     3     0        2
    1     100     4     0        7
    2     100     5     1        7
    5     100     6     0        7
    6     100     7     1        7
    3     100     8     0        7
    4     100     9     0        7
    7     101    10     0       NA
    8     101    11     0       NA
    9     102    12     1       12
    > 
    > ## calculate output, 
    > output <- ifelse( mrg$rowID >  mrg$maxRowID,'after','before')
    > ## consider also case with no event > 0
    > output[is.na(output)] <- 'before'
    > 
    > ## add the output column to the original dataframe
    > df$output <- output
    > df
       rowID userID event output
    1      1    999     0 before
    2      2    999     1 before
    3      3    999     0  after
    4      4    100     0 before
    5      5    100     1 before
    6      6    100     0 before
    7      7    100     1 before
    8      8    100     0  after
    9      9    100     0  after
    10    10    101     0 before
    11    11    101     0 before
    12    12    102     1 before
    > 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多