Hello ! Instead of showing the calculation as a result, the result is being shown as "tripduration/60".

Final_data2 %>% mutate(Trip_in_Hours="tripduration/60")

*Can anyone please point out the mistake !

Try

Final_data2 %>% mutate(Trip_in_Hours= tripduration/60)

rlang::last_trace()
<error/dplyr:::mutate_error>
Error in mutate():
:information_source: In argument: Trip_in_Hours = tripduration/60.
Caused by error in tripduration / 60:
! non-numeric argument to binary operator

*This error message is showing!

It looks like Trip_in_Hours is not a numeric variable.

What does

str(Final_data2$Trip_in_Hours)

give?

chr [1:2709841] "390.0" "441.0" "829.0" "1,783.0" "364.0" "216.0" "177.0" "100.0" ...

*I want to remove the decimals and then divide the number by 60, to get the Time in hours.

Does this get you what you want?

library(readr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
Final_data2 <- data.frame(tripduration = c("390.0", "441.0", "829.0", "1,783.0", "364.0", "216.0", "177.0", "100.0"))
Final_data2 <- Final_data2 %>% mutate(tripduration = parse_number(tripduration),
                       Trip_in_Hours= tripduration/60)
Final_data2
#>   tripduration Trip_in_Hours
#> 1          390      6.500000
#> 2          441      7.350000
#> 3          829     13.816667
#> 4         1783     29.716667
#> 5          364      6.066667
#> 6          216      3.600000
#> 7          177      2.950000
#> 8          100      1.666667

Created on 2023-04-02 with reprex v2.0.2

Thank you ! This worked but why the extra "parse_number(tripduration)" command was included ?

The original tripduration column is characters. That is why you were getting the error non-numeric argument to binary operator. The parse_number() function converts characters into numbers. You can see that by running the str() function just before and after using parse_number().

Final_data2 <- data.frame(tripduration = c("390.0", "441.0", "829.0", "1,783.0", "364.0", "216.0", "177.0", "100.0"))
str(Final_data2)
'data.frame':	8 obs. of  1 variable:
 $ tripduration: chr  "390.0" "441.0" "829.0" "1,783.0" ...

Final_data2 <- Final_data2 %>% mutate(tripduration = parse_number(tripduration))

str(Final_data2)
'data.frame':	8 obs. of  1 variable:
 $ tripduration: num  390 441 829 1783 364 ...
1 Like

Thanks a lot ! Appreciate your help.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.