Converting fractions to decimals in an R vector

This question already has an answer here:

  • How to perform arithmetic on values and operators expressed as strings? 1 answer

  • 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)))
    
    链接地址: http://www.djcxy.com/p/24874.html

    上一篇: R处的阈值处的整数

    下一篇: 将分数转换为R向量中的小数