Data Type for converting

Hello,

I am trying to convert multiple variables from dbl to intergers in one block of code but I get the below error. Can someone help me overcome this error. I have included code below.

Error message: Error: unexpected ')' in:
"Green<- (Red) %>%
mutate(a= as.integer (a))& b = as.integer (b))"

library(tibble)

Red <- tibble(
a = c( 1,2),
b = c(3,4),
c = c(5,6))

#Convert varible a,b, and c from double to integer variable
Green<- (Red) %>%
mutate(a= as.integer (a))& b = as.integer (b)) & c =as.integer (c))

Hi, is this what you're looking for?

Green<- Red %>%
mutate(a= as.integer (a), b = as.integer (b) , c =as.integer (c))
str(Green)
# tibble [2 × 3] (S3: tbl_df/tbl/data.frame)
#  $ a: int [1:2] 1 2
#  $ b: int [1:2] 3 4
#  $ c: int [1:2] 5 6

JW

A couple more options

library(tidyverse)

Red <- tibble(
    a = c( 1,2),
    b = c(3,4),
    c = c(5,6))

Red %>% 
    mutate(across(where(is.double), as.integer))
#> # A tibble: 2 x 3
#>       a     b     c
#>   <int> <int> <int>
#> 1     1     3     5
#> 2     2     4     6

Red %>% 
    mutate(across(a:c, as.integer))
#> # A tibble: 2 x 3
#>       a     b     c
#>   <int> <int> <int>
#> 1     1     3     5
#> 2     2     4     6

Created on 2021-04-05 by the reprex package (v2.0.0)

Thanks JW this is awesome. I really appreciate the guidance here...have a great week!!

This topic was automatically closed 7 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.