[R] Spread data.frame on 2 variables
G.Maubach at weinwolf.de
G.Maubach at weinwolf.de
Thu Jul 28 12:32:17 CEST 2016
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
More information about the R-help
mailing list