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)
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)