[R] Adding column values based on a condition

Eric Berger
Sun, 07 Jul 2019 06:32:51 -0000

# 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 at 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 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.
>