[R] reshaping column items into rows per unique ID
peter dalgaard
pdalgd at gmail.com
Sun Feb 25 20:20:24 CET 2018
It depends quite strongly on what you want to do with the result, but I wonder if what is really needed might be a list of diettypes per person, i.e. continuing from Eric's code
> On 25 Feb 2018, at 18:56 , Eric Berger <ericjberger at gmail.com> wrote:
>
> Hi Allaisone,
> I took a slightly different approach but you might find this either as or
> more useful than your approach, or at least a start on the path to a
> solution you need.
>
> df1 <-
> data.frame(CustId=c(1,1,1,2,3,3,4,4,4),DietType=c("a","c","b","f","a","j","c","c","f"),
> stringsAsFactors=FALSE)
> with(df1, tapply(DietType, CustId, list))
$`1`
[1] "a" "c" "b"
$`2`
[1] "f"
$`3`
[1] "a" "j"
$`4`
[1] "c" "c" "f"
or maybe get rid of duplicates with
> with(df1, tapply(DietType, CustId, unique))
$`1`
[1] "a" "c" "b"
$`2`
[1] "f"
$`3`
[1] "a" "j"
$`4`
[1] "c" "f"
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help
mailing list