[R] How to create a numeric data.frame

Patrizio Frederic frederic.patrizio at gmail.com
Mon Jun 13 23:11:36 CEST 2011


On Mon, Jun 13, 2011 at 6:47 PM, Aparna <aparna.sampath26 at gmail.com> wrote:
> Hi Joshua
>
> While looking at the data, all the values seem to be in numeric. As i mentioned,
> the dataset is already in data.frame.
>
> As suggested, I used str(mydata) and got the following result:
>
>
> str(leu_cluster1)
> 'data.frame':   984 obs. of  100 variables:
>  $ V2  : Factor w/ 986 levels "-0.00257361",..: 543 116 252 54 520 ...

your data columns are not numeric but factors indeed.
you may try this one

a <- as.character(rnorm(100))		# some numeric data
adf <- data.frame(matrix(a,10))		# which are misinterpreted as factors
adf
adf[,1]
class(adf[,1]) # check for the class of the first column
sapply(adf,function(x)class(x)) # check classes for all columns

b <- sapply(adf,function(x)as.numeric(as.character(x))) #
as.character: use levels literally, as.numeric: transforms in numbers
b # look at b

class(b) # which is now a numeric matrix

best regards

PF

-- 
+-----------------------------------------------------------------------
| Patrizio Frederic,
| http://www.economia.unimore.it/frederic_patrizio/
+-----------------------------------------------------------------------



More information about the R-help mailing list