try_as: try_as

Description Usage Arguments Details Value See Also Examples

View source: R/try_as.R

Description

try_as

Usage

1
try_as(object, class)

Arguments

object

object

class

string; name of class

Details

try_as attempts to convert object to class.

warnings are surpressed; errors return NULL

Value

a object of the new class or NULL if as produces an error

See Also

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  nums <- seq(1.0,2.0,0.2)
  ints <- 1:5
  chrs <- letters[1:5]
  dts  <- seq( Sys.Date(), length.out=5, by="1 week") 
  posx <- as.POSIXct( dts )
 
  cls <- c('numeric','integer','character','Date','POSIXct')
  
  nums %>% try_as("numeric")
  nums %>% try_as("integer")
  nums %>% try_as("character")
  nums %>% try_as("Date")
  nums %>% try_as("POSIXct") 
    
  ints %>% try_as("numeric")
  ints %>% try_as("integer")
  ints %>% try_as("character")
  ints %>% try_as("Date")
  ints %>% try_as("POSIXct") 

  chrs %>% try_as("numeric")
  chrs %>% try_as("integer")
  chrs %>% try_as("character")
  chrs %>% try_as("Date")
  chrs %>% try_as("POSIXct") 

  dts %>% try_as("numeric")
  dts %>% try_as("integer")
  dts %>% try_as("character")
  dts %>% try_as("Date")
  dts %>% try_as("POSIXct") 
 
  posx %>% try_as("numeric")
  posx %>% try_as("integer")
  posx %>% try_as("character")
  posx %>% try_as("Date")
  posx %>% try_as("POSIXct") 
  

decisionpatterns/coercion documentation built on Nov. 4, 2019, 10:23 a.m.