0

I have a df like

ind1.id ind2.id group   wang
02_1    02_1    1205    -0.2999
02_1    02_11   1205    -0.1688
02_1    02_12   1205    -0.0873
02_1    02_13   1205    -0.0443
02_1    02_14   1205    -0.1415

and I would like to split the group column so that it is like this

ind1.id ind2.id group1  group2  wang
02_1    02_1    12  05  -0.2999
02_1    02_11   12  05  -0.1688
02_1    02_12   12  05  -0.0873
02_1    02_13   12  05  -0.0443
02_1    02_14   12  05  -0.1415

I've messed around with strsplit and stuff but haven't managed to get every far.

Thanks.

user438383
  • 5,716
  • 8
  • 28
  • 43
  • 1
    please check your desired result. It does not seem right. – Andre Elrico Mar 15 '18 at 17:12
  • 1
    Can you explain why first 3 observations, the group 1205 becomes 12 and 5, then 4th example group 1205 becomes 12 and 0, then last example group 1205 becomes 12 and 50? – clarity123 Mar 15 '18 at 17:12

2 Answers2

3

try:

 library(tidyverse)
 df %>%
   as_tibble() %>%
   separate(group, c("group1", "group2"), sep = "0")
Stephan
  • 2,056
  • 1
  • 9
  • 20
0

Similar, cut after first two digits.

df %>% separate(group,c("group1","group2"),sep = "(?<=^\\d\\d)",remove=T)

Result:

   ind1.id ind2.id group1 group2    wang
1:    02_1    02_1     12     05 -0.2999
2:    02_1   02_11     12     05 -0.1688
3:    02_1   02_12     12     05 -0.0873
4:    02_1   02_13     12     05 -0.0443
5:    02_1   02_14     12     05 -0.1415
Andre Elrico
  • 10,956
  • 6
  • 50
  • 69