[R] Not able to convert data.frame to numeric properly

Frede Aakmann Tøgersen frtog at vestas.com
Wed Apr 9 08:50:10 CEST 2014


Now we are able to help you. What you see is an artefact of an object in T of class 'factor'

 
> ## So please, see ?factor
> ## first 2 columns subset of a factor
> str(a)
'data.frame':	1 obs. of  4 variables:
 $ GHP: Factor w/ 51 levels "0.0944","0.1446",..: 33
 $ GP : Factor w/ 51 levels "0.1755","0.3582",..: 29
 $ T  : num 2.9
 $ Tn : num 3.3
> 
> ## you can't do this for a dataframe with columns of factors
> ## it doesn't give you the labels of the levels as one would expect
> ## but for some reason the number of levels
> as.character(a)
[1] "33"     "29"     "2.9007" "3.2988"
> 
> ## gets the number of level (33)
> as.numeric(a[,1])
[1] 33
> ## gets the label of level number 33
> as.character(a[,1])
[1] "2.2194"
> ## gets the label as a numeric
> as.numeric(as.character(a[,1]))
[1] 2.2194
> 
> ## do this for the first 2 columns
> for (i in 1:2) a[,i] <- as.numeric(as.character(a[,i]))
> 
> ## the structure of a
> str(a)
'data.frame':	1 obs. of  4 variables:
 $ GHP: num 2.22
 $ GP : num 2.66
 $ T  : num 2.9
 $ Tn : num 3.3
>
> ## now
> min(a)
[1] 2.2194
>

Yours sincerely / Med venlig hilsen


Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender. 


> -----Original Message-----
> From: Sachinthaka Abeywardana [mailto:sachin.abeywardana at gmail.com]
> Sent: 9. april 2014 08:26
> To: Frede Aakmann Tøgersen; r-help at r-project.org
> Subject: Re: [R] Not able to convert data.frame to numeric properly
> 
> a is ofcourse a subset of data.frame, a ROW of the original table specifically.
> 
> > str(a)
> 'data.frame': 1 obs. of  4 variables:
>  $ GHP: Factor w/ 51 levels "0.0944","0.1446",..: 33
>  $ GP : Factor w/ 51 levels "0.1755","0.3582",..: 29
>  $ T  : num 2.9
>  $ Tn : num 3.3
> > dput(a)
> structure(list(GHP = structure(33L, .Label = c("0.0944", "0.1446",
> "0.2", "0.4124", "0.7601", "0.871", "0.8849", "0.9176", "1.0168",
> "1.0663", "1.0947", "1.1823", "1.1831", "1.2418", "1.2542", "1.3082",
> "1.3283", "1.3758", "1.4437", "1.4621", "1.5769", "1.6699", "1.7306",
> "1.8366", "1.8456", "1.8651", "1.8972", "1.9595", "2.0781", "2.0802",
> "2.1553", "2.2097", "2.2194", "2.3669", "2.3935", "2.5351", "2.5374",
> "2.5966", "2.6934", "2.7764", "2.9073", "6.003", "6.0825", "6.1013",
> "6.1068", "6.1675", "6.2401", "6.6001", "6.6047", "7.8665", "+AC0-27.0565"
> ), class = "factor"), GP = structure(29L, .Label = c("0.1755",
> "0.3582", "0.4155", "0.4208", "0.7388", "0.8394", "0.8853", "0.9254",
> "0.9423", "0.9695", "0.9757", "0.9793", "0.9896", "1.0422", "1.0519",
> "1.116", "1.2482", "1.2691", "1.2735", "1.2788", "1.2948", "1.3141",
> "1.3204", "1.4006", "1.5333", "1.6157", "1.9003", "2.6024", "2.6561",
> "2.7466", "2.7572", "2.8108", "2.8256", "2.9565", "2.978", "3.0665",
> "3.1155", "3.2027", "3.2123", "3.257", "3.4055", "6.0616", "6.0671",
> "6.1166", "6.2053", "6.592", "6.6734", "7.005", "7.3159", "8.5777",
> "+AC0-20.3347"), class = "factor"), T = 2.9007, Tn = 3.2988), .Names =
> c("GHP",
> "GP", "T", "Tn"), row.names = 28L, class = "data.frame")
> 
> On Wed, Apr 9, 2014 at 4:17 PM, Frede Aakmann Tøgersen
> <frtog at vestas.com> wrote:
> > We can't say because we don't know how a was created.
> >
> > Please email the output from
> >
> > str(a)
> >
> > and
> >
> > dput(a)
> >
> >
> >
> > Yours sincerely / Med venlig hilsen
> >
> >
> > Frede Aakmann Tøgersen
> > Specialist, M.Sc., Ph.D.
> > Plant Performance & Modeling
> >
> > Technology & Service Solutions
> > T +45 9730 5135
> > M +45 2547 6050
> > frtog at vestas.com
> > http://www.vestas.com
> >
> > Company reg. name: Vestas Wind Systems A/S
> > This e-mail is subject to our e-mail disclaimer statement.
> > Please refer to www.vestas.com/legal/notice
> > If you have received this e-mail in error please contact the sender.
> >
> >
> >> -----Original Message-----
> >> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org]
> >> On Behalf Of Sachinthaka Abeywardana
> >> Sent: 9. april 2014 08:11
> >> To: r-help at r-project.org
> >> Subject: [R] Not able to convert data.frame to numeric properly
> >>
> >> I have the following:
> >>
> >>
> >> >a #note that the 28 is a row.name
> >>
> >>         GHP     GP      T     Tn
> >> 28   2.2194 2.6561 2.9007 3.2988
> >>
> >> >min(as.numeric(a))
> >> 2.9007
> >> >min(as.numeric(as.character(a)))
> >> 2.9007
> >> >as.numeric(as.character(a))                 #What's going on here???
> >> [1] 33.0000 29.0000  2.9007  3.2988
> >>
> >> So basically how do I get this to show the correct value of 2.21 as my
> >> minimum value?
> >>
> >> Thanks,
> >> Sachin
> >>
> >> ______________________________________________
> >> 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.




More information about the R-help mailing list