2

This seems to be the easiest thing to do - I've really tried everything that I have found and nothing has worked well for me.

I have a very simple vector of fractions like this:

x = c(1/2,5/2,7/2)

I want to convert the values in this vector to decimals.

I tried what I found in this question, but it converted my fractions into simple numbers that had been rounded like c(1,3,4)

I also tried a few libraries and same result...

One thing that I should mention is that my vector is a factor, but if I convert it to numeric or integer it converts the values as well into rounded numbers!

EDIT 1:

I'm not sure what's different about my particular vector. When I run the example vector I have in the question everything works fine. It actually auto evaluates it. I got my vector from a csv file but it doesn't seem like a character vector since the fractions are not in quotes when printing it.

EDIT 2:

It seems like the csv is treating the column as a date, but when I try to turn it into a number form manually within the csv, it doesn't really convert into the result of the fraction.

EDIT 3:

It seems like the example I provided does not completely represent my original data. If you would like to try a small sample of the real data you may download the csv here. Also, a dput of my data:

structure(c(14L, 13L, 4L, 5L, 8L, 7L, 3L, 8L, 11L, 1L), .Label = c("10/1", 
"12/1", "15/1", "2/1", "3/1", "4/1", "5/1", "5/2", "6/1", "7/1", 
"7/2", "8/1", "8/5", "9/2"), class = "factor")
Community
  • 1
  • 1
