-4

I have my input. I am using R. I want solution in R

    id       places         present   
   1234    |new|world|life   yes
   5111      
 2012222   |lamp             yes
 123333    |world11|ness     yes    

I want output

      id       places         present   
   1234        new            y9970s
   1234        world          7655s
   1234        life           54644s
   5111      
2012222        lamp           y777s
 123333        world11        y998s
 123333        ness           y99s

I have tried

dt <- data.table(input)
dt=dt[ , list( V3 = unlist( strsplit(as.character( V3),"\\|") ) ) , by = V1]

but the 3rd column is left out. Even if I have multiple columns in that case how will I work

user3619015
  • 176
  • 1
  • 1
  • 9
  • 2
    So what have you tried so far? Someone will help you improve your code if you demonstrate that you made some effort before asking for help on SO. – talat May 30 '14 at 10:30
  • 1
    Please read the info about [how to ask a question](http://stackoverflow.com/help/how-to-ask) and how to produce a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). This will make it much easier for others to help you. – Jaap May 30 '14 at 10:55
  • I have tried dt <- data.table(input) dt=dt[ , list( V3 = unlist( strsplit(as.character( V2),"\\|") ) ) , by = V1] but the other columns are left if I have additional column than present – user3619015 May 30 '14 at 11:12
  • @user3619015 did you try the suggested code from my answer? – Jaap May 30 '14 at 17:20
  • can you include a `dput` of (a part of) your data? – Jaap May 30 '14 at 17:21
  • Its giving an additional column for the first pipe which it shudn't. 2012222 lamp y777s 2012222 y777s – user3619015 May 31 '14 at 10:46
  • I dont know about dput what does it do? – user3619015 May 31 '14 at 15:54

1 Answers1

0

A possible solution with the data.table package:

library(data.table)
dt <- data.table(df)
new.dt <- dt[,strsplit(as.character(places),"|",fixed=TRUE), by=list(id,present)]
Jaap
  • 81,064
  • 34
  • 182
  • 193
  • there is an empty column because of the first pipe symbol and it should not come. First Id element 1234 has 4 rows, but it should give 3 rows – user3619015 May 31 '14 at 11:57