Hello R users, I have a set of data frames for which I am tallying row numbers, as shown below.> nrow(mC_Explant)[1] 14480> nrow(mC_Callus)[1] 23320> nrow(mC_RegenPlant)[1] 8108 etc. I want to create a new data frame which has the variable names as column headings, and then a single row with the nrow tallies. My first step was this: dfIntron <- c(nrow(mC_Explant), nrow(mC_Callus), nrow(mC_RegenPlant)) Then, to set the column names, I tried this, and got the following error: colnames(dfIntron) <- c("mC_Explant", "mC_Callus", "mC_RegenPlant") Error in `colnames<-`(`*tmp*`, value = c("mC_Explant", "mC_Callus", "mC_RegenPlant" : attempt to set colnames on object with less than two dimensions Even my first step seems wildly inefficient, and obviously doesn't work. There must be a simple way to do this, but I can't use table(), for example, as there are multiple data frames. Any help will be appreciated. --Kelly V.
Hi Kelly, c() creates a vector. You need data.frame() instead. dfIntron <- c(mC_Explant=nrow(mC_Explant), mC_Callus=nrow(mC_Callus), mC_RegenPlant=nrow(mC_RegenPlant)) # set colnames simultaneously. Sarah On Tue, Sep 20, 2011 at 2:23 PM, Vining, Kelly <Kelly.Vining at oregonstate.edu> wrote:> Hello R users, > I have a set of data frames for which I am tallying row numbers, as shown below. > >> nrow(mC_Explant) > [1] 14480 >> nrow(mC_Callus) > [1] 23320 >> nrow(mC_RegenPlant) > [1] 8108 > > etc. > > I want to create a new data frame which has the variable names as column headings, and then a single row with the nrow tallies. My first step was this: > > dfIntron <- c(nrow(mC_Explant), nrow(mC_Callus), nrow(mC_RegenPlant)) > > Then, to set the column names, I tried this, and got the following error: > > ?colnames(dfIntron) <- c("mC_Explant", "mC_Callus", "mC_RegenPlant") > Error in `colnames<-`(`*tmp*`, value = c("mC_Explant", "mC_Callus", "mC_RegenPlant" : > ?attempt to set colnames on object with less than two dimensions > > Even my first step seems wildly inefficient, and obviously doesn't work. There must be a simple way to do this, but I can't use table(), for example, as there are multiple data frames. > > Any help will be appreciated. > > --Kelly V. > >-- Sarah Goslee http://www.functionaldiversity.org
Hi: Using Michael's example data, here's another approach: x = data.frame(x = 1:5) y = data.frame(y = 1:10) z = data.frame(z = 1:3) # Generate a vector to name the list components nms <- c('x', 'y', 'z') # Combine data frames into a list L <- list(x, y, z) # name them names(L) <- nms # Use lapply() to get the number of rows in each component data frame as.data.frame(lapply(L, nrow)) x y z 1 5 10 3 HTH, Dennis On Tue, Sep 20, 2011 at 11:23 AM, Vining, Kelly <Kelly.Vining at oregonstate.edu> wrote:> Hello R users, > I have a set of data frames for which I am tallying row numbers, as shown below. > >> nrow(mC_Explant) > [1] 14480 >> nrow(mC_Callus) > [1] 23320 >> nrow(mC_RegenPlant) > [1] 8108 > > etc. > > I want to create a new data frame which has the variable names as column headings, and then a single row with the nrow tallies. My first step was this: > > dfIntron <- c(nrow(mC_Explant), nrow(mC_Callus), nrow(mC_RegenPlant)) > > Then, to set the column names, I tried this, and got the following error: > > ?colnames(dfIntron) <- c("mC_Explant", "mC_Callus", "mC_RegenPlant") > Error in `colnames<-`(`*tmp*`, value = c("mC_Explant", "mC_Callus", "mC_RegenPlant" : > ?attempt to set colnames on object with less than two dimensions > > Even my first step seems wildly inefficient, and obviously doesn't work. There must be a simple way to do this, but I can't use table(), for example, as there are multiple data frames. > > Any help will be appreciated. > > --Kelly V. > > > ______________________________________________ > R-help at r-project.org 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. >