Ben S. - 1 month ago 15

R Question

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...

Answer

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 row at a time. For example, like this

```
txt =
"1, 2, 3, 0.3, 2/5, 0.75, 1/3
2, 3, 1/5, 2/5, 1, 2, 3"
dat = read.table(text = txt, sep = ",", stringsAsFactors = F)
answer = t(apply(dat, 1, function(row) sapply(row, function(x) eval(parse(text = x)))))
```

Source (Stackoverflow)

Comments