Search code examples
rcsvfractions

Reading fractions in csv file with R


I have a text file of numerical data, with headers, where some numbers are entered as fractions, some are entered as integers, and some are entered as floats, e.g.:

col1name, col2name, col3name, col4name    
1, 2, 3, 4
0.5, 0.6, 0.7, 0.8
1/2, 2/3, 3/4, 4/5
1, 0.2, 3/3, 4

When I use read.csv, how do I have these expressions evaluated and stored as numbers?

Thanks...


Solution

  • First, import your data as a vector of character strings. Using your toy example in the question we can do this by

    txt = "1, 2, 3, 0.3, 2/5, 0.75, 1/3"
    dat = read.table(text = txt, sep = ",", stringsAsFactors = F)
    

    Once you have your data in a character vector, we can use eval(parse()) to evaluate the expressions as if they had been typed in at the console. Unfortunately eval is not vectorised, so we wrap it in sapply, to apply this function to each element of your data in turn

    answer = sapply(dat, function(x) eval(parse(text = x)))
    

    We can extend this to deal with multirow data by applying the above method to each column at a time. For example, like this

    txt = "col1name, col2name, col3name, col4name
    1, 2, 3, 4
    0.5, 0.6, 0.7, 0.8
    1/2, 2/3, 3/4, 4/5
    1, 0.2, 3/3, 4"
    
    dat = read.table(text = txt, sep = ",", stringsAsFactors = F, header = T)
    answer = apply(dat, 2, function(this.col) sapply(this.col, function(x) eval(parse(text = x))))
    #      col1name  col2name col3name col4name
    # [1,]      1.0 2.0000000     3.00      4.0
    # [2,]      0.5 0.6000000     0.70      0.8
    # [3,]      0.5 0.6666667     0.75      0.8
    # [4,]      1.0 0.2000000     1.00      4.0