Differences between the character () and "" in R
Just realize that the output is different:
> y="" > y [1] "" > y=character() > y character(0) However, nothing strange happened. And I do not quite understand these differences and I want this problem (if any) to be understood. So thanks for the help.
You confuse the length (number of elements) of the vector with the number of characters in the string:
Consider these three things:
> x=c("","") > y="" > z=character() Their length is the number of elements in the vector:
> length(x) [1] 2 > length(y) [1] 1 > length(z) [1] 0 To get the number of characters, use nchar :
> nchar(x) [1] 0 0 > nchar(y) [1] 0 > nchar(z) integer(0) Note that nchar(x) shows the number of letters in each element of x , so it returns an integer vector of two zeros. nchar(y) is an integer vector of one zero.
So the last one, nchar(z) returns integer(0) , which is an integer vector without zeros. It has a length of 0. It has no elements, but if it had elements, they would be integers.
character(0) - an empty vector of character type objects. For comparison:
> character(0) character(0) > character(1) [1] "" > character(2) [1] "" "" > character(12) [1] "" "" "" "" "" "" "" "" "" "" "" "" character(0) is a character type vector with ZERO elements. But "" is a character-type vector with one element that is equal to an empty string.
If y="" , then length(y) - 1 . On the other hand, if y=character() , then length(y) is 0