can_be: can_be

Description Usage Arguments Details Value See Also Examples

View source: R/can_be.R

Description

Determine if the a vector can be coerced into the given class

Usage

1
2
3
4
5
6
7
8
9

Arguments

object

object;

class

character; name of class to check if it can be coerced

Details

can_be determines if an object can reliably coerced to class, returning TRUE if ALL values can be coerced.

It uses the results oftry(as(...)) under the hood. See try_as() for details .

which_can_be provides indices of object`` that **can** be coerced to the given class. NA' can always be coerced.

which_cant_be provides indices of object that can't be coerced to the given class. NA can always be coerced.

Value

A logical scalar indicating whether object can be coerced to class Class

logical

which_can_be returns a integer vector of the indices of values that can be successfully coerced to the new class.

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  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 %>% can_be("numeric")
  nums %>% can_be("integer")
  nums %>% can_be("character")
  nums %>% can_be("Date")
  nums %>% can_be("POSIXct") 
    
  ints %>% can_be("numeric")
  ints %>% can_be("integer")
  ints %>% can_be("character")
  ints %>% can_be("Date")
  ints %>% can_be("POSIXct") 

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

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


  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 %>% which_can_be("numeric")
  nums %>% which_can_be("integer")
  nums %>% which_can_be("character")
  nums %>% which_can_be("Date")
  nums %>% which_can_be("POSIXct") 
    
  ints %>% which_can_be("numeric")
  ints %>% which_can_be("integer")
  ints %>% which_can_be("character")
  ints %>% which_can_be("Date")
  ints %>% which_can_be("POSIXct") 

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

  dts %>% which_can_be("numeric")
  dts %>% which_can_be("integer")
  dts %>% which_can_be("character")
  dts %>% which_can_be("Date")
  dts %>% which_can_be("POSIXct") 
 
  posx %>% which_can_be("numeric")
  posx %>% which_can_be("integer")
  posx %>% which_can_be("character")
  posx %>% which_can_be("Date")
  posx %>% which_can_be("POSIXct") 
  
  
  x <- c( 1:3, letters[1:3], NA )
  x %>% which_can_be( "integer" )
  x %>% which_cant_be( "integer" )
  

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