[R] How to create a numeric data.frame

Joshua Wiley jwiley.psych at gmail.com
Mon Jun 13 16:06:17 CEST 2011


Hi,

If your matrix is already numeric, then:

as.data.frame(your_matrix_name)

will do the trick.  However, if you have a matrix that is not numeric
(say it is character), then you could use:

as.data.frame(as.numeric(your_matrix_name))

Matrices can only hold one class of data (for example, all numeric, or
all character, or all factor), so if *any* of your data is character
(say one column contains people's names), then the entire matrix will
be character, and calling as.numeric() on it is probably not what you
want (the character data will get screwed up).  In which case, you
might convert the matrix to a data frame first:

as.data.frame(your_matrix_name)

because data frames can contain different classes of data in their
different columns.  Once it is a data frame, you could convert the
columns that should be numeric to numeric (say, columns 2 through 6
only) by:

your_data_name[, 2:6] <- lapply(your_data_name[, 2:6], as.numeric)

For relevant documentation, see

?as.numeric
?as.data.frame
## under the "Details" section, it shows the hierarchy of data types
## that is how I could know that if there is character data, the numeric
## data will be converted up to the character class
?matrix


Hope this helps,

Josh

On Mon, Jun 13, 2011 at 3: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.
>
> A sample of my dataset looks like this (Since its a huge dataset and it would
> make the screen look more complex, I am pasting only the first few rows and
> columns.
>
>  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,
>
> as.numeric(leu_cluster1): I get an error Error: (list) object cannot be coerced
> to type 'double'. 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.
>
>
> thanks in advance. :)
>
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list