Applying a function to each row of a data.table

前端 未结 7 1294
日久生厌
日久生厌 2020-12-03 10:46

I looking for a way to efficiently apply a function to each row of data.table. Let\'s consider the following data table:

library(data.table)
library(stringr)         


        
7条回答
  •  时光取名叫无心
    2020-12-03 11:04

    The dplyr/tidyr approach also works with data tables.

    library(dplyr)
    library(tidyr)
    x %>% 
      separate(b, into = c("b1", "b2")) %>% 
      gather(b, "V1", b1:b2) %>%
      arrange(V1) %>%
      select(a, V1)
    

    Or, using the standard evaluation forms:

    x %>% 
      separate_("b", into = c("b1", "b2")) %>% 
      gather_("b", "V1", c("b1", "b2")) %>%
      arrange_(~ V1) %>%
      select_(~ a, ~ V1)
    

    The case of different numbers of values in the b column is only slightly more complicated.

    library(stringr)
    
    x2 <- data.table(
      a = c(1:3, 1), 
      b = c('12 13', '14', '15 16 17', '18 19')
    )
    
    n <- max(str_count(x2$b, " ")) + 1
    b_cols <- paste0("b", seq_len(n))
    x2 %>% 
      separate_("b", into = b_cols, extra = "drop") %>% 
      gather_("b", "V1", b_cols) %>%
      arrange_(~ V1) %>%
      select_(~ a, ~ V1)
    

提交回复
热议问题