开发者

R- converting data from fraction to decimal [duplicate]

This question already has answers here: How to perform arithmetic on values and operators expressed as strings? (2 answers) Closed 5 years ago.

Is there a simple way to convert data in a dataframe from fraction to decimal format? I have a column of data that that's been recorded as a fraction:

Levels: 1/2 1/3 1/4 1/5 1/8 2/3

Is there a quick开发者_JAVA技巧 way to convert it to .5 .333 25 .2 .125 .67?


Here's a way I've done that in the past.

> frac <- c("1/2","1/3","1/4","1/5","1/8","2/3")
> sapply(frac, function(x) eval(parse(text=x)))
      1/2       1/3       1/4       1/5       1/8       2/3 
0.5000000 0.3333333 0.2500000 0.2000000 0.1250000 0.6666667 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