0

I have a large data frame and want to create a new variable which depends on two other variables.

Here is a short example:

v1 <- rep(c(1:5),each=3)
v2 <- c('X','A','Y','X','Y','B','X','Y','C','X','Y','C','X','Y','A')

dat <- data.frame(v1,v2)

#create a new var which contains either A,B, or C depending on what is found in v2  


#desired output
v3 <- rep(c('A','B','C','C','A'),each=3)
data.frame(v1,v2,v3)

Any ideas on how to do this with a short code?

I tried this, but it's far from the solution. Too many missings. :(

dat$v3[dat$v2 %in% c('A','B','C')] <- dat$v2[dat$v2 %in% c('A','B','C')]
6
  • Something like with(dat, ave(v2, v1, FUN = function(i) tail(i, 1))) should do it given that the order is always as shown Commented Nov 23, 2018 at 10:49
  • As far as I can see v3 only depends on one variable. The rule appears to be 1 -> A, 2 -> B and so on. Commented Nov 23, 2018 at 10:50
  • It is more complicated. I will add another line ;) Commented Nov 23, 2018 at 10:54
  • Can you also add your attempt that failed please? Commented Nov 23, 2018 at 11:00
  • 1
    dplyr's case_when() or ifelse() statements fit perfectly here. However, since you did not provide the exact conditions, it's hard to write an example. Commented Nov 23, 2018 at 11:07

1 Answer 1

2
library(tidyverse)
dat %>% group_by(v1) %>% mutate(v3 = intersect(v2, c("A", "B", "C")))
# A tibble: 15 x 3
# Groups:   v1 [5]
#       v1 v2    v3   
#    <int> <fct> <chr>
#  1     1 X     A    
#  2     1 A     A    
#  3     1 Y     A    
#  4     2 X     B    
#  5     2 Y     B    
#  6     2 B     B    
#  7     3 X     C    
#  8     3 Y     C    
#  9     3 C     C    
# 10     4 X     C    
# 11     4 Y     C    
# 12     4 C     C    
# 13     5 X     A    
# 14     5 Y     A    
# 15     5 A     A    

This is assuming that only one of A, B, C can appear in a group given by v1.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.