It would help if you created a toy example so those who respond can
show the actual code.
A couple of comments on your code, using single-letter names as
identifiers is not recommended just because there are a number of
heavily-used functions that have single letter names. Name conflicts
are less of a problem now that packages have name spaces but it is
best not to tempt fate.
> D<-labkey.data
> H<-colnames(D)
> T<-t(D)
> L<-length(D$id)
You have the right idea of taking the data frame and transposing it.
During this process all the values will probably be converted to
character data, if they are not already character. Columns of a data
frame can be heterogeneous but the transpose of a data frame is an
array with homogeneous data types.
The best thing to do when developing code like this is to use the
str() function frequently. So
str(labkey.data) # shows the structure of the original data frame
str(t(labkey.data)) # shows the structure of the transpose
You can work on columns of the transposed table with loops and
indices, as shown below, but often it is more effective to write a
function that produces the desired result from one column then "apply"
the function to the columns. It would look like
cfunc <- function(cc) {
for (nm in names(cc)) output <- paste(output, "<tr><td>",
nm,":</td><td>", cc[nm], "</td>")
}
Then you can just
apply(t(labkey.data), 2, cfunc))
If you are doing this on large data frames you will probably find it
more convenient to write to a connection, rather than accumulating all
the output in a character string. See the help page for "file" or
"open".
Constructions like
output <- paste(output, "new stuff")
become inefficient in an interpreted language because appending to an
existing structure usually means that you need to allocate the new
structure, then copy the old value and append the new value. Files,
on the other hand, are designed to have contents appended to them and
the operating system takes care to do this efficiently.
Building structures by iterating and appending is discouraged in R. Compare
> sqrts <- numeric(0)
> system.time(for (i in 1:100000) sqrts <- c(sqrts, sqrt(i)))
user system elapsed
64.136 5.680 69.862
> sqrts <- numeric(100000)
> system.time(for (i in seq(along = sqrts)) sqrts[i] <- sqrt(i))
user system elapsed
1.04 0.00 1.04
> system.time(sqrts <- sqrt(1:100000))
user system elapsed
0.008 0.000 0.008
Each of these determines and stores the square roots of the first
100000 integers. The third one takes advantage of the vectorization
of arithmetic and mathematic functions and is the fastest. The second
one preallocates the storage and updates elements in a loop. It
suffers from the overhead of interpretation and looping but does not
do any copying. The first is appending to an existing structure in a
loop and is by far the slowest because of all the copying entailed.