将分数转换为R向量中的小数
这个问题在这里已经有了答案:
终于找到解决办法:
x = c(1/2,5/2,7/2)
frac <- factor(x)
as.numeric(levels(frac))[frac]
同样适用于x = c("1/2","5/2","7/2")
另一个问题是,您的标签与您想要将标签转换为十进制值的值不同。 然后使用
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/24873.html
上一篇: Converting fractions to decimals in an R vector
下一篇: Dynamically adding values to dynamically created vectors