[R] Cannot get simple data.frame binding.

From: John Kane <jrkrideau_at_yahoo.ca>
Date: Tue 29 Aug 2006 - 06:08:01 EST

I am stuck on a simple problem where an example works fine but the real one does not.

I have a data.frame where I wish to sum up some values across the rows and create a new data.frame with some of old data.frame variables and the new summed variable.

It works fine in my simple example but I am doing something wrong in the real world. In the real world I am loading a labeled data.frame. The orginal data comes from a spss file imported using spss.get but the current data.frame is a subset of the orginal spss file.


cata <- c( 1,1,6,1,1,NA)
catb <- c( 1,2,3,4,5,6)
doga <- c(3,5,3,6,4, 0)
dogb <- c(2,4,6,8,10, 12)
rata <- c (NA, 9, 9, 8, 9, 8)
ratb <- c( 1,2,3,4,5,6)
bata <- c( 12, 42,NA, 45, 32, 54)
batb <- c( 13, 15, 17,19,21,23)

id <- c('a', 'b', 'b', 'c', 'a', 'b')
site <- c(1,1,4,4,1,4)
mat1 <- cbind(cata, catb, doga, dogb, rata, ratb, bata, batb)

data1 <- data.frame(site, id, mat1)
aa <- which(names(data1)=="rata")
bb <- length(names(data1))

mat1 <- as.matrix(data1[,aa:bb])
food <- apply( mat1, 1, sum , na.rm=T)

abba <- data.frame(data1[, 1:6], food)

Real life problem

> md1<-partly.corrected.materials
> aa <- which(names(md1)=="oaks")
> bb <- length(names(md1))
> # sum the values of the "other" variables
> mat1 <- as.matrix( md1[, aa:bb] )
> other <- apply(mat1,1, sum, na.rm=T)
> ire1 <- data.frame(md1[, 1:11], other)
Error in data.frame(md1[, 1:11], other) : arguments imply differing number of rows: 11, 75

I have simply worked around the problem by using ire1 <- data.frame(md1$site, md1$colour, md1$ss1 ... , other)
but I would like to know what stupid thing I am doing.


R-help@stat.math.ethz.ch 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. Received on Tue Aug 29 16:01:55 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 29 Aug 2006 - 18:24:27 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.