Dear R helpers, To illustrate my problem, here is a simplified example. I want to write a table to a file similar to: x a 1 4,5 2 8,9,10 Note the length of elements of "a" is 2 and 3 respectively. This can be created by, for example, x <- c(1,2) a <- NULL a[1] <- list(c(4,5)) a[2] <- list(c(8,9,10) Any suggestions to write such a table to file would be appreciated. Thanks, Zhu Wang
What about > x <- 1:2 > a <- list(c(4,5),c(8,9,10)) # Which is the way you *should* # have constructed a! > ddd <- data.frame(x=x,a=I(unlist(lapply(a,paste,collapse=",")))) > write.table(ddd,file="ddd.out",quote=FALSE,row.names=FALSE) cheers, Rolf Turner rolf at math.unb.ca Original message:> Dear R helpers, > > To illustrate my problem, here is a simplified example. I want to write > a table to a file similar to: > x a > 1 4,5 > 2 8,9,10 > > Note the length of elements of "a" is 2 and 3 respectively. This can be > created by, for example, > x <- c(1,2) > a <- NULL > a[1] <- list(c(4,5)) > a[2] <- list(c(8,9,10) > > Any suggestions to write such a table to file would be appreciated. > > Thanks, > > Zhu Wang
Yes, the code does the job. Thanks, Zhu Wang Rolf Turner wrote:> What about > > > x <- 1:2 > > a <- list(c(4,5),c(8,9,10)) # Which is the way you *should* > # have constructed a! > > ddd <- data.frame(x=x,a=I(unlist(lapply(a,paste,collapse=",")))) > > write.table(ddd,file="ddd.out",quote=FALSE,row.names=FALSE) > > cheers, > > Rolf Turner > rolf at math.unb.ca > > Original message: > > >> Dear R helpers, >> >> To illustrate my problem, here is a simplified example. I want to write >> a table to a file similar to: >> x a >> 1 4,5 >> 2 8,9,10 >> >> Note the length of elements of "a" is 2 and 3 respectively. This can be >> created by, for example, >> x <- c(1,2) >> a <- NULL >> a[1] <- list(c(4,5)) >> a[2] <- list(c(8,9,10) >> >> Any suggestions to write such a table to file would be appreciated. >> >> Thanks, >> >> Zhu Wang
Gabor Grothendieck
2006-Sep-01 22:06 UTC
[R] write a table to file with unequal length of lists
A simple for loop would do it: x <- 1:2; a <- list(4:5, 8:10) # test data cat("x a\n", file = "") # only if you want a header for(i in seq(along = x)) cat(x[i], a[[i]], "\n", file = "") On 9/1/06, Zhu Wang <zwang at scharp.org> wrote:> Dear R helpers, > > To illustrate my problem, here is a simplified example. I want to write > a table to a file similar to: > x a > 1 4,5 > 2 8,9,10 > > Note the length of elements of "a" is 2 and 3 respectively. This can be > created by, for example, > x <- c(1,2) > a <- NULL > a[1] <- list(c(4,5)) > a[2] <- list(c(8,9,10) > > Any suggestions to write such a table to file would be appreciated. > > Thanks, > > Zhu Wang > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > 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. >
Apparently I missed cat. Thanks. Zhu Wang Gabor Grothendieck wrote:> A simple for loop would do it: > > x <- 1:2; a <- list(4:5, 8:10) # test data > > cat("x a\n", file = "") # only if you want a header > for(i in seq(along = x)) cat(x[i], a[[i]], "\n", file = "") > > > On 9/1/06, Zhu Wang <zwang at scharp.org> wrote: >> Dear R helpers, >> >> To illustrate my problem, here is a simplified example. I want to write >> a table to a file similar to: >> x a >> 1 4,5 >> 2 8,9,10 >> >> Note the length of elements of "a" is 2 and 3 respectively. This can be >> created by, for example, >> x <- c(1,2) >> a <- NULL >> a[1] <- list(c(4,5)) >> a[2] <- list(c(8,9,10) >> >> Any suggestions to write such a table to file would be appreciated. >> >> Thanks, >> >> Zhu Wang >> >> ______________________________________________ >> R-help at stat.math.ethz.ch mailing list >> 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. >>