0
$\begingroup$

I am trying to create a new column in this dataset. I would like to have rating to be 1, 2, 3, 4 or 5 instead of 1.25, 2.00, 3.75, ..., 5.00. Can you help me? The only solution I found is this

chocolate$Rat = gsub("1.25", "1", chocolate$Rating)

but I know it is not a good way to work. Is there something faster which can to the work efficiently? Thanks

$\endgroup$
  • 1
    $\begingroup$ Do you want 3.75 as 3 or 4…? $\endgroup$ – BlackCurrant May 23 at 14:57
  • $\begingroup$ If possible, 4. but If you can only round to 3 it's not a problem $\endgroup$ – user96624 May 23 at 15:00
  • $\begingroup$ I want to use it to learn about associative rules and test how it works. I have an example with "titanic" dataset and I think this dataset could also work. My idea is to see the combinations which lead to "rating =1, rating =2, rating=3, rating=4, rating=5" with the apriori() algorithm $\endgroup$ – user96624 May 23 at 15:04
  • 1
    $\begingroup$ Just google round in r. $\endgroup$ – BlackCurrant May 23 at 15:18
  • $\begingroup$ Also, I am thinking about using this new column to build a decisional tree. Do you think it could work? $\endgroup$ – user96624 May 23 at 15:18
1
$\begingroup$

The R function is “round”. The default is to round to the nearest integer, which is what you want. The function takes vector inputs, so you can apply it to your column in one line instead of using a “for” loop.

Then you’ll tack in on to your original data in any number of ways, such as “expanded_data <- data.frame(original_data, round(column))”.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you very much! $\endgroup$ – user96624 May 23 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.