[R] cbind with vectors of different lengths?
Arantzazu Blanco Bernardeau
aramucia at hotmail.com
Thu Jun 10 01:12:55 CEST 2010
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
More information about the R-help
mailing list