假设你有这样的东西:
Col1 Col2
a odd from 1 to 9
b even from 2 to 14
c even from 30 to 50
...我想通过将间隔分成单独的行来扩展行,因此:
Col1 Col2
a 1
a 3
a 5
...
b 2
b 4
b 6
...
c 30
c 32
c 34
...请注意,当它说“偶数从”时,上下界也是偶数,奇数也是如此。
发布于 2018-02-09 22:00:53
使用tidyverse
library(tidyverse)
df %>% mutate(Col2 = map(str_split(Col2," "),
~seq(as.numeric(.[3]),as.numeric(.[5]),2))) %>%
unnest或者,借用@g-grothendieck的解决方案中的separate,可读性更好一些:
df %>%
separate(Col2,as.character(1:5),convert=TRUE) %>%
transmute(Col1,Col2 = map2(`3`,`5`,seq,2)) %>%
unnesthttps://stackoverflow.com/questions/48707294
复制相似问题