[R] Changing sign of columns and values

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Mon Jun 13 01:41:48 CEST 2022

Hi Hana,
This is a bit more difficult, but the same basic steps apply. See the
When you submit a problem like this, it is a lot easier if you send
the output from "dput" (e.g. dput(df1)) or set your data frames up
with "read.table" like I have done in the example below. It make it
lots easier for anyone who wants to reply to see what your dataset
looks like and input that dataset to devise a solution.
I have made some assumptions about marking the rows to be altered and
the arithmetic to do on those rows. What follows may not be as general
a solution as you want:

"SNPID OA    EA
snp001        C     A
snp002        G     A
snp003        C     A
snp004        G     A
snp005        C     T",

"SNPID   OA    EA   id00001 id00002 id00003 id00004 id00005
snp001    A    C    1.01    2       0.97    1.97    1.99
snp002    A    G    1.02    2       1       2       2
snp003    C    A    1       1.03    2       0       1
snp004    A    G    1.02    1.99    2       1.02    1.98
snp005    C    T    1       0       1.01    1       1",

# get a logical vector of the different XA values
reversals<-df1\$EA != df2\$EA | df1\$OA != df2\$OA
reversals

# set a variable to the maximum value of idxxxxx
# just to make the code easier to understand
maxid<-2

# create a copy of df2
dfnew<-df2

# now swap the XA columns and reflect the
# idxxxxxx values of the rows in which
# EA and OA are different between df1 and df2
# here I have looped through the rows
nrows<-dim(dfnew)[1]
idcols<-4:8
XAcols<-2:3
for(i in 1:nrows) {
if(reversals[i]) {
dfnew[i,XAcols]<-rev(dfnew[i,XAcols])
dfnew[i,idcols]<-maxid-dfnew[i,idcols]
}
}

dfnew

Jim

On Mon, Jun 13, 2022 at 12:09 AM anteneh asmare <hanatezera using gmail.com> wrote:
>
> Dear Jim, Morning I have the same issue regarding comparing and
> swapping the value of columns fro two different data frames.
> I have the following two data frames
> Data frame 1
> "SNPID" "OA"    "EA"
> "snp001"        "C"     "A"
> "snp002"        "G"     "A"
> "snp003"        "C"     "A"
> "snp004"        "G"     "A"
> "snp005"        "C"     "T"
>
> Data frame 2
> SNPID   OA      EA      id00001 id00002 id00003 id00004 id00005
> snp001     A    C        1.01                      2                      0.97
>           1.97                    1.99
> snp002    A     G        1.02                     2                        1
>          2                            2
> snp003    C     A         1                      1.03                       2
>           0                            1
> snp004   A      G          1.02                    1.99                      2
>              1.02                           1.98
> snp005  C       T            1                        0                      1.01
>                     1                           1
>
> I want to  if  OA s and EAs in data frame 2 is the same as OAs and EAs
>  data frame 1 in such case I want to keep all information in data
> fram2 as it is . However if   OA s and EAs in data frame 2 is
> different from OAs and EAs  data frame 1, I want to change OA s and
> EAs in data frame 2 to  OAs and EAs  data frame 1 and   I want to
> redefine the values of the dosages (ids) of the variant (the dosage d
> for the i-th individual and the j-th variant would become d_ij
> new=2-dij.
> Dosage [j,]=2-dosage[j,]
> My desire data frame looks like
> Dataframe new
> SNPID"  "OA"       "EA"        id00001  id00002 id00003 id00004 id00005
> "snp001"        "C"     "A"     2-1.01            2- 2    2-    0.97           2-1.97
>     2-1.99
> "snp002"        "G"     "A"     2- 1.02              2- 2                   2-1
>   2-2                     2- 2
> "snp003"        "C"     "A"       1                    1.03                         2
>             0                          1
> "snp004"        "G"     "A"     2-1.02                   2-1.99
> 2-2                    2-1.02                    2-1.98
> "snp005"        "C"     "T"          1                        0
> 1.01                           1                                1
>  can you help me the r code for the above.
> Kind regards,
> Hana
>
>
> On 6/12/22, hanatezera <hanatezera using gmail.com> wrote:
> > Dear Jim, Thanks a lot this is exactly i am looking for.Stay safe and
> > blessed !Best,Hana
> > -------- Original message --------From: Jim Lemon <drjimlemon using gmail.com>
> > Date: 6/12/22  6:43 AM  (GMT+03:00) To: hanatezera <hanatezera using gmail.com>
> > Cc: r-help mailing list <r-help using r-project.org> Subject: Re: [R] Changing
> > sign of columns and values Hi Hana,I didn't look closely. The simplest rule
> > that I can see is that theletters (nucleotides?) should be swapped if there
> > has been a signchange in the beta value.
> > Therefore:mydf<-read.table(text="IDnumber  OA  EA  beta1   C       A
> > -0.052   G        A        0.0983   G        T
> > -0.789",header=TRUE,stringsAsFactors=FALSE)# logical vector marking the rows
> > to be swappedswap_letters<-mydf\$beta < 0# save the OA values to be
> > swappednewEA<-mydf\$OA[swap_letters]# change the OAs to
> > EAsmydf\$OA[swap_letters]<-mydf\$EA[swap_letters]# set the relevant EA values
> > to the old OA valuesmydf\$EA[swap_letters]<-newEA# change the beta
> > valuesmydf\$beta<-abs(mydf\$beta)mydfJimOn Sun, Jun 12, 2022 at 9:11 AM
> > hanatezera <hanatezera using gmail.com> wrote:>> Dear jim thanks for your help! I
> > want to change also the value of OA and EF simultaneously.> For instance i
> > am looking the data> mydf> IDnumber OA EA  beta> 1        1  A  C  0.050>
> > 2        2  G  A 0.098> 3        3  T   G 0.789>> Best,> Hana>>>> --------
> > Original message --------> From: Jim Lemon <drjimlemon using gmail.com>> Date:
> > 6/12/22 1:59 AM (GMT+03:00)> To: hanatezera <hanatezera using gmail.com>, r-help
> > mailing list <r-help using r-project.org>> Subject: Re: [R] Changing sign of
> > columns and values>> Hi Hana,> I think this is what you want:>> # first read
> > C       A       -0.05> 2   G        A        0.098> 3   G        T
> > -0.789",> header=TRUE,stringsAsFactors=FALSE)> # check it> mydf> IDnumber OA
> > EA   beta> 1        1  C  A -0.050> 2        2  G  A  0.098> 3        3  G
> > T -0.789> # change values of mydf\$beta to absolute values>
> > mydf\$beta<-abs(mydf\$beta)> mydf> IDnumber OA EA  beta> 1        1  C  A
> > 0.050> 2        2  G  A 0.098> 3        3  G  T 0.789>> Jim>> On Sun, Jun
> > 12, 2022 at 8:27 AM hanatezera <hanatezera using gmail.com> wrote:> >> > I have
> > the following data set in data frameIDnumber  OA  EA
> > beta1                         C       A       -0.052
> > G        A        0.0983                          G        T
> > -0.789....I want to change the sign of negative beta. If the negative value
> > change to postive i want to switch its EA and OA.My desire result will
> > beIDnumber  OA  EA  beta1                         A      C
> > 0.052                         G        A
> > 0.0983                          T         G        0.789....Any one can help
> > me with r codes? kind regards,Hana> >         [[alternative HTML version
> > deleted]]> >> > ______________________________________________> >
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see> >