[R] Adding column values based on a condition

Eric Berger er|cjberger @end|ng |rom gm@||@com
Sun Jul 7 08:32:51 CEST 2019


# 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]]



More information about the R-help mailing list