[R] Latex Table Help on R
Peter Ehlers
ehlers at ucalgary.ca
Thu Jul 21 16:40:36 CEST 2011
On 2011-07-20 23:50, Jim Silverton wrote:
> Hello everyone,
> Peter (see my earlier post) recommended the following script for finding the
> means and standard deviations and putting them in table form.
>
> However, I would like the standard deviations under the means in brackets.
> Can anyone check this code to see how this can be adjusted?
>
>
> library(xtable)
> dataset1 = matrix( c(1,2,3,4, 5, 6 ), 2 , 3)
> dataset2 = matrix( c(4,3,5,10, 1, 0), 2, 3)
>
> dataset<- rbind(dataset1,dataset2) #combine dataset
>
> means<- apply(dataset,1,mean) #calculate row means
> sds<- apply(dataset,1,sd) #calculate row standard deviation
>
> msd<- paste(round(means,2)," (",round(sds,2),")",sep="") #mean and
> standard deviation
>
> rn<- c("Var1","Var2") #rownames
> cn<- c("Dataset1","Dataset2") #column names
> tab<- matrix(msd,2,2,dimnames=list(rn,cn))
>
> tab
> Dataset1 Dataset2
> Var1 "3 (2)" "3.33 (2.08)"
> Var2 "4 (2)" "4.33 (5.13)"
>
> xtable(tab)
>
You can use the same idea to construct an appropriate matrix:
matm <- matrix(round(means, 2), 2, 2)
mats <- matrix(paste("(",round(sds, 2),")"), 2, 2)
mat <- rbind(matm, c(" "," "), mats) # note the spaces
mat <- mat[c(1,4,3,2,5), ]
colnames(mat) <- c("Dataset1","Dataset2")
rownames(mat) <- c("Var1", " ", " ", "Var2", " ")
# note 1,2,3 spaces to make rownames unique
mat.tb <- xtable(mat, align=c("r","c","c"))
mat.tb
Peter Ehlers
More information about the R-help
mailing list