0

I am a beginner in R , I am trying to find the multiply (product price) column by (purchase price) in a new column name ( total ) , but I get an error when run the following :

new_test_import <- read_csv("test_import.csv")

head(new_test_import)

str(new_test_import)

colnames(new_test_import)

total_price <- select (new_test_import , "product" , "product_price" , "purchase_price")

mutate(total_price, total = product_price * purchase_price)

cols( product_price = col_character(), purchase_price = col_character()

Error in `mutate()`:
`**ℹ In argument: `total = product_price * purchase_price`.
Caused by error in `product_price * purchase_price`:
! non-numeric argument to binary operator***

Backtrace:    
`1. dplyr::mutate(total_price, total = product_price * purchase_price)    
 2. dplyr:::mutate.data.frame(total_price, total = product_price * purchase_price)    
 3. dplyr:::mutate_cols(.data, dplyr_quosures(...), by)    
 5. dplyr:::mutate_col(dots[[i]], data, mask, new_columns)    
 6. mask$eval_all_mutate(quo)    
 7. dplyr (local) eval()    
harre
  • 7,081
  • 2
  • 16
  • 28
  • A likely reason is that for some reason, r read either product_price or purchase_price as non-numeric when you imported the data. Check this by running class(new_test_import$product_price) and class(new_test_import$purchase_price) and see if one or both have the class "character" or anything non-numeric. If so, you can run new_test_import$product_price<-as.numeric(new_test_import$product_price) and then run your code again. – Sointu Feb 17 '23 at 13:48
  • Hi user21072729! Welcome to SO! Both `product_price` and `purchase_price` are characters that cannot be multiplied. Please see how to make a great reproducible example here (https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), update your post accordingly, and we can guide you how to proceed. – harre Feb 17 '23 at 14:05
  • Thanks for your replys , you are right both was charchter , I edit it as follows but still get an error . class(new_test_import$product_price) class(new_test_import$purchase_price) new_test_import$product_price<-as.numeric(new_test_import$product_price) new_test_import$purchase_price<-as.numeric(new_test_import$purchase_price) mutate(total_price, total = product_price * purchase_price) – Wisam_Saeed Feb 17 '23 at 20:42
  • did you make sure the variables are also numeric in your new data frame "total_price"? Do the select after you did the "as.numeric" part or use total_price$purchase_price <- as. numeric(total_price$purchase_price) etc. – Sointu Feb 17 '23 at 20:54
  • `class(new_test_import$purchase_price)` `new_test_import$purchase_price<-as.numeric(new_test_import$purchase_price)` `total_price$purchase_price <- as.numeric(total_price$purchase_price)` `total_price <- select (new_test_import , "product" , "purchase_price")` `mutate(total_price, total = purchase_price * 10 )` --------- I can run the code now , but both purchase price and total columns are NA – Wisam_Saeed Feb 18 '23 at 10:50
  • You probably need to post a reproducible example as suggested above to get advice on this latest issue. – Sointu Feb 18 '23 at 13:21

0 Answers0