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

Attachment: signature.asc
Description: PGP signature

______________________________________________
R-help@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.

Reply via email to