How can one get an R dataset with blanks in its name, such as 'BJsales.lead (BJsales)' in package "datasets" ?
pkg = "datasets"
cat( "Summary of all the datasets in package", pkg, "--\n" )
d = data( package=pkg ) $results # "Package" "LibPath" "Item" "Title"
names = d[ , "Item" ]
titles = d[ , "Title" ]
# sum( duplicated( names )) ??
for( j in 1:len(names) ){
name = names[[j]]
cat( name, ":\n" )
data( list=name )
x = get( name ) # <-- Error if blank in name
m = paste( dim( as.matrix( x )), collapse=" " ) # grr
cat( class(x), m, " freq", frequency(x), "\n" )
}
# -> Error in get(name) : object 'BJsales.lead (BJsales)' not found
OK, get
can only lookup valid names, that's reasonable.
But what to do -- how can one get the data for 'BJsales.lead (BJsales)' ?
R version 3.1.3 (2015-03-09)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
Running under: OS X 10.8.3 (Mountain Lion)
In fact,
get()
can look up "invalid" names:The issue here is that the
Item
column of theresults
matrix returned bydata()
does not always contain the exact name of the data set; in some cases, it has a parenthetical suffix, although I've no idea why.You can strip it off with
gsub()
, and then loading viaget()
should work.Also, you shouldn't need the
data(list=name)
call.Also, there's no
len()
(unfortunately); I think you meanlength()
.Hence: