[R] Odp: cbind with vectors of different lengths?

Petr PIKAL petr.pikal at precheza.cz
Thu Jun 10 10:52:24 CEST 2010


Hi

you shall use na.action = "na.exclude" option in your lm call?

Regards
Petr


r-help-bounces at r-project.org napsal dne 10.06.2010 01:12:55:

> 
> Hello R help
> I have a dataframe, with 71 samples (rows) and 30 variables. I got 
linear 
> models for some of the variables,  and I want to join fitted and 
residuals of 
> these models to the data frame. Sometimes, these vectors have the same 
length 
> of the dependant variable, but in a few cases, NA values can be found on 
my 
> data, and therefore, both fitted and residuals have a few rows less than 
the 
> original data frame. As I try cbind, R answers with error, because both 
> vectors have different lenghts. I have tried with merge but... suddenly  
I had
> a lot of rows of repeated values. I think (with my small idea of R and 
the 
> manuals and helps I did read) that first, I have to force residuals and 
fitted
> of my model to be a data frame.
> >as.data.frame(fitted(lm))
> this gives, for example
> [1] 1.1
> 
> 
> [3] 3.8
> 
> 
> [4] 1.3
> 
> [5] 0.9
> 
> instead of the original fitted(lm)
> 1        3       4          5
> 1.1     3.8     1.3      0.9
> 
> 
> that I want to join to my data frame
> [1] 17.0
> [2] 15.2
> 
> 
> [3] 17.3
> 
> 
> [4] 15.0
> 
> [5] 17.4  
> as you can see, row 2 does not exist in fitted... how can I tell R to 
leave 
> this row as NA, as below?
> 
> [1] 17.0    1.1
> 
> [2] 15.2    NA
> 
> 
> 
> [3] 17.3    3.8
> 
> 
> 
> [4] 15.0    1.3
> 
> 
> [5] 17.4    0.9  
> thanks and greets.
> Arantzazu Blanco Bernardeau
> Dpto de Química Agrícola, Geología y Edafología
> Universidad de Murcia-Campus de Espinardo
> 
> _________________________________________________________________
> ¿Un navegador seguro buscando estás? ¡Protegete ya en 
www.ayudartepodria.com!
> www.ayudartepodria.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