Thank you for the general code. Really appreciate it.
On Tue, Sep 5, 2023 at 7:59?PM Eric Berger <ericjberger at gmail.com>
wrote:
> As Duncan points out, ifelse() provides a more general approach than
> the specific pmax().
>
> Even more generally, you might want to consider the apply() function
> (and its relatives sapply(), lapply(), ...)
>
> For example
>
> apply(cbind(x1,x2), MAR=1, max)
>
> In the above statement, x1 and x2 are combined into two columns, MAR=1
> says apply the following function to each row,
> and max specifies the function to be applied. The summary is that this
> statement applies the max to each row in the object
> specified by the first argument. It's very general because you can
> replace max by an arbitrary function.
>
> Similarly, if you get more into R you might modify the above using
> chained operations. The following rewrites the above
> by piping cbind(x1,x2) into the apply command. Not a necessary feature
> to know but eventually will assist you in writing code that does
> not require a lot of intermediate variables.
>
> cbind(x1,x2) |> apply(MAR=1,max)
>
> On Tue, Sep 5, 2023 at 2:45?PM Duncan Murdoch <murdoch.duncan at
gmail.com>
> wrote:
> >
> > On 05/09/2023 4:55 a.m., roslinazairimah zakaria wrote:
> > > Hi all,
> > >
> > > I have these data
> > >
> > > x1 <- c(116,0,115,137,127,0,0)
> > > x2 <- c(0,159,0,0,0,159,127)
> > >
> > > I want : xx <- c(116,115,137,127,159, 127)
> > >
> > > I would like to merge these data into one column. Whenever the
data is
> '0'
> > > it will be replaced by the value in the column which is non
zero..
> > > I tried append and merge but fail to get what I want.
> > >
> >
> > Others have pointed out pmax(x1, x2). For the sample data, even x1+x2
> > would give the same answer.
> >
> > The real question is what you want to do if both x1 and x2 have
non-zero
> > entries, or negative entries:
> >
> > pmax(x1, x2)
> >
> > will pick the larger one. It might be zero if the other is negative,
> > and that doesn't match your description.
> >
> > x1+x2
> >
> > will just give the sum, which doesn't match your description if
both are
> > non-zero.
> >
> > But maybe you want the x1 value unless it is zero, even if it is
smaller
> > than the x2 value: then you would use
> >
> > ifelse(x1 != 0, x1, x2)
> >
> > Similarly
> >
> > ifelse(x2 != 0, x2, x1)
> >
> > would prefer the x2 value.
> >
> > You should think about what you would do in these other cases as well.
> >
> > Duncan Murdoch
> >
> > ______________________________________________
> > R-help at 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.
>
--
*Roslinazairimah Zakaria*
*Tel: +609-5492370; Fax. No.+609-5492766*
*Email: roslinazairimah at ump.edu.my <roslinazairimah at ump.edu.my>;
roslinaump at gmail.com <roslinaump at gmail.com>*
Faculty of Industrial Sciences & Technology
University Malaysia Pahang
Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia
[[alternative HTML version deleted]]