jgozal
  • 1,480
  • 6
  • 22
  • 43
  • 1
    Do you have a character vector? – akrun Dec 05 '15 at 16:57
  • Can you show us the code your using already to get those rounded numbers? – Hyden Dec 05 '15 at 16:58
  • The solution from http://stackoverflow.com/a/3480257 works fine. – John_West Dec 05 '15 at 16:59
  • You need to convert to character first. Many duplicates of that – Pierre L Dec 05 '15 at 17:00
  • @John_West By the way, I'd already posted my answer before your comment popped up (I didn't just copy your comment). – Hyden Dec 05 '15 at 17:01
  • I'm not sure what's different about my particular vector. When I run the example vector I have in the question everything works fine. It actually auto evaluates it. I got my vector from a csv file but it doesn't seem like a character vector since the fractions are not in quotes when printing it – jgozal Dec 05 '15 at 17:01
  • I am not sure about the problem. But, you can get the fractions by `library(MASS); fractions(0.5)` – akrun Dec 05 '15 at 17:02
  • @Hyden - no problem :) the link I quoted stems from OP. – John_West Dec 05 '15 at 17:04
  • When you read in the csv file. add `read.csv(file, stringsAsFactors=FALSE)` – Pierre L Dec 05 '15 at 17:04
  • @PierreLafortune will try that right now – jgozal Dec 05 '15 at 17:07
  • It seems like the csv is treating the fractions as a date. @PierreLafortune, tried your small solution but the problem still remains :( – jgozal Dec 05 '15 at 17:18
  • I am importing the data from the csv with a `fileEncoding = 'UTF-16LE'` Could that be the problem? – jgozal Dec 05 '15 at 17:35
  • I just tested it and it works. Hyden's and John's answers both work also. This is problem has taken longer than it had to because you provided an example that did not match your data. – Pierre L Dec 05 '15 at 17:38
  • I apologize @PierreLafortune. I'm really not sure of how to reproduce the data that I have. I know this shouldn't been done but I'm going to provide a link to a very small sample size of my data in a csv format in case that helps. Will be in the question in 5 min – jgozal Dec 05 '15 at 17:44
  • It's okay you can add `dput(head(df$col, 10))` where `df` is the name of your data frame and `col` is the name of the column in question. – Pierre L Dec 05 '15 at 17:47
  • ok - I just updated the question but I'll do that too @PierreLafortune – jgozal Dec 05 '15 at 17:49
  • The fractions in the CSV file are being read as dates. You have to write them as mixed numbers. It can't be 1/2,5/2,7/2, it must be 1/2,2 1/2,3 1/2. – Hyden Dec 05 '15 at 17:49
  • `structure(c(14L, 13L, 4L, 5L, 8L, 7L, 3L, 8L, 11L, 1L), .Label = c("10/1", "12/1", "15/1", "2/1", "3/1", "4/1", "5/1", "5/2", "6/1", "7/1", "7/2", "8/1", "8/5", "9/2"), class = "factor")` – jgozal Dec 05 '15 at 17:50
  • @Hyden is there any way to do it programatically? (I have to leave soon but I'll be back in a few hours to read the response) – jgozal Dec 05 '15 at 17:53
  • @jgozal Do you want to know how to turn the CSV file into mixed fractions? That's for another question. – Hyden Dec 05 '15 at 18:02
  • That `dput` is from your data? Why are we still going over this. I just tried `frac <- as.character(x)` and then the `sapply` function from the link and it works fine using the data you just provided. Please add the dput to your question and post the results of `as.character(x)`. – Pierre L Dec 05 '15 at 18:02
  • Dear jgozal, you have values and labels that differ. @Hyden and my previous solutions work with values. See update of my answer. Also see http://stackoverflow.com/a/33038447 – John_West Dec 05 '15 at 18:31
  • @jgozal Please accept an answer that you believe is correct, also if you have further questions concerning turning the CVS file into mixed fractions then please create a new question. – Hyden Dec 05 '15 at 18:47
  • 1
    @PierreLafortune - doing as.character() and then putting it into sapply worked. – jgozal Dec 05 '15 at 19:12
  • @Hyden, please add the as.character() of x and I will accept the answer – jgozal Dec 05 '15 at 19:13

1 Answers1

2

Finally found that solution:

x = c(1/2,5/2,7/2)
frac <- factor(x)
as.numeric(levels(frac))[frac]

Works with x = c("1/2","5/2","7/2") too

The other problem is that you have labels that differ from values You want to convert labels to decimal values. Then use

frac <- structure(c(14L, 13L, 4L, 5L, 8L, 7L, 3L, 8L, 11L, 1L), .Label = c("10/1", "12/1", "15/1", "2/1", "3/1", "4/1", "5/1", "5/2", "6/1", "7/1", "7/2", "8/1", "8/5", "9/2"), class = "factor")
labl =attributes(frac)[1]
sapply(as.character(unlist(labl)), function(x) eval(parse(text=x)))
Community
  • 1
  • 1
John_West
  • 2,239
  • 4
  • 24
  • 44
  • I thought he wanted to read from a .csv file? – Hyden Dec 05 '15 at 17:36
  • Hyden's answer already solves it. This is just a variation. – Pierre L Dec 05 '15 at 17:37
  • I get: `NA NA NA NA NA Warning message: NAs introduced by coercion` :/ – jgozal Dec 05 '15 at 17:38
  • @Pierre Lafortune Solves with numeric/character - it does not use factor variable type – John_West Dec 05 '15 at 17:39
  • @Pierre Lafortune Your edit produces additional string on output ;) – John_West Dec 05 '15 at 17:41
  • The OP is posting what they "think" their data is. This type of question asking happens all the time. Instead of posting the actual structure of the data, they post a "similar" example and then wonder why the solutions do not work. – Pierre L Dec 05 '15 at 17:41
  • @John_West Ah, got it. The csv files are read as dates, 1/2 is fine, however 5/2 isn't. It must be written as mixed fractions so 2 1/2. – Hyden Dec 05 '15 at 17:43
  • hey I did say in EDIT2 that the values in the column were being treated as dates! Sorry if that was not in the original question :( – jgozal Dec 05 '15 at 17:57
  • @jgozal I did understand that. I was trying to explain why R was reading them as dates and how to stop R from doing that. – Hyden Dec 05 '15 at 17:59