Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

r - Converting letters to numbers in entire dataframe

I am having trouble applying the chartr() function on all columns of my data frame for converting letters to numbers.

I managed doing it on single columns, yet I want to be able to do it on the entire data frame. Here is an example of my data:

ID = c(1,2,3)
POS1 = c('AG','GC','TT')
POS2 = c('GT','CC','TC')
POS3 = c('GG','CT','AT')
DF = data.frame(ID,POS1,POS2,POS3)

DF$POS1X <- chartr('ACGT','1234',DF$POS1)

  ID POS1 POS2 POS3 POS1X
1  1   AG   GT   GG    13
2  2   GC   CC   CT    32
3  3   TT   TC   AT    44

As seen from the code, I want to convert A to 1, C to 2, G to 3, and T to 4. I have 40+ columns and thus repeating the same command as above 40+ times would be impractical (especially if I run into the same problem later on with say hundreds of columns)

Sincerily, ykl

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Why not use lapply?

DF2 <- DF ## to not overwrite the original DF
DF2[-1] <- lapply(DF2[-1], chartr, old = "ACGT", new = "1234")
DF2
#   ID POS1 POS2 POS3
# 1  1   13   34   33
# 2  2   32   22   24
# 3  3   44   42   14

Now you have two data frames with equivalent column names which I find easier to compare than appending new columns to the old data. Especially when there are many many columns.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...