[R] Adding column values based on a condition

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Sun Jul 7 16:25:02 CEST 2019


Can't this be done more simply and efficiently (no sapply implied loops, no
conditionals) just with indexing?

Using Eric's example:

df <-within(df,
      parent_author_id <- author_id[match(parent_id, id)]
       )

> df
   id author_id parent_id parent_author_id
1 101        25        NA               NA
2 102        24        NA               NA
3 103        23       101               25
4 104        22        NA               NA
5 105        21        NA               NA

Cheers,
Bert

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 Sat, Jul 6, 2019 at 11:33 PM Eric Berger <ericjberger using gmail.com> wrote:

> # create some test data
> df <- data.frame( id=101:105, author_id=25:21, parent_id=c(NA,NA,101,NA,NA)
> )
>
> df$parent_author_id <- sapply(1:nrow(df),
>               function(i) { if( !is.na(df$parent_id[i]) ) { j <-
> match(df$parent_id[i],df$id); df$author_id[j] } else NA } )
>
> df
>
> #   id author_id parent_id parent_author_id
> # 1 101        25        NA               NA
> # 2 102        24        NA               NA
> # 3 103        23       101               25
> # 4 104        22        NA               NA
> # 5 105        21        NA               NA
>
> HTH,
> Eric
>
>
> On Sun, Jul 7, 2019 at 7:59 AM Alessandro Puglisi <
> alessandro.puglisi using gmail.com> wrote:
>
> > Hi all!
> >
> > I have a tibble regarding a series of comments on a forum; the tibble
> has a
> > certain number of variables, and in particular:
> >
> > id: comment id;
> > author_id: author id;
> > parent_id: if the message is a reply, the original comment id
> >
> > I want to add another column, in which I put the author id of the
> original
> > comment, when parent_id IS NOT NA.
> >
> > I already tried to use mutate + ifelse but my problem is that I have to
> > refer to an author id in a different row than that in which I'm
> evaluating
> > the if condition...
> >
> > Thanks in advance,
> > Alessandro
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > 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.
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list