[R] Cannot get simple data.frame binding.

John Kane jrkrideau at yahoo.ca
Tue Aug 29 00:30:05 CEST 2006


--- Tony Plate <tplate at acm.org> wrote:

> Maybe I'm missing something, but your "Real life
> code" looks like it 
> should work.  What happens when you do:
> 
>  > ire1 <- data.frame(md1[, 1:11], other)
> Error in data.frame(md1[, 1:11], other) : arguments
> imply differing number of rows: 11, 75
>  > str(md1[, 1:11])
>  > str(other)
> 
> ?
> 
> Maybe the labelled data frame is causing the
> problem?  Did you try 
> as.data.frame(md1[,1:11])? (I'm guessing that will
> strip off extra 
> attributes).
> 

I seem to have gotten it to work.  I had given up, and
simply coded it using md1$x1 etc.  

I was using an intermediate data.frame that I had
saved as an Rdata object. Once I got it working with
the md1$1 approach I dropped the intermediate object
and just used the orignal data.frame that I had
loaded.

I  made a copy of the file to run your suggestions and
when I ran the program the thing worked!  I must have
done something in saving the data.frame and reloading
but I have no idea what. since I simply said  dat1 <-
df1 and then saved dat1.

Thanks a lot. I was going a bit nuts on this.

> -- Tony Plate
> 
> John Kane wrote:
> > 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
> > 
> > ______________________________________________
> > R-help at 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.
> > 
> 
>



More information about the R-help mailing list