[R] tranform a table?
Don MacQueen
macq at llnl.gov
Thu Sep 10 17:01:38 CEST 2009
One could probably use one of the apply family of functions (apply,
lapply, sapply), but having looked into it a bit, I think it's
simpler to use an explicit loop. I doubt you'll encounter a need for
greater efficiency, in the cpu time sense, unless your tables are
huge.
But the looping can be written more simply:
require(xtable)
df <- data.frame(nm=letters[1:4], val1=1:4, val2=round(runif(4)))
for (i in seq(nrow(df))) {
print( xtable( t(df[i,])) , type='html', include.colnames=FALSE)
cat('<br>')
}
One of the other packages for writing html from R objects might have
a function that will automatically subset a dataframe in this
particular way, but it strikes me as somewhat unlikely.
-Don
At 6:10 AM -0700 9/10/09, bbimber wrote:
>hello everyone,
>
>i'm new to R, so i hope you dont mind a fairly basic R question. we're
>using R to manipulate the results of SQL queries and create an HTML output.
>I'm starting with a table that looks essentially like this:
>
>Name Field1 Field2
>John value1 value2
>Jane value3 value4
>
>My table is stored as a dataframe. I'd like to efficiently produce an
>output that iterates through each row, transposes it and outputs an HTML
>table (one per row). like this:
>
>Name: John
>Field1: value1
>Field2: value2
>
>Name: Jane
>Field1: value3
>Field2: value4
>
>I can accomplish this by looping through each row, then outputting that
>row's table. This gets the job done, but it seems there must be a better
>way. I'm going to need to do this sort of conversion a lot,
>so the simpler the better. is there a better way to approach it than the
>code below? is there a more general term for the sort of transformation i'm
>trying to make that might help guide my searching?
>
>i realize i need to look into better methods of outputting HTML tables (like
>r2html).
>
>here's the basic idea. 'labkey.data' is the data frame produced by my SQL
>query:
>
>D<-labkey.data
>H<-colnames(D)
>T<-t(D)
>L<-length(D$id)
>
>output <- ""
>
>for(i in 1:L) {
>R<-my.row <- D[i, ]
>R<-t(R)
>Len<-length(R)
>
>output <- paste(output, "<table border=0>")
>for(j in 1:Len) {
>output <- paste(output,"<tr><td>", H[j],":</td><td>", R[j], "</td>")
>}
>
>output <- paste(output, "</table><p>")
>
>}
>
>write(output, file="${htmlout:output}")
>
>Thanks for any help.
>--
>View this message in context:
>http://*www.*nabble.com/tranform-a-table--tp25382806p25382806.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>R-help at r-project.org 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.
--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062
More information about the R-help
mailing list