So I have a data frame (called gen) filled with nucleotide information: each value is either A, C, G, or T. I am looking to replace A with 1, C with 2, G with 3, and T with 4. When I use the function gen[gen==A] = 1
, I get the error:
Error in [<-.data.frame
(*tmp*
, gen == A, value = 1) :
object 'A' not found
I even tried using gen <- replace(gen, gen == A, 1)
, but it gives me the same error. Does anyone know how to fix this error? If not, is there a package that I can install in R with a program that will convert A, C, G, and T to numeric values?
Thanks
You need to wrap A in quotes or else R looks for a variable named A. If the columns are character vectors:
also 1 way to do all at once
If the columns are factors