toChar: Convert strings to vectors of chars.

toCharR Documentation

Convert strings to vectors of chars.

Description

For inputs containing only a single string, returns a character vector of single chars (UTF8s) by default. For inputs containing multiple strings, or if drop= FALSE is set, returns a list of character vectors (of single chars) named for the string. Duplicate names are fine.

Usage

toChar(x, drop = TRUE, use.names = TRUE)

Arguments

x

A vector of strings to convert

drop

Only affects output when x= is a single element. Set to FALSE to avoid automatic simplification of the one-element list output to a vector.

use.names

By default the list will use the original strings as names. This may not be reasonable for large strings; set this false to just use numeric ordering (the same as the input string order).

Value

The input strings, split into vectors of single characters. If more than one string is input, or if drop= FALSE is set this outputs a list of vectors. Vectors will be named for the input string, if possible.

Examples

toChar( c("ABC", "ABC", "A\u00dfB", 123, "", "x", NA ) )
#=> $ABC
#=> [1] "A" "B" "C"
#=>
#=> $ABC
#=> [1] "A" "B" "C"
#=>
#=> $AßB
#=> [1] "A" "ß" "B"
#=>
#=> $`123`
#=> [1] "1" "2" "3"
#=>
#=> [[5]]
#=> [1] ""
#=>
#=> $x
#=> [1] "x"
#=>
#=> $<NA>
#=> [1] NA

toChar( "ABC" )
#=> [1] "A" "B" "C"

toChar( c("ABC", "ABC", use.names= FALSE) )
#=> [[1]]
#=> [1] "A" "B" "C"
#=>
#=> [[1]]
#=> [1] "A" "B" "C"

toChar( "ABC", drop= FALSE )
#=> $ABC
#=> [1] "A" "B" "C"

toChar( "ABC", drop= FALSE, use.names= FALSE )
#=> [[1]]
#=> [1] "A" "B" "C"

toChar( 123 )
#=> [1] "1" "2" "3"

toChar( NULL )
#=> character(0)

toChar( character(0) )
#=> character(0)


jefferys/JefferysRUtils documentation built on Jan. 12, 2024, 9:18 p.m.