简体   繁体   中英

Character vectors as ff objects in R

I am trying to convert a standard (RAM) character vector to an ff object (vector). The code below returns an error:

> as.ff(c('a', 'b'))
Error in ff(initdata = initdata, length = length, levels = levels, ordered = ordered,: 
vmode 'character' not implemented

This thread ( https://stackoverflow.com/questions/17744525/r-difficulties-facing-with-read-csv-ffdf-physicalmode-and-virtualmode ) suggests that ff objects do not accept characters at all, only factors. Still, the below does not work:

> as.ff(c('a', 'b'), vmode = 'factor')
Error in ff(initdata = initdata, length = length, levels = levels, ordered = ordered,:
vmode 'factor' not implemented

The list below does not include 'factors':

.vimplemented
boolean   logical      quad    nibble      byte     ubyte     short    ushort 
 TRUE      TRUE      TRUE      TRUE      TRUE      TRUE      TRUE      TRUE 
integer    single    double   complex      raw  character 
 TRUE      TRUE      TRUE     FALSE      TRUE     FALSE 

So is it possible at all to create an ff vector of characters?

Curently, in ff, pure character vectors are not implemented. Factors are. As c('a','b') is a character, it will not work to convert it to ff. But it is of course possible to convert factors to ff.

require(ff)
class(c('a', 'b'))
[1] "character"
class(factor(c('a', 'b')))
[1] "factor"
as.ff(factor(c('a', 'b')))
ff (open) integer length=2 (2) levels: a b
[1] [2] 
  a   b 
class(as.ff(factor(c('a', 'b'))))
[1] "ff_vector" "ff" 

Mark also that the factor levels are in RAM. All the rest is on disk.

Just call factor on your variable:

as.ff(factor(c('a', 'b')))
ff (open) integer length=2 (2) levels: a b
[1] [2] 
  a   b 

Internally, factors are integers,

storage.mode(factor(c('a', 'b')))
[1] "integer"

with a levels attribute that maps to the character representation. As you noted, integers are supported by ff .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM