hello
could you give me a hint to convert data in factor type to numeric (float) ? regards -- Open WebMail Project (http://openwebmail.org) -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. ______________________________________________ [hidden email] 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. |
check the following:
http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-do-I-convert-factors-to-numeric_003f Best, Dimitris Ahmet Temiz wrote: > hello > > could you give me a hint to convert data in factor type to numeric (float) ? > > regards > > -- > Open WebMail Project (http://openwebmail.org) > > -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 ______________________________________________ [hidden email] 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. |
In reply to this post by orkun
On 01/13/2010 10:47 AM, Ahmet Temiz wrote:
> hello > > could you give me a hint to convert data in factor type to numeric (float) ? > > regards > > -- > Open WebMail Project (http://openwebmail.org) > > if this will work. How did you end up with a factor (e.g. through import)? Stephan ______________________________________________ [hidden email] 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. |
S Devriese wrote:
> On 01/13/2010 10:47 AM, Ahmet Temiz wrote: >> hello >> >> could you give me a hint to convert data in factor type to numeric (float) ? >> >> regards >> >> -- >> Open WebMail Project (http://openwebmail.org) >> >> > you could try as.numeric but without more details it is difficult to see > if this will work. How did you end up with a factor (e.g. through import)? > But the question of how you got the factor data is good; you can usually avoid getting factors to begin with. -Peter Ehlers > Stephan > > ______________________________________________ > [hidden email] 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. > > -- Peter Ehlers University of Calgary 403.202.3921 ______________________________________________ [hidden email] 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. |
Hello,
I find a way to convert data in factor type to numeric : data_numeric <- as.numeric(as.character(data_factor)). It's treaky but works. Peter Ehlers a écrit : > S Devriese wrote: >> On 01/13/2010 10:47 AM, Ahmet Temiz wrote: >>> hello >>> >>> could you give me a hint to convert data in factor type to numeric >>> (float) ? >>> >>> regards >>> >>> -- >>> Open WebMail Project (http://openwebmail.org) >>> >>> >> you could try as.numeric but without more details it is difficult to see >> if this will work. How did you end up with a factor (e.g. through >> import)? >> > No, don't use as.numeric(). Do follow Dimitris' advice. > But the question of how you got the factor data is good; you > can usually avoid getting factors to begin with. > > -Peter Ehlers > >> Stephan >> >> ______________________________________________ >> [hidden email] 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. >> >> > -- Nathalie YAUSCHEW-RAGUENES Ph.D Student Unité de Recherches Ecologie Fonctionnelle et Physique de l'Environnement (EPHYSE) ______________________________________________ [hidden email] 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. |
In reply to this post by Peter Ehlers
On 01/13/2010 05:41 PM, Peter Ehlers wrote:
> S Devriese wrote: >> On 01/13/2010 10:47 AM, Ahmet Temiz wrote: >>> hello >>> >>> could you give me a hint to convert data in factor type to numeric >>> (float) ? >>> >>> regards >>> >>> -- >>> Open WebMail Project (http://openwebmail.org) >>> >>> >> you could try as.numeric but without more details it is difficult to see >> if this will work. How did you end up with a factor (e.g. through >> import)? >> > No, don't use as.numeric(). Do follow Dimitris' advice. > But the question of how you got the factor data is good; you > can usually avoid getting factors to begin with. > > -Peter Ehlers > >> Stephan >> >> ______________________________________________ >> [hidden email] 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. >> >> > I know, slightly sloppy answer (see Dimitri's answer), but I hoped to find out how he got the factor in the first place, because if it is an import issue (and e,g. decimal character is different from the locale decimal character) the FAQ answer might not work as expected. Stephan ______________________________________________ [hidden email] 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. |
In reply to this post by Nathalie Yauschew-Raguenes
On 14/01/2010, at 6:00 AM, Nathalie Yauschew-Raguenes wrote: > Hello, > > I find a way to convert data in factor type to numeric : > data_numeric <- as.numeric(as.character(data_factor)). > It's treaky but works. Possibly even more ``treaky'' but more efficient is: data_numeric <- as.numeric(levels(data_factor)[data_factor]) as has been pointed out quite a few times on this list. cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}} ______________________________________________ [hidden email] 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. |
In reply to this post by D. Rizopoulos
I have yet another novice 'converting factors to numeric problem'.
The problem is as described by others: read data (using read.csv) in from excel.csv file and it turns out to be a factor in R. The data in execl only has 4 rows: treat absorb 0 1.8052 0.5 2.07075 1.0 2.2345 I read the object in as s1 and generated the following r-code and output trying a couple of things: as.numeric(as.character(s1)) [1] NA Warning: > sapply(s1, class) treat.absorb "factor" > as.numeric(as.character(s1)) [1] NA Warining: NAs produced by conversion > as.numeric(as.character(s1$absorb)) numeric(0) > s1$absorb <- as.numeric(as.character(s1$absorb)) Error in `$<-.data.frame`(`*tmp*`, "absorb", value = numeric(0)) : replacement has 0 rows, data has 3 thanks for your consideration Andreas |
If you read the data as posted in your email with read.csv, you
probably got this: > s1 <- read.csv(textConnection("treat absorb + 0 1.8052 + 0.5 2.07075 + 1.0 2.2345")) > closeAllConnections() > s1 treat...absorb 1 0 1.8052 2 0.5 2.07075 3 1.0 2.2345 > str(s1) 'data.frame': 3 obs. of 1 variable: $ treat...absorb: Factor w/ 3 levels "0 1.8052",..: 1 2 3 > Do an str(s1) on your object to see what it say. You data did not have a comma as a separator and your: > sapply(s1, class) treat.absorb "factor" did show a funny column name (treat.absorb), so check your data and post back what the str said about it. What you probably wanted to do with the data is the following: > s1 <- read.table(textConnection("treat absorb + 0 1.8052 + 0.5 2.07075 + 1.0 2.2345"), header = TRUE) > closeAllConnections() > str(s1) 'data.frame': 3 obs. of 2 variables: $ treat : num 0 0.5 1 $ absorb: num 1.81 2.07 2.23 > s1 treat absorb 1 0.0 1.80520 2 0.5 2.07075 3 1.0 2.23450 > now you probably have the right structure to process. On Thu, Oct 14, 2010 at 9:53 PM, andreas <[hidden email]> wrote: > > I have yet another novice 'converting factors to numeric problem'. > > The problem is as described by others: read data (using read.csv) in from > excel.csv file and it turns out to be a factor in R. The data in execl only > has 4 rows: > > treat absorb > 0 1.8052 > 0.5 2.07075 > 1.0 2.2345 > > I read the object in as s1 and generated the following r-code and output > trying a couple of things: > > > > as.numeric(as.character(s1)) > [1] NA > Warning: >> sapply(s1, class) > treat.absorb > "factor" > >> as.numeric(as.character(s1)) > [1] NA > Warining: > NAs produced by conversion > >> as.numeric(as.character(s1$absorb)) > numeric(0) > >> s1$absorb <- as.numeric(as.character(s1$absorb)) > Error in `$<-.data.frame`(`*tmp*`, "absorb", value = numeric(0)) : > replacement has 0 rows, data has 3 > > thanks for your consideration > > Andreas > -- > View this message in context: http://r.789695.n4.nabble.com/convert-factor-data-to-numeric-tp1012855p2996406.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? ______________________________________________ [hidden email] 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. |
cheers for the quick reply.
Sorry I am getting a bit confused I have never used the anything else any from of extension on read.csv what I did was: 1. enter the data into excel and save it as comma delimited 2. then used: s1 <- read.csv("book1.csv") If i try to run this s1 <- read.table(textConnection("treat absorb + 0 1.8052 + 0.5 2.07075 + 1.0 2.2345"), header = TRUE) it comes with up with the following: > source(.trPaths[7], echo=TRUE, max.deparse.length=150) Fehler in source(.trPaths[7], echo = TRUE, max.deparse.length = 150) : Objekt '.trPaths' nicht gefunden the way i have done is is the following > s1 <- read.csv("Book1.csv") > head(s1) treat.absorb 1 0;1.8052 2 0.5;2.07075 3 1;2.2345 > str(s1) 'data.frame': 3 obs. of 1 variable: $ treat.absorb: Factor w/ 3 levels "0.5;2.07075",..: 2 1 3 hope that gives u a better insight |
On Thu, Oct 14, 2010 at 8:41 PM, andreas <[hidden email]> wrote:
> > cheers for the quick reply. > > Sorry I am getting a bit confused I have never used the anything else any > from of extension on read.csv what I did was: > > 1. enter the data into excel and save it as comma delimited > 2. then used: s1 <- read.csv("book1.csv") > > If i try to run this > > s1 <- read.table(textConnection("treat absorb > + 0 1.8052 > + 0.5 2.07075 > + 1.0 2.2345"), header = TRUE) > > it comes with up with the following: > >> source(.trPaths[7], echo=TRUE, max.deparse.length=150) > Fehler in source(.trPaths[7], echo = TRUE, max.deparse.length = 150) : > Objekt '.trPaths' nicht gefunden > > the way i have done is is the following > >> s1 <- read.csv("Book1.csv") >> head(s1) > treat.absorb > 1 0;1.8052 > 2 0.5;2.07075 > 3 1;2.2345 >> str(s1) > 'data.frame': 3 obs. of 1 variable: > $ treat.absorb: Factor w/ 3 levels "0.5;2.07075",..: 2 1 3 The semicolons ";" suggest your data may not actually (or only) be comma separated. Does this produce any better results? read.table("Book1.csv", sep = ";", stringsAsFactors = FALSE) --Josh > > hope that gives u a better insight > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/convert-factor-data-to-numeric-tp1012855p2996471.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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/ ______________________________________________ [hidden email] 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. |
cheers mate that worked fine
thanks a bunch |
I had exactly the same problem with trying to import another .csv file. Turns out that I was working on a german computer that instead of using a comma when I saved it as .csv used a semicolon. Just saved it as a normal excel file, put it on a mates computer and saved it as .csv Worked a treat..
|
On Sat, 2010-10-16 at 16:16 -0700, andreas wrote:
> I had exactly the same problem with trying to import another .csv file. Turns > out that I was working on a german computer that instead of using a comma > when I saved it as .csv used a semicolon. Just saved it as a normal excel > file, put it on a mates computer and saved it as .csv Worked a treat.. I no longer have the earlier messages in this thread, but isn't the above what read.csv2() was designed to overcome? And you could always cook your own with read.table() and specify the separator and decimal for example if neither read.csv nor read.csv2 were exactly what you wanted. HTH G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% ______________________________________________ [hidden email] 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. |
Free forum by Nabble | Edit this page |