Take the 2-minute tour ×
Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. It's 100% free, no registration required.

so we have a0:

>a0[1,1:2]
l11      l12
-0.921 0.389593

Then;

> is.numeric(a0[1,1:2])
[1] FALSE

Ok, the text file containing them is a bit of a mess. Then:

> as.numeric(a0[1,1:2])
[1] 131   3

I know there was a trick to solve that. I just can't remember what it was...

EDIT: sample file:

 -0.921      0.389593    0.99998742210431    -0.00501553917135373    0.999984216926007   -0.00561835375178836   1   2.36    10  2   0.05    1   1
 0.923842    0.382812    0.999998286086073   -0.00185143860707771    0.999999995689246   9.28520798418186e-05   0   2.03    10  2   0.05    2   1
     -0.82904    0.559261    0.905909593351804   -0.423471142668741      0.899659482046882   -0.436592277031025     1   1.51    10  2   0.05    3   1
 -0.796621   0.604487    0.852012998984401   -0.523520629547306      0.882445298428051   -0.470415024507325     0   2.53    10  2   0.05    4   1
 0.836046    0.54875     0.9211906384523     0.389111561930306   0.926385125966013   0.376577479928014  1   2.66    10  2   0.05    5   1
 -0.873104   -0.487579   0.942147732871282   0.335197925777448   0.957896161639222   0.287114861191206  0   2.5 10  2   0.05    6   1
 -0.728606   0.684948    0.838364634070403   -0.545109842453794      0.770224459340949   -0.637772908042465     1   3.97    10  2   0.05    7   1
 0.759379    -0.650765   0.842087096982091   -0.539341562552223      0.765143566165036   -0.643859707666391     0   1.13    10  2   0.05    8   1
 -0.497911   -0.867269   0.480229829449393   0.877142697003747   0.225403470821452   0.974265505569012  1   0.62    10  2   0.05    9   1
 0.465581    0.885042    0.173457702239973   0.98484131997679    0.159038890626454   0.987272318698497  0   0.79    10  2   0.05    10  1
 -0.772559   -0.634978   0.866527018491628   0.499130169619119   0.859460127819      0.511202786269155  1   19.53   10  2   0.05    11  1
 0.81446     0.580278    0.943553667636478   0.331219679804432   0.895967382559323   0.444119859260758  0   1.73    10  2   0.05    12  1
 -0.792377   0.610095    0.865518659616982   -0.500876681284748      0.877697131009408   -0.479215761654241     1   1.24    10  2   0.05    13  1
 0.844213    -0.536081   0.899692596172177   -0.436524034152723      0.906010964105467   -0.423254217841573     0   2.02    10  2   0.05    14  1
 0.421542    0.906835    0.103422987236111   0.99463746446188    0.102255903720432   0.994758126458044  1   0.47    10  2   0.05    15  1
 0.409305    0.912408    0.0729480114866381      0.997335744681873   0.0838704194992194      0.996476669437386  0   0.45    10  2   0.05    16  1
 -0.664573   -0.747275   0.58764731149828    0.809117196263214   0.603464234608522   0.797390066120936  1   0.85    10  2   0.05    17  1
 0.599777    0.800191    0.524086235602366   0.851665202795172   0.567148160033905   0.823615786984536  0   1.32    10  2   0.05    18  1
 -0.397025   -0.917846   0.030139624142486   0.999545698333273   0.0311215453692834      0.999515607388813  1   0.46    10  2   0.05    19  1
 -0.393222   -0.919468   0.0278258557873373      0.999612785907474   0.0265821201726647      0.999646633009448  0   0.42    10  2   0.05    20  1
 0.772559    0.634978    0.924006045897534   0.38237785911949    0.908075241643739   0.418807062397072  1   8.12    20  2   0.05    1   2
 -0.807518   -0.589907   0.947419893700908   0.319993039017663   0.96499482496042    0.262268922672146  0   5.8 20  2   0.05    2   2
 V1      V2      V3      V4      V5      V6     NA  NA  NA  NA  NA  NA  NA
 -0.526762   0.850092    -0.311928648717808      0.95010552998553    -0.316412744506619      0.94862161851488   0   3.25    20  2   0.05    4   2
 -0.5161     -0.856543   0.309033921455991   0.951051016186583   0.248027721702134   0.96875293510123   1   1.82    20  2   0.05    5   2
 0.491867    0.870721    0.268019541588267   0.96341347578639    0.167808915882198   0.98581954116889   0   2.69    20  2   0.05    6   2
 0.58991     0.807579    0.51533762936385    0.856987238972464   0.421665311535527   0.906751545379244  1   1.91    20  2   0.05    7   2
 -0.549902   -0.835295   0.442022781946357   0.897003823983155   0.390584116545383   0.920567242466547  0   2.06    20  2   0.05    8   2
 0.800218    -0.599709   0.852476180705526   -0.522766067500292      0.833708092721497   -0.552205411174757     1   47.26   20  2   0.05    9   2
 0.837387    -0.546652   0.910736035285173   -0.41298895146607   0.863018073213623   -0.505173044912974     0   5.36    20  2   0.05    10  2
 0.46345     0.886149    0.179828914086857   0.98369790162343    0.176294786076906   0.984337415931193  1   1.84    20  2   0.05    11  2
 V1      V2      V3      V4      V5      V6     NA  NA  NA  NA  NA  NA  NA
     0.759379    0.650765    0.817799312011448   0.575503505874294   0.824911507994246   0.565261889727814  1   7.7 20  2   0.05    13  2
 0.793956    0.608013    0.822124323319108   0.56930799836916    0.812590005961236   0.582835724893317  0   3.8 20  2   0.05    14  2
 0.706746    0.707516    0.70298536741427    0.711204311855196   0.695840302062382   0.718196542755348  1   5.1 20  2   0.05    15  2
 0.663932    0.747827    0.717210127099939   0.696856967809958   0.711000892677057   0.70319110532801   0   4.17    20  2   0.05    16  2
 0.813568    -0.581494   0.904931790793546   -0.425556640191628      0.912655929947496   -0.408728704071137     1   4.24    20  2   0.05    17  2
 -0.836046   0.54875     0.924852673074549   -0.380325561995065      0.92915112499605    -0.369700131077304     0   3.78    20  2   0.05    18  2
 V1      V2      V3      V4      V5      V6     NA  NA  NA  NA  NA  NA  NA
 0.586824    -0.809738   -0.610122851736008      0.792306825535108   -0.588615757355873      0.80841294533943   0   3.07    20  2   0.05    20  2
