![]() |
Introduction
To R |
> c1 <- "Hello" A scalar character string > c1 [1] "Hello" > c2 <- c("Yes",'Maybe',"No") A vector of characters > c2 [1] "Yes" "Maybe" "No" > c3 <- c("Is","Could Be","Isn't") Note single quote inside doublesYou can also use some of the functions we used to construct numeric vectors with character data:
> c3 <- rep("Monkey",4) Using rep() > c3 [1] "Monkey" "Monkey" "Monkey" "Monkey" > c4 <- rep(c("Monkey","Bucket"),3) > c4 [1] "Monkey" "Bucket" "Monkey" "Bucket" "Monkey" "Bucket"Of course, don't expect to be able to add characters!
> c5 <- c('M','F','F','F','M','M') > f5 <- as.factor(c5) > f5 [1] M F F F M M Levels: F MNotice that a factor gets printed out like a character vector, but without the quote marks. The levels are also printed out.
The categories in a factor can also be seen by using the levels() function:
> levels(f5) [1] "F" "M" The result is a character vectorAnd you can change these levels easily by assigning a new character vector to the levels:
> levels(f5)_c("Female","Male") > f5 [1] Male Female Female Female Male Male Levels: Female MaleOne thing you can do with a factor is to tabulate the counts of each category with the table() function:
> table(f5) Female Male 3 3 3 of each!