Replacing a values from a column in another column
3
2
Entering edit mode
6.2 years ago

Hi all,

As you see in the picture, I have two columns. So, I want to replace the values smaller than 0.500000 from the column A_Freq with the values in the M.F column In the same rows. What is the best idea?

for example: replace 0.312500 In the third row of the column A_Freq with 0.687500 From the same row but in the column M.F.

         CHROM_POS   A_Freq      M.F    N_Chr 
1  CM009840.1_1096 0.812500 0.187500 16.25000 
2  CM009840.1_1177 0.611111 0.388889 12.22222 
3  CM009840.1_1276 0.312500 0.687500  6.25000 
4  CM009840.1_1295 0.277778 0.722222  5.55556 
5  CM009840.1_1471 0.250000 0.750000  5.00000 
6  CM009840.1_1518 0.875000 0.125000 17.50000 
7  CM009840.1_1527 0.222222 0.777778  4.44444 
8  CM009840.1_1533 0.777778 0.222222 15.55556 
9  CM009840.1_1630 0.250000 0.750000  5.00000 
10 CM009840.1_1639 0.000000 1.000000  0.00000 
11 CM009840.1_1711 0.500000 0.500000 10.00000 
12 CM009840.1_1972 0.250000 0.750000  5.00000 
13 CM009840.1_2030 0.142857 0.857143  2.85714 
14 CM009840.1_2101 0.375000 0.625000  7.50000 
15 CM009840.1_2690 0.687500 0.312500 13.75000 
16 CM009840.1_2849 0.142857 0.857143  2.85714 
17 CM009840.1_3013 0.312500 0.687500  6.25000 
18 CM009840.1_3042 0.714286 0.285714 14.28572 
19 CM009840.1_3062 0.250000 0.750000  5.00000 
20 CM009840.1_3128 0.250000 0.750000  5.00000

Best Regard

Modtafa

R • 27k views
ADD COMMENT
4
Entering edit mode
6.2 years ago

with R :

select <- df$A_Freq < 0.5
df[select,"A_Freq"] <- df[select,"M.F"]

or maybe more elegant :

df$A_Freq <- ifelse(df$A_Freq < 0.5, df$M.F, df$A_Freq)
ADD COMMENT
1
Entering edit mode

Dear Rosewick,

I've shared the correct script in the attachment.

select <- k$M.F > 0.5
k[select,"M.F"] <- k[select,"A_Freq"]
ADD REPLY
0
Entering edit mode

I feel that there was an error, And by executing this command, the result was reversed.

As I mentioned above. i want for example: eplace 0.312500 In the third row of the column A_Freq with 0.687500 From the same row but in the column M.F. to wit 0.312500 Alternative 0.687500 in the column M.F. But as you can see below, the result is reversed.

        CHROM_POS   A_Freq      M.F    N_Chr 
1 CM009840.1_1096 0.812500 0.187500 16.25000 
2 CM009840.1_1177 0.611111 0.388889 12.22222 
3 CM009840.1_1276 0.687500 0.687500  6.25000 
4 CM009840.1_1295 0.722222 0.722222  5.55556 
5 CM009840.1_1471 0.750000 0.750000  5.00000 
6 CM009840.1_1518 0.875000 0.125000 17.50000
ADD REPLY
2
Entering edit mode
6.2 years ago
russhh 5.7k
library(tidyverse)
new_df <- mutate(df, A_Freq = ifelse(A_Freq >= 0.5, A_Freq, M.F))
ADD COMMENT

Login before adding your answer.

Traffic: 2191 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6