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 10125NA
# 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_
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 th
3 matches
Mail list logo