R Optimizing double for loop, matrix manipulation

后端 未结 4 2087
感情败类
感情败类 2021-01-20 19:20

I am trying to manipulate column data in a two column matrix and output it as a data.frame.

The matrix that I have is in this format where both the values in the sta

4条回答
  •  生来不讨喜
    2021-01-20 20:06

    An ugly dplyr solution:

    library(dplyr)
    df <- as.data.frame(df)
    
    df %>% mutate(End = V2[findInterval(V1, na.omit(V2)) + 1]) %>%
           group_by(End) %>%
           summarise(Start = paste(V1, collapse=", "))
    

    Edit - using findInterval thanks to @bgoldst

提交回复
热议问题