Arantzazu Blanco Bernardeau
2010-Jun-09 23:12 UTC
[R] cbind with vectors of different lengths?
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
I wrote a function that cbinds vectors of different lengths. Basically, the
function adds NAs to shorter vectors and cbinds in the end. I'm attaching
the code for guidance.
# This function takes in a list() of vectors and cbinds them into a
data.frame.
timerMelt <- function(x, write.down = FALSE, filename = "output") {
stopifnot(is.list(x))
filename <- paste(filename, ".txt", sep = "")
len1 <- length(x[[1]])
len2 <- length(x[[2]])
len3 <- length(x[[3]])
max.len <- max(c(len1, len2, len3))
x.cat1 <- data.frame(rep(NA, max.len))
x.cat2 <- data.frame(rep(NA, max.len))
x.cat3 <- data.frame(rep(NA, max.len))
if (len1 < max.len) {
x.cat1[1:len1,] <- data.frame(x[[1]])
} else {
x.cat1 <- data.frame(x[[1]])
}
if (len2 < max.len) {
x.cat2[1:len2,] <- data.frame(x[[2]])
} else {
x.cat2 <- data.frame(x[[2]])
}
if (len3 < max.len) {
x.cat3[1:len3,] <- data.frame(x[[3]])
} else {
x.cat3 <- data.frame(x[[3]])
}
result <- cbind(x.cat1, x.cat2, x.cat3)
names(result) <- c("s", "p", "r")
if (write.down == TRUE) {
write.table(result, filename, row.names = FALSE)
}
return(result)
}
Cheers,
Roman
--
View this message in context:
http://r.789695.n4.nabble.com/cbind-with-vectors-of-different-lengths-tp2249680p2250025.html
Sent from the R help mailing list archive at Nabble.com.
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 gotlinear> models for some of the variables, and I want to join fitted andresiduals of> these models to the data frame. Sometimes, these vectors have the samelength> of the dependant variable, but in a few cases, NA values can be found onmy> data, and therefore, both fitted and residuals have a few rows less thanthe> original data frame. As I try cbind, R answers with error, because both > vectors have different lenghts. I have tried with merge but... suddenlyI had> a lot of rows of repeated values. I think (with my small idea of R andthe> manuals and helps I did read) that first, I have to force residuals andfitted> 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 toleave> 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 enwww.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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.