[R] Reorganize the data (dplyr or other packages?)
Rasmus Liland
jr@| @end|ng |rom po@teo@no
Mon Aug 17 21:09:17 CEST 2020
On 2020-08-17 10:09 -0700, Bert Gunter wrote:
| On Mon, Aug 17, 2020 at 9:53 AM Rasmus Liland wrote:
| |
| | Also, stack is also possible to use:
| |
| | tab <- structure(list(
| | date = c("2019M08", "2019M09", "2019M10"),
| | down = c(0.01709827, 0.02094724, 0.01750911),
| | uc = c(0.2653882, 0.2265797, 0.245003),
| | up = c(0.7175136, 0.7524731, 0.7374879)),
| | class = "data.frame", row.names = c(NA, -3L))
| |
| | out <- utils::stack(x=tab, select=-date)
| | colnames(out) <- c("percentage", "direction")
| | out$date <- tab$date
| | out <- out[,sort(colnames(out))]
|
| Well, not that there is anything
| "wrong" with previous suggestions, but
| it is pretty straightforward just with
| base R functionality:
|
| > nm <- names(tab)[2:4]
| > with(tab, data.frame(date = rep(date, length(nm)),
| + direction = rep(nm, e = 3),
| + percentage = do.call(c, tab[, nm]))
| + )
This is good :) You can also use unlist
directly instead of do.call(c, ...)
nm <- names(tab)[2:4]
data.frame(
date=tab$date,
direction=rep(nm, each=length(nm)),
percentage=unlist(tab[,nm]))
V
r
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200817/183d666d/attachment.sig>
More information about the R-help
mailing list