[R] how to name the column after converting a vector to a data frame

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Mon May 3 21:12:05 CEST 2010


On Mon, May 3, 2010 at 7:21 PM, wei x1 <weix1_2007 at hotmail.com> wrote:
>
> hell all:
>
> I have a vector as follows:
>
>> head(res)
> 1007_s_at.value   1053_at.value    117_at.value    121_at.value 1255_g_at.value
>    0.225801033     0.009747404     0.709517954     0.008825740     0.496859178
>  1294_at.value
>    0.005091231
>
>
>
> after I convert the res into a data frame I got the following:
>
> resx<- data.frame(res)
>> head(resx)
>                        res
> 1007_s_at.value 0.225801033
> 1053_at.value   0.009747404
> 117_at.value    0.709517954
> 121_at.value    0.008825740
> 1255_g_at.value 0.496859178
> 1294_at.value   0.005091231
>
>
>
> as you may see, the first column of resx has no names and it does not appear as one of dimension of data frame.
>
> I wonder anyone may have solution to assign a name to it.
>

 The first column isnt a column! It's the row names. If you want to
make a column from the row names, try:

> res=c(a=1,b=2,c=3)
> resx=data.frame(res)
> resx
  res
a   1
b   2
c   3
> resx$names = rownames(resx)
> resx
  res names
a   1     a
b   2     b
c   3     c

 now it has row names as well as a column (called 'names') with them in.

Barry



More information about the R-help mailing list