[R] Create a data.table by looping over variable names
c06n.rm at gmail.com
Wed Dec 16 13:36:40 CET 2015
Maybe there’s a cleverer way, but that’s what I can think off the top of my head:
n <- 100
M.dt <- data.table(matrix(ncol = 100))
M.dt <- 100
names(M.dt) <- sapply(1:100, function(x) paste0("i", x))
The sapply is your loop.
> Am 16.12.2015 um 12:53 schrieb Matteo Richiardi <matteo.richiardi at gmail.com>:
> I apologise for this very basic question, but I found no answers on the web.
> I want to create a data.table with n columns, named i1 ... i'n', with only
> one row with value = 100 for every variable.
> I can do it "by hand":
> M.dt <- data.table(i1=100,i2=100,i3=100)
> but I would like to make it in a loop, as 'n' is large in my case.
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help