Change column based on other two columns

Viewed 56

I have a dataframe as follows:

df <- data.frame(matrix(NA, nrow = 5, ncol = 1))
colnames(df) <- "father"
df$father <- c("A", "B", "B","C","D")
df$id <- c("C", "E","F","G","H")
df$pop <- c("ref","ref","ref","val","val")

Which gives:

  father id pop
1      A  C ref
2      B  E ref
3      B  F ref
4      C  G val
5      D  H val

Then if "id" appears in "father", the value of the pop should be the one that appears in the row of the father. For example in this example "C" appears both in "father" and in "id", in father C the pop is val, then, I want the pop of C as id to be val as follows:

  father id pop
1      A  C val 
2      B  E ref
3      B  F ref
4      C  G val
5      D  H val

Any ideas?

1 Answers

You can try the following

require(dplyr)
require(magrittr)

merge(x = df,
      y = df,
      by.x = 'id',
      by.y = 'father',
      all.x = T) %>% 
  mutate(., pop = ifelse(is.na(pop.y), pop.x, pop.y)) %>%
  subset(., select = c('father', 'id', 'pop'))

The idea is to perform a left join onto itself by merge (since all.x=T), and select the wanted value by mutate.

Related