[R] Odp: problem with rbind

Petr PIKAL petr.pikal at precheza.cz
Wed Jan 21 11:07:01 CET 2009


Hi

> data<-read.table("data.txt", header=T, sep='\t')
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:

> 
> 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,
> 
> 
> -- 
> View this message in context: http://www.nabble.com/problem-with-rbind-
> tp21577241p21577241.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.



More information about the R-help mailing list