Skip to content

problem with rbind

2 messages · SNN, PIKAL Petr

SNN
#
Hi All,

I have a problem with rbind.
I have data that consist of weight height .. etc of 1000 patients. I would
like to find the mean and the standard deviation ( for the weight , height
etc)  for each gender.


data<-read.table("data.txt", header=T, sep='\t')
fdata=NULL

for (i in 1:50){

nn<-names(X)[i]

m<-tapply(X[,i],data$gender,mean,na.rm=T)
s<-tapply(X[,i], data$gender, sd,na.rm=T)

p<-cbind(mean=m,sd.dev=s)

cn<-paste(nn,colnames(p),sep="_")
 
colnames(p)<-cn


fdata<-rbind(fdata,p)
} 
write.table(fdata, ?results.txt?,sep=?\t?,quote=FALSE, col.names=T)


here is the problem, 
1.	I have a header for each table but only the first one is printed.
2.	the weight_mean is suppose to be on the top of the means but it appears
on the top of the first column ( with no tab before the header)

weight_mean    weight_sd.dev 
F  14.33333  4.932883 
M  34.66667 10.692677 
F  35.00000  7.071068 
M  34.66667 10.692677 
.
.
.

I want the result to look like this with a line separating each table and
each table has a header

       weight_mean    weight_sd.dev 
 F 14.33333  4.932883 
M 34.66667 10.692677 

   hight_mean    hight_sd.dev
F 35.00000  7.071068 
M 34.66667 10.692677 

3.Is there a way to make a title for each table, for example
       
                    weight
    weight_mean    weight_sd.dev 
 F 14.33333  4.932883 
M 34.66667 10.692677 



I appreciate your help,
#
Hi
Error in file(file, "r") : cannot open the connection
In addition: Warning message:
In file(file, "r") : cannot open file 'data.txt': No such file or 
directory

gives me an error so it is not possible to reproduce your function.

However it looks like work for lapply, sapply, by or similar

see e.g.
lapply(split(iris[,1:4], iris$Species), function(x) cbind(mean(x), sd(x)))
by(iris[,1:4], iris$Species, function(x) cbind(mean(x), sd(x)))

Regards
Petr


r-help-bounces at r-project.org napsal dne 21.01.2009 06:07:40:
would
height
appears
and
http://www.R-project.org/posting-guide.html