Last data update: 2014.03.03

R: Put character vectors, columns of a data.frame or list...
factoriseR Documentation

Put character vectors, columns of a data.frame or list elements as factor

Description

Put character vectors, columns of a data.frame or list elements as factor if they are character strings or optionally if they are logicals

Usage

factorise(x, logicals = FALSE, ...)

## Default S3 method:
factorise(x, logicals = FALSE, ...)

## S3 method for class 'character'
factorise(x, logicals = FALSE, ...)

## S3 method for class 'data.frame'
factorise(x, logicals = FALSE, ...)

## S3 method for class 'list'
factorise(x, logicals = FALSE, ...)

Arguments

x

a character vector, a data.frame or a list

logicals

logical indicating if logical vectors should also be converted to factors. Defaults to FALSE.

...

optional arguments passed on to the methods

Value

The updated x vector/data.frame or list where the character vectors or optionally logical elements are converted to factors

See Also

as.factor, factor

Examples

x <- data.frame(x = 1:4, y = LETTERS[1:4], b = c(TRUE, FALSE, NA, TRUE), stringsAsFactors=FALSE)
str(factorise(x))
str(factorise(x, logicals = TRUE))
str(factorise(list(a = LETTERS, b = 1:10, c = pi, d = list(x = x))))

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(ETLUtils)
Loading required package: ff
Loading required package: bit
Attaching package bit
package:bit (c) 2008-2012 Jens Oehlschlaegel (GPL-2)
creators: bit bitwhich
coercion: as.logical as.integer as.bit as.bitwhich which
operator: ! & | xor != ==
querying: print length any all min max range sum summary
bit access: length<- [ [<- [[ [[<-
for more help type ?bit

Attaching package: 'bit'

The following object is masked from 'package:base':

    xor

Attaching package ff
- getOption("fftempdir")=="/tmp/RtmpkHQfWP"

- getOption("ffextension")=="ff"

- getOption("ffdrop")==TRUE

- getOption("fffinonexit")==TRUE

- getOption("ffpagesize")==65536

- getOption("ffcaching")=="mmnoflush"  -- consider "ffeachflush" if your system stalls on large writes

- getOption("ffbatchbytes")==16777216 -- consider a different value for tuning your system

- getOption("ffmaxbytes")==536870912 -- consider a different value for tuning your system


Attaching package: 'ff'

The following objects are masked from 'package:bit':

    clone, clone.default, clone.list

The following objects are masked from 'package:utils':

    write.csv, write.csv2

The following objects are masked from 'package:base':

    is.factor, is.ordered

> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/ETLUtils/factorise.Rd_%03d_medium.png", width=480, height=480)
> ### Name: factorise
> ### Title: Put character vectors, columns of a data.frame or list elements
> ###   as factor
> ### Aliases: factorise factorise.character factorise.data.frame
> ###   factorise.default factorise.list
> 
> ### ** Examples
> 
> x <- data.frame(x = 1:4, y = LETTERS[1:4], b = c(TRUE, FALSE, NA, TRUE), stringsAsFactors=FALSE)
> str(factorise(x))
'data.frame':	4 obs. of  3 variables:
 $ x: int  1 2 3 4
 $ y: Factor w/ 4 levels "A","B","C","D": 1 2 3 4
 $ b: logi  TRUE FALSE NA TRUE
> str(factorise(x, logicals = TRUE))
'data.frame':	4 obs. of  3 variables:
 $ x: int  1 2 3 4
 $ y: Factor w/ 4 levels "A","B","C","D": 1 2 3 4
 $ b: Factor w/ 2 levels "FALSE","TRUE": 2 1 NA 2
> str(factorise(list(a = LETTERS, b = 1:10, c = pi, d = list(x = x))))
List of 4
 $ a: Factor w/ 26 levels "A","B","C","D",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ b: int [1:10] 1 2 3 4 5 6 7 8 9 10
 $ c: num 3.14
 $ d:List of 1
  ..$ x:'data.frame':	4 obs. of  3 variables:
  .. ..$ x: int [1:4] 1 2 3 4
  .. ..$ y: Factor w/ 4 levels "A","B","C","D": 1 2 3 4
  .. ..$ b: logi [1:4] TRUE FALSE NA TRUE
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>