0

I have a want to create a new variable that is basically taking the value of an existing variable, but it is conditional. You have var1 and var2 that are both numeric. The new variable should take either var1 or var2, whichever is lower.

Example code (trying this just replicated var1):

upc_data_actual2$price9 <- if(upc_data_actual2$MSRP_PRC<upc_data_actual2$price){
  upc_data_actual2$price9=upc_data_actual2$MSRP_PRC
} else {
  upc_data_actual2$price=upc_data_actual2$price
}
5
  • Maybe an if/else condition is a good start Commented Aug 28, 2019 at 23:21
  • new_variable <- min(var1, var2) Commented Aug 28, 2019 at 23:40
  • Doing this just yields 0: upc_data_actual2$price8 <- min(upc_data_actual2$MSRP_PRC, upc_data_actual2$price) Commented Aug 29, 2019 at 1:28
  • Try . pmin(var1, var2). Commented Aug 29, 2019 at 3:07
  • pmin(var1, var2) works - thank you! Commented Aug 29, 2019 at 13:29

1 Answer 1

0
 var1=5
 var2=4
 t=0
 if (var1<var2) {
   t=var1
 } else {
   t=var2
 }
 print(t)
 Takes the smaller variable and puts it into another
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. I tried this and it just replicated var1. See below for example code: upc_data_actual2$price9 <- if(upc_data_actual2$MSRP_PRC<upc_data_actual2$price){ upc_data_actual2$price9=upc_data_actual2$MSRP_PRC } else { upc_data_actual2$price=upc_data_actual2$price }

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.