[R] comparing and changing(swaping) the value of columns from two diffrent data farmes

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Sun Jun 12 19:11:46 CEST 2022


Excellent!

I am sharing the below with the list in case it might be useful for others.

Here is a similar approach that uses logical indexing directly, rather
than through ifelse(), via a simple function to pick out the rows to
change. Figuring out the details of how it works may be helpful to you
in case it's not immediately obvious.

foo <- function(dat1, dat2, testcols){
      apply(dat1[, testcols] != dat2[,testcols],1,any) ## ?apply  ?any
for details
}

Yielding...

> wh <- foo(df1, df2, 2:3)
> wh ## logical index of rows that require changes
[1]  TRUE  TRUE FALSE  TRUE FALSE

> df2[wh, 4:8] <- 2 - df2[wh, 4:8]  ## per your specification
> ## and since df2's first 3 columns must be the same as df1's:
> df2[,2:3] <- df1[, 2:3]

> df2
   SNPID OA EA id00001 id00002 id00003 id00004 id00005
1 snp001  C  A    0.99    0.00    1.03    0.03    0.01
2 snp002  G  A    0.98    0.00    1.00    0.00    0.00
3 snp003  C  A    1.00    1.03    2.00    0.00    1.00
4 snp004  G  A    0.98    0.01    0.00    0.98    0.02
5 snp005  C  T    1.00    0.00    1.01    1.00    1.00



On Sun, Jun 12, 2022 at 9:32 AM anteneh asmare <hanatezera using gmail.com> wrote:
>
> Dear, Bert, Thanks i try to extract  dataframe1[ , c(2, 3)] and
> combine to dataframe2 , then i apply if else
> it works, Thanks
> best,
> Hana
>
> On 6/12/22, Bert Gunter <bgunter.4567 using gmail.com> wrote:
> > No you don't. You need to learn R's data manipulation procedures so
> > that you can program such (in this case fairly simple) procedures
> > yourself.
> >
> > The ifelse() function might be a good place to start here.
> > ?ifelse
> >
> > Bert Gunter
> >
> >
> >
> > On Sun, Jun 12, 2022 at 8:58 AM anteneh asmare <hanatezera using gmail.com>
> > wrote:
> >>
> >> Dear Bert , Thanks for reply! I need the  the function or package that
> >> used to compare columns from two different data frames.
> >> Best,
> >> Hana
> >> On 6/12/22, Bert Gunter <bgunter.4567 using gmail.com> wrote:
> >> > Is this some sort of homework? This list has a no homework policy.
> >> >
> >> > If not, please show us your attempt to solve the problem. You seem to
> >> > be asking us to do your work for you, which is not a good way to learn
> >> > R. Show us your attempt and the error messages and/or incorrect
> >> > results you got. You will improve your R skills faster this way. IMO
> >> > only, of course. The specifications you provided seem to require
> >> > fairly basic data manipulations that you should really learn how to do
> >> > yourself.
> >> >
> >> > Also...
> >> > Thanks for the reproducible example -- that is a good way to get a
> >> > helpful response. But please give us the code that creates the example
> >> > rather than just providing the text, which requires us to read and
> >> > convert it ourselves, an extra step. In general, if we can just rerun
> >> > your code it makes it easier to help ... which makes it more likely
> >> > that someone will give you a useful response.
> >> >
> >> > Bert Gunter
> >> >
> >> > "The trouble with having an open mind is that people keep coming along
> >> > and sticking things into it."
> >> > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >> >
> >> > On Sun, Jun 12, 2022 at 7:07 AM anteneh asmare <hanatezera using gmail.com>
> >> > wrote:
> >> >>
> >> >> 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
> >> >> Dose any one can help me the r code for the above.
> >> >> Kind regards,
> >> >> Hana
> >> >>
> >> >> ______________________________________________
> >> >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> >> PLEASE do read the posting guide
> >> >> http://www.R-project.org/posting-guide.html
> >> >> and provide commented, minimal, self-contained, reproducible code.
> >> >
> >



More information about the R-help mailing list