## This will generate a vector of cas objects containing 10
## fictive (0-00-0), but valid registry numbers:
cas(10)
## This is a cas-object:
is.cas(cas(0L))
## This is not a cas-object:
is.cas(0L)
## Three different ways of creating a cas object from
## Benzene's CAS registry number (the result is the same)
as.cas("71-43-2")
as.cas("71432")
as.cas(71432L)
## This is one way of creating a vector with multiple CAS registry numbers:
cas_data <- as.cas(c("64175", "71432", "58082"))
## This is how you select a specific element(s) from the vector:
cas_data[2:3]
cas_data[[2]]
## You can also replace specific elements in the vector:
cas_data[1] <- "7440-23-5"
cas_data[[2]] <- "129-00-0"
## You can format CAS numbers with or without hyphens:
format(cas_data, TRUE)
format(cas_data, FALSE)
## The same can be achieved using as.character
as.character(cas_data, TRUE)
as.character(cas_data, FALSE)
## There are also show and print methods available:
show(cas_data)
print(cas_data)
## Numeric values can be obtained from CAS using as.numeric, as.double or as.integer
as.numeric(cas_data)
## Be careful, however. Some CAS numbers cannot be represented by R's 32 bit integers
## and will produce NA's. This will work OK:
huge_cas <- as.cas("9999999-99-5")
if (FALSE) {
## This will not:
as.integer(huge_cas)
}
## The trick applied by this package is that the final
## validation digit is stored separately as attribute:
unclass(huge_cas)
## This is how cas objects can be concatenated:
cas_data <- c(huge_cas, cas_data)
## This will create a data.frame
as.data.frame(cas_data)
## This will create a list:
as.list(cas_data)
Run the code above in your browser using DataLab