share|improve this question
1  
What is the question exactly? You want (-0.921,0.389593) as a numeric vector? –  chl Nov 11 '10 at 9:32
    
yes (thanks again) –  user603 Nov 11 '10 at 11:44
add comment

2 Answers 2

up vote 3 down vote accepted

This is because you have read the numbers as factors; if you use read.table, try header=T or restructure the data before read. Some sample of the file should be helpful to resolve it.

Workaround would be to first convert factors to strings using as.character and then back to numbers with as.numeric.

Edit: Code working for provided example:

readLines('<filename>')->z
read.table(textConnection(z[-grep('V',z)]))
share|improve this answer
    
@mbq Or use the colClasses argument in read.table()? –  chl Nov 11 '10 at 9:36
1  
@chl Possibly; depends on how this data look. –  mbq Nov 11 '10 at 9:38
    
"This is because you have read the numbers as factors; if you use read.table, try header=T or restructure the data before read. Some sample of the file should be helpful to resolve it." nop. "Workaround would be to first convert factors to strings using as.character and then back to numbers with as.numeric." nop. same result. –  user603 Nov 11 '10 at 9:40
2  
@kwak -- remove those "V1 V2..." from the file before doing anything. as.numeric won't work having unconvertable elements in vector. –  mbq Nov 11 '10 at 10:02
    
@mbq:> that did the trick. I was expecting something else. –  user603 Nov 11 '10 at 10:19
show 1 more comment

Would that help?

> a <- as.data.frame(matrix(scan("1.txt", what="character", 
                                 na.strings=c("NA",paste("V",1:6,sep=""))), 
                     nc=13, byrow=T))
> class(a[,1])
[1] "factor"
> for (i in 1:ncol(a)) a[,i] <- as.numeric(as.character(a[,i]))
> class(a[,1])
[1] "numeric"
> summary(a)  # should work here

The way you import data doesn't matter so much; I think the critical part if to convert value as character then as numeric (this allows to convert levels of a factor to their numerical counterparts).

share|improve this answer
2  
you can use data.matrix() for that stat.ethz.ch/R-manual/R-devel/library/base/html/… –  user603 Nov 11 '10 at 11:43
    
@kwak Thanks. Never used that function. –  chl Nov 11 '10 at 12:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.