"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Tue, Jun 12, 2018 at 6:32 AM, Massimo Bressan <
[hidden email]> wrote:
> # considering this data.frame as a reproducible example
> d<-data.frame(i=c(1,2,3), s=c('97,98,99','103,105', '118'),
> stringsAsFactors = FALSE)
> #I need to get this final result
> r<-data.frame(i=c(1,1,1,2,2,3), s=c(97, 98, 99, 103, 105, 118))
> #this is my attempt
> #number of components for each element (3) of the list
> #returned by strsplit
> n<-unlist(lapply(strsplit(d$s,','), length))
> #extract components of all elements of the list
> #replicate each element of i
> #by the number of components of each element of the list
> i<-rep(d$i, n)
> #compose final result
> r_final<-data.frame(i,s, stringsAsFactors = FALSE)
> #I'm not much satisfied by the approach, it seems to me a bit clumsy...
> #any help for improving it?
> #a novice
> [[alternative HTML version deleted]]
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.