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?



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.


If you've never heard of a reprex before, you might want to start by reading the 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 = "")

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


@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 :blush:
@jrazak, it looks as your data are in a data.frame, did you remember to prefix the column names? I.e.
MyDataFrame$V8 instead of V8 etc.

@jcblum, aside the sign difference. The formula didnt work.

@nwerth, you are right. It could be because of the way I read the txt file into R. This was the command I used to read the txt file (RStudio--->File--->Import Dataset--->From Text (base)...). How do I read txt file (.DAT.Rdata) into R? What code should I use instead?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.