Re: [R] Create a data.table by looping over variable names

2015-12-16 Thread András Tajti
Dear Matteo, I found a one-liner solution to this problem: n <- 100 data.table(i0 = 100)[, (paste0("i", 1:n)) := 100] the data.table(i0=100) creates a one column-one row data.table, Between the [] brakets i create a column name vector with paste, and surround it with () to make it recognised as n

Re: [R] Create a data.table by looping over variable names

2015-12-16 Thread David Käthner
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 : > > I apologise

Re: [R] Create a data.table by looping over variable names

2015-12-16 Thread Matteo Richiardi
Hi, thanks a lot to everybody for your help. Very nice suggestions! Matteo On 16 December 2015 at 12:53, Matteo Richiardi wrote: > 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

Re: [R] Create a data.table by looping over variable names

2015-12-16 Thread Giorgio Garziano
library(data.table) dat <- as.data.table(matrix(100, nrow=1, ncol=100)) colnames(dat) <- gsub("V", "i", colnames(dat)) -- GG [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://st

Re: [R] Create a data.table by looping over variable names

2015-12-16 Thread PIKAL Petr
. But as I do not know what you want to do with your data.table I may be completely out. Cheers Petr > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Matteo > Richiardi > Sent: Wednesday, December 16, 2015 12:53 PM > To: r-help@r-pr

[R] Create a data.table by looping over variable names

2015-12-16 Thread Matteo Richiardi
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 lo