[R] How to transfer variable names to column names?
djmuser at gmail.com
Wed Sep 21 05:58:03 CEST 2011
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
x y z
1 5 10 3
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.
>  14480
>  23320
>  8108
> 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
> 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