This function manages to coerce one or more objects into a character vector. Unlike
as.character, this function can handle data frames, lists and recursive lists
(lists of lists), even when there are factor objects inside data frames and lists. If there is any
NULL object in a list,
as.character2 will coerce that element into
character(0) rather than the character "NULL", which is what
as.character does. When the object is of class matrix or data frame, the function
will open it by column. The order of characters in result manages to keep accordance
with that of the input object.
one or more objects to be coerced.
a character vector
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
as.character2(NULL, NULL) # Try a list of NULLs null_list <- list(a = NULL, b = NULL, c = NULL) # Compare the different results of as.character # and as.character2. In fact, we usually # want the latter one. as.character(null_list) as.character2(null_list) # Try a list with a data frame in it df <- data.frame(matrix(c(66,77,NA,99), nrow = 2)) l <- list(a = 1:4, b = factor(c(10,20,NA, 30)), c = c('x', 'y', NA, 'z'), d = df) as.character2(l) # Try a list of lists l2 <- list(l, l, cha = c('a', 'b', 'c')) as.character2(l2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.