Assignment Task:
Need help to change the data type to integer format for the n
column only.
Here is the code:
pizza %>%
select(day,driver,free_wine)%>%
group_by(day,driver)%>%
summarise(n=sum(free_wine,na.rm=TRUE),.groups = 'drop')%>%
arrange(desc(n))%>%
head(1)%>%
as.integer(n)
Note: current data format is dbl for n
column - I can only use tidyverse library and prefer to use function as.integer()
.
Code is not working - giving below error
Warning in pizza %>% select(day, driver, free_wine) %>% group_by(day, driver) %>% :
NAs introduced by coercion
And gives this output:
[1] NA NA 20
The expected output should be in the below format
Day Driver n
<chr> <chr> <int>
Thursday Rick 20
CodePudding user response:
Your code as written passes the entire dataframe to as.integer()
. To instead apply it to just one column, you can add it to your summarise()
call:
pizza %>%
select(day,driver,free_wine)%>%
group_by(day,driver)%>%
summarise(n=as.integer(sum(free_wine,na.rm=TRUE)),.groups = 'drop')%>%
arrange(desc(n))