# How to compute the value for factor variables?

I have a data with factor variables. How do I compute using this formula below using column V8 and V9?

M=log2((V8+1)/(V9+1))

Could you please turn this into a self-contained reprex (short for reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

``````install.packages("reprex")
``````

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

``````reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")
``````

For pointers specific to the community site, check out the reprex FAQ.

1 Like

It is not clear which of your columns are factors, but assuming that V8 and V9 are factors I would use something like

``````M = log2((as.numeric(as.character(V8))-1)/(as.numeric(as.character(V9))-1))
``````

Cheers
Steen

@stkrog, V8 and V9 are factors.

If it's possible, the best solution to this is to fix how the data's read into R. `V8` and `V9` are supposed to be numeric, so them being factors anywhere is a problem. When read by base R functions, columns end up as factors if the `read.*()` function thinks they're strings. So something's telling R those aren't numbers. This could be quotation marks around the values in the text file, or you might've set the `colClasses` argument as `"character"` for those columns.

How are you reading this data in? Is it from a text-based file (e.g., CSV or JSON)? If so, could you share the first couple rows from the file?

Hi @jrazak!

I think thereâ€™s a slight difference between @stkrogâ€™s code and your formula above: minus 1 instead of plus 1. Is that what you mean by â€śit didnâ€™t workâ€ť? Or something else?

In general, â€śit didnâ€™t workâ€ť isnâ€™t a very useful response for people who are trying to help, and can come across as somewhat rude. Please explain what didnâ€™t work, or what unexpected result you got.

Oh, you're right, sorry about that. I guess I was focusing on the conversion of the factors
@jrazak, it looks as your data are in a data.frame, did you remember to prefix the column names? I.e.