[R] Cannot get simple data.frame binding.

Tony Plate tplate at acm.org
Mon Aug 28 22:47:11 CEST 2006


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).

-- 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