[R] Using "by" to create individual variance-covariance matrices

Uwe Ligges ligges at statistik.tu-dortmund.de
Sun Sep 28 20:13:18 CEST 2008


Let me repeat an important topic from the the posting guide: it would be 
helpful for us to see a toy example, i.e. code to generate data for x.

Best wishes,
Uwe Ligges



Josh B wrote:
> Hello R list subscribers,
> 
> I am trying to use the "by" command to create line-specific variance covariance matrices (where "x" is the original data matrix):
> 
> by(x, x$line, function(d) {
> d.clean <- d[,-1]})
> write.table(d.clean$line[1,1], sep = ",", file = "covariances.csv", col.names = FALSE, row.names = FALSE, append = TRUE)
> write.table("", sep = ",", file = "covariances.csv", col.names = FALSE, row.names = FALSE, append = TRUE)
> x.cov <- cov(d.clean, y= NULL, use= "complete.obs", method= c("pearson"))
> })
> 
> When I run this code, I get an error:
> 
>> by(x, x$line, function(d) {
> + d.clean <- d[,-1]})
> Error in names(IND) <- deparse(substitute(INDICES))[1] : 
>   'names' attribute [1] must be the same length as the vector [0]
> 
> Any suggestions on how to improve my code? Thanks in advance.
> 
> Sincerely, Josh
> 
> 
>       
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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