[R] Spread data.frame on 2 variables
Ulrik Stervbo
ulrik.stervbo at gmail.com
Thu Jul 28 13:00:52 CEST 2016
Hi Georg,
it's hard to tell without a reproducible example.
Should spread really merge elements? Does spread know anything about
CustID? Maybe you need to make a useful key of the CustIDs first and spread
on that?
Maybe I'm all off, because I'm really just guessing.
Best,
Ulrik
On Thu, 28 Jul 2016 at 12:36 <G.Maubach at weinwolf.de> wrote:
> Hi All,
>
> I need to spread a data.frame on 2 variables, e. g. "channel" and "unit".
>
> If I do it in two steps spreads keeps all cases that does not look like
> the one before although it contains the same values for a specific case.
>
> Here is what I have right now:
>
> -- cut --
>
> test1$dummy <- 1
> test2 <- spread(data = test1, key = 'channel', value = "dummy")
> test2
> cat("First spread is OK!")
>
> test2$dummy <- 1
> test3 <- spread(data = test2, key = 'unit', value = 'dummy')
>
> test1
> # test2
> test3
> warning(paste0("Second spread is not OK cause spread does not merge
> cases\n",
> "with CustID 700 and 800 into one case,\n",
> "cause they have values on different variables,\n",
> "although the corresponding values of the cases with",
> "custID 700 and 800 are missing."))
>
> cat("What I would like to have is:\n")
> target4 <- structure(list(custID = c(100, 200, 300, 500, 600, 700, 800,
> 900),
> `10` = c(1, NA, NA, NA, NA, NA, NA, NA),
> `20` = c(1, NA, NA, NA, NA, NA, NA, NA),
> `30` = c(NA, NA, NA, NA, NA, NA, 1, 1),
> `40` = c(NA, NA, NA, NA, 1, NA, 1, 1),
> `50` = c(NA, NA, 1, NA, NA, NA, 1, 1),
> `60` = c(NA, NA, NA, NA, NA, 1, NA, NA),
> `70` = c(NA, NA, NA, NA, NA, 1, NA, NA),
> `99` = c(NA, 1, NA, 1, NA, NA, NA, NA),
> `1000` = c(1, NA, NA, NA, NA, NA, 1, 1),
> `2000` = c(NA, NA, NA, NA, 1, 1, 1, NA),
> `3000` = c(NA, NA, 1, NA, NA, 1, NA, NA),
> `4000` = c(NA, NA, 1, NA, NA, NA, NA, NA),
> `6000` = c(NA, NA, NA, NA, 1, NA, NA, NA),
> `9999` = c(NA, 1, NA, 1, NA, NA, NA, NA)),
> .Names = c("custID",
> "10", "20", "30", "40", "50", "60", "70", "99",
> "1000", "2000", "3000", "4000", "6000", "9999"),
> row.names = c(NA, 8L), class = "data.frame")
>
> target4
>
> cat("What would be a proper way to create target4 from test1?")
>
> -- cut --
>
> What would be the proper way to create target4 from test1?
>
> Kind regards
>
> Georg
>
> ______________________________________________
> 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list