[R] How to transfer variable names to column names?

Sarah Goslee sarah.goslee at gmail.com
Tue Sep 20 20:28:11 CEST 2011


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



More information about the R-help mailing list