[R] Splitting one column value into multiple rows

David Winsemius dwinsemius at comcast.net
Fri Jul 15 03:02:14 CEST 2011


On Jul 14, 2011, at 6:47 PM, Madana_Babu wrote:

> Hi i have the data in the following format:
>
> rent,100,1,common,674
> pipe,200,0,usual,864
> car,300,1,uncommon,392:jump,700,0,common,664
> car,200,1,uncommon,864:snap,900,1,usual,746
> stint,600,1,uncommon,257
> pull,800,0,usual,594
>
> where as i want the above 6 lines data into 8 lines as below  
> (Spliting row 3
> & 4 at : and sending to a new row):

 > Lines <- readLines(textConnection("rent,100,1,common,674
+ pipe,200,0,usual,864
+ car,300,1,uncommon,392:jump,700,0,common,664
+ car,200,1,uncommon,864:snap,900,1,usual,746
+ stint,600,1,uncommon,257
+ pull,800,0,usual,594"))
 > closeAllConnections()
 > newlines <- strsplit(Lines, ":")
 > newlines2 <- unlist(newlines)
 > newlines2
[1] "rent,100,1,common,674"    "pipe,200,0,usual,864"     "car, 
300,1,uncommon,392"   "jump,700,0,common,664"
[5] "car,200,1,uncommon,864"   "snap,900,1,usual,746"     "stint, 
600,1,uncommon,257" "pull,800,0,usual,594"
 > read.table(textConnection(newlines2), sep=",")
      V1  V2 V3       V4  V5
1  rent 100  1   common 674
2  pipe 200  0    usual 864
3   car 300  1 uncommon 392
4  jump 700  0   common 664
5   car 200  1 uncommon 864
6  snap 900  1    usual 746
7 stint 600  1 uncommon 257
8  pull 800  0    usual 594

>
> rent,100,1,common,674
> pipe,200,0,usual,864
> car,300,1,uncommon,392
> jump,700,0,common,664
> car,200,1,uncommon,864
> snap,900,1,usual,746
> stint,600,1,uncommon,257
> pull,800,0,usual,594
>
> Request any one who can help me getting this done.
>
> Regards,
> Madana
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Splitting-one-column-value-into-multiple-rows-tp3668835p3668835.html
> Sent from the R help mailing list archive at Nabble.com.
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list