字符()和“”之间的差异在R [英] Differences between character() and "" in R
问题描述
只是意识到输出是不同的:
Just realize the output is different:
> y=""
> y
[1] ""
> y=character()
> y
character(0)
但是,没有什么奇怪的事情发生了。我不清楚这些差异,并希望保持这个问题(如果有的话)清楚。
However, nothing odd has happened. And I am not clear about these differences, and want to keep this problem(if any) clear in mind. So, thank you for helping.
推荐答案
您会混淆向量的长度(元素数)与字符数在字符串中:
You are confusing the length (number of elements) of a vector with the number of characters in a string:
考虑以下三件事:
> 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
要获得字符数, code> nchar :
To get the number of characters, use nchar
:
> nchar(x)
[1] 0 0
> nchar(y)
[1] 0
> nchar(z)
integer(0)
$ b (x)显示 x
的每个元素中有多少个字母,因此返回两个零的整数向量。 nchar(y)
则是一个零的整数向量。
Note that nchar(x)
shows how many letters in each element of x
, so it returns an integer vector of two zeroes. nchar(y)
is then an integer vector of one zero.
所以最后一个, nchar(z)
返回 integer(0)
,这是一个无零的整数向量。它的长度为零。它没有元素,但如果它有元素,它们将是整数。
So the last one, nchar(z)
returns an integer(0)
, which is an integer vector of no zeroes. It has length of zero. It has no elements, but if it did have elements, they would be integers.
字符(0)
是字符类型对象的空向量。比较:
character(0)
is an empty vector of character-type objects. Compare:
> character(0)
character(0)
> character(1)
[1] ""
> character(2)
[1] "" ""
> character(12)
[1] "" "" "" "" "" "" "" "" "" "" "" ""
这篇关于字符()和“”之间的差异在R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!