'How do I combine these lines into one line of R code?

How would I format these lines of code to be able to fit into one line of code?

TotalHoursAsleep <- sleep_data$TotalMinutesAsleep/60
TotalHoursAsleep <- as.double(TotalHoursAsleep)
sleep_data$TotalHoursAsleep <- TotalHoursAsleep
r


Solution 1:[1]

You could do

sleep_data$TotalHoursAsleep <- sleep_data$TotalMinutesAsleep / 60

You don't need as.double at all. Even if the total minutes asleep is an integer variable and each member is divisible by 60, the act of dividing it by 60 automatically converts it to a double. This is true even if you specify that the 60 is an integer by doing 60L

x <- c(60L, 120L, 180L)
typeof(x)
#> [1] "integer"

y <- 60L
typeof(y)
#> [1] "integer"

z <- x / y
typeof(z)
#> [1] "double"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1