I would like to use map() from the purrr package to iterate over a subset of variables of my data frame. Is there a standard and convenient approach that?
take the following example dataset:
library(data.table)
library(purrr)
dt <- data.table(id= c("Alpha 1","Alpha 2","Alpha 3","Beta 1"),
id2= c("gamma 1","gamma 2","gamma 3","Delta 1") ,
y = rnorm(4))
id id2 y
1: Alpha 1 gamma 1 -1.1184009
2: Alpha 2 gamma 2 0.4347047
3: Alpha 3 gamma 3 0.2318315
4: Beta 1 Delta 1 1.2640080
I would like to split my id columns every time there is a space (" "). The final dataset should look like this.
id numberid id2 numberid2 y
1: Alpha 1 gamma 1 -1.45772675
2: Alpha 2 gamma 2 -1.07430118
3: Alpha 3 gamma 3 -0.53454071
4: Beta 1 Delta 1 -0.05854228
I know how to do this one column at the time:
dt_m <- dt%>%separate(id,
sep=" ", c("id","numberid"))
id numberid id2 y
1: Alpha 1 gamma 1 2.0789930
2: Alpha 2 gamma 2 -0.2528485
3: Alpha 3 gamma 3 0.1332267
4: Beta 1 Delta 1 1.9299524
But I would like to iterate this using map over a number of columns. Does anyone knows a convenient way to
iterate with map over a set of columns, returning a data frame
and using the columns both for indexing and as a character sting (to paste number"id" and number"id2")?
I have tried something like this but it produces an empty data frame
vars <- c("id","id2")
dt2 <- dt%>%map_df(vars,~separate(.x,sep=" ", c((.x), "number")))
thanks a lot for your help