This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Replacing a values from a column in another column

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

Please don’t post screenshots of test data, instead copy and paste it in to you post as code formatted text (use the 101010 Button)

If your original data is an image, you will need to parse it with an OCR reader - I've use Tesseract-ocr and got good results.

If your data is text, please post text as example, not an image.

3 answers

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)

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

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"]
library(tidyverse)
new_df <- mutate(df, A_Freq = ifelse(A_Freq >= 0.5, A_Freq, M.F))
$ awk '{ if ($1 < 0.5) { $1 = $2 } print $0; }' in.txt > out.txt

many thanks for your reply,

Please look again at the table above that has been corrected.

Prefer script to work in R.

Log in to answer this question.