[R] How to create a numeric data.frame

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Mon Jun 13 16:45:33 CEST 2011


On Mon, Jun 13, 2011 at 11:06 AM, Aparna <aparna.sampath26 at gmail.com> wrote:
> Hi All
>
> I am new to R and  I am not sure of how this should be done. I have a matrix of
> 985x100 values and the class is data.frame.

 You don't have a 'matrix' in the R sense of the word. You seem to
have a table of numbers which are stored in an object of class
'data.frame'.

>  V2           V3           V4           V5           V6
> 2   0.009953966  -0.01586103 -0.016227028  0.016774711 -0.021342598
> 3  -0.230181145  0.203303786 -0.685321843  0.147050709 -0.122269004
> 4  -0.552905273 -0.034039644 -0.511356309 -0.330524909 -0.239088566
> 5  -0.089739322 -0.082768643 -0.411209134 -0.301011664  1.560185991
> 6  -1.986059137 -0.252217616 -0.369044526 -0.585619405  0.545903757
> 7  -1.635875161  2.741310455 -0.058411313 -1.458825827  0.078480977
> 8   0.525846706 -1.134643662 -0.067014844 -1.431990219 -0.557057121
> 9  -0.913511821  0.688374777  0.376412044 -0.861746434  2.065507172
> 10 -1.538179621  0.814330376  1.639939042  -1.41478931  1.802738289
> 11  0.817957993 -0.426560507  2.773380242 -0.123291817  1.316883748
>
>
> When I try to use this command to convert it to numeric,

 A data frame doesn't have an overall sense of itself being numeric or
character. Only the columns have that, and they can be independent.

> as.numeric(leu_cluster1): I get an error Error: (list) object cannot be coerced
> to type 'double'.

 Because a data frame is implemented as a list where each element is
the same length. Each element is a vector of numbers or characters.
You are doing the equivalent of:

 as.numeric(list(foo=c(1,2,3))

 now you may think it reasonable to do an 'as.numeric' on that, but what about:

 as.numeric(list(foo=list(bar=c(1,2,3),baz=c(34,5)),bar=c("Hello","World"))

  how would you 'as.numeric' that?

> I tried several functions and looked into other forums too,
> but could not find a solution. i am trying to change it to numeric data.frame
> and not to a matrix.

 There is no numeric data frame. There is only numeric matrix, or a
dataframe with all numeric columns. Do summary(mydataframe) to see
what class your columns all are.

Barry



More information about the R-help mailing list