Message-ID: <CAN2xGJYm8pib89x58E5gyJK9UFeoa1Ghz_Qg2V0YLm2H3D8V-A@mail.gmail.com>
Date: 2015-04-23T22:25:12Z
From: Dimitri Liakhovitski
Subject: reshape data frame when one column has unequal number of entries
In-Reply-To: <000a01d07dd3$38d74d70$aa85e850$@bigpond.com>
Thank you very much, everybody!
On Thu, Apr 23, 2015 at 10:38 AM, Duncan Mackay <dulcalma at bigpond.com> wrote:
> Hi Dimitri
>
> here is a quick crude way (needs some polishing)
>
> data.frame(a = rep(x$a,sapply(sapply(x$b, strsplit, ", "), length)), b=
> unlist(sapply(x$b, strsplit, ", ")))
>
> Duncan
>
> Duncan Mackay
> Department of Agronomy and Soil Science
> University of New England
> Armidale NSW 2351
> Email: home: mackay at northnet.com.au
>
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Dimitri
> Liakhovitski
> Sent: Thursday, 23 April 2015 23:15
> To: r-help
> Subject: [R] reshape data frame when one column has unequal number of
> entries
>
> Hello!
>
> I have my data frame x with 2 character columns:
>
> x <- data.frame(a = numeric(), b = I(list()))
> x[1:3,"a"] = 1:3
> x[[1, "b"]] <- "a, b, c"
> x[[2, "b"]] <- "d, e"
> x[[3, "b"]] <- "f"
> x$a = as.character(x$a)
> x$b = as.character(x$b)
> x
> str(x)
>
> I need to produce this data frame:
>
> 1 a
> 1 b
> 1 c
> 2 d
> 2 e
> 3 f
>
> Is it possible without looping?
> Thank you!
>
>
> --
> Dimitri Liakhovitski
>
> ______________________________________________
> R-help at r-project.org 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.
>
--
Dimitri Liakhovitski