[R] Cannot get simple data.frame binding.
John Kane
jrkrideau at yahoo.ca
Mon Aug 28 22:08:01 CEST 2006
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.
EXAMPLE
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)
attach(data1)
data1
aa <- which(names(data1)=="rata")
bb <- length(names(data1))
mat1 <- as.matrix(data1[,aa:bb])
food <- apply( mat1, 1, sum , na.rm=T)
food
abba <- data.frame(data1[, 1:6], food)
abba
----------------------------------
Real life problem
>load("C:/start/R.objects/partly.corrected.materials.Rdata")
> 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.
Thanks
More information about the R-help
mailing list