r的齐鲁风采群英会类型

Antoine Soetewey 2019-12-30 5 minute read

本文介绍了R.中的不同齐鲁风采群英会类型,从统计角度来看,了解不同的变量类型,阅读“ 可变类型和示例 “。

R中存在哪些齐鲁风采群英会类型?

R中有6种最常见的齐鲁风采群英会类型:

  1. 数字
  2. 整数
  3. 复杂的
  4. 特点
  5. 因素
  6. 逻辑

R中的齐鲁风采群英会集通常是这6种不同齐鲁风采群英会类型的组合。下面我们更详细地探索每个齐鲁风采群英会类型,除了齐鲁风采群英会类型“复杂”,因为我们专注于主要的齐鲁风采群英会类型,并且这种齐鲁风采群英会类型很少在实践中使用。

数字

R中最常见的齐鲁风采群英会类型是数字。如果值是数字,或者如果值包含小数,则变量或串将被存储为数字齐鲁风采群英会。例如,以下两个系列默认存储为数字:

# numeric series without decimals
num_data <- c(3, 7, 2)
num_data
## [1] 3 7 2
class(num_data)
## [1] "numeric"
# numeric series with decimals
num_data_dec <- c(3.4, 7.1, 2.9)
num_data_dec
## [1] 3.4 7.1 2.9
class(num_data_dec)
## [1] "numeric"
# also possible to check the class thanks to str()
str(num_data_dec)
##  num [1:3] 3.4 7.1 2.9

换句话说,如果将一个或多个数字分配给r中的对象,则默认情况下将存储为数字(具有小数的数字),除非另有指定。

整数

整数 齐鲁风采群英会类型实际上是数字齐鲁风采群英会的特殊情况。整数是没有小数的数字齐鲁风采群英会。如果您确定您存储的数字永远不会包含小数,则可以使用它。例如,让我们说你对10个家庭的样本中的孩子数量感兴趣。此变量是一个离散变量(请参阅提醒 变量类型 if you do not remember what is a discrete variable) and will never have decimals. Therefore, it can be stored as integer data thanks to the as.integer() command:

children
##  [1] 1 3 2 2 4 4 1 1 1 4
children <- as.integer(children)
class(children)
## [1] "integer"

请注意,如果您的变量没有小数,则R会自动将类型设置为整数而不是数字。

特点

The data type character is used when storing text, known as strings in R. The simplest ways to store data under the character format is by using "" around the piece of text:

char <- "some text"
char
## [1] "some text"
class(char)
## [1] "character"

If you want to force any kind of data to be stored as character, you can do it by using the command as.character():

char2 <- as.character(children)
char2
##  [1] "1" "3" "2" "2" "4" "4" "1" "1" "1" "4"
class(char2)
## [1] "character"

Note that everything inside "" will be considered as character, no matter if it looks like character or not. For example:

chars <- c("7.42")
chars
## [1] "7.42"
class(chars)
## [1] "character"

此外,一旦在变量或向量中存在至少一个字符值,整个变量或向量将被视为字符:

char_num <- c("text", 1, 3.72, 4)
char_num
## [1] "text" "1"    "3.72" "4"
class(char_num)
## [1] "character"

最后但并非最不重要的是,虽然空间在数字齐鲁风采群英会中无关紧要,但它对字符齐鲁风采群英会确实很重要:

num_space <- c(1)
num_nospace <- c(1)
# is num_space equal to num_nospace?
num_space == num_nospace
## [1] TRUE
char_space <- "text "
char_nospace <- "text"
# is char_space equal to char_nospace?
char_space == char_nospace
## [1] FALSE

As you can see from the results above, a space within character data (i.e., within "") makes it a different string in R!

因素

因子变量是一个特殊的字符变量,它也包含文本。但是,当存在有限数量的唯一字符串时使用因子变量。它经常代表一个 分类变量 . For instance, the gender will usually take on only two values, “female” or “male” (and will be considered as a factor variable) whereas the name will generally have lots of possibilities (and thus will be considered as a character variable). To create a factor variable use the factor() function:

gender <- factor(c("female", "female", "male", "female", "male"))
gender
## [1] female female male   female male  
## Levels: female male

To know the different levels of a factor variable, use levels():

levels(gender)
## [1] "female" "male"

By default, the levels are sorted alphabetically. You can reorder the levels with the argument levels in the factor() function:

gender <- factor(gender, levels = c("male", "female"))
levels(gender)
## [1] "male"   "female"

特点 strings can be converted to factors with as.factor():

text <- c("test1", "test2", "test1", "test1") # create a character vector
class(text) # to know the class
## [1] "character"
text_factor <- as.factor(text) # transform to factor
class(text_factor) # recheck the class
## [1] "factor"

The character strings have been transformed to factors, as shown by its class of the type factor.

逻辑

A logical variable is a variable with only two values; TRUE or FALSE:

value1 <- 7
value2 <- 9

# is value1 greater than value2?
greater <- value1 > value2
greater
## [1] FALSE
class(greater)
## [1] "logical"
# is value1 less than or equal to value2?
less <- value1 <= value2
less
## [1] TRUE
class(less)
## [1] "logical"

It is also possible to transform logical data into numeric data. After the transformation from logical to numeric with the as.numeric() command, FALSE values equal to 0 and TRUE values equal to 1:

greater_num <- as.numeric(greater)
sum(greater)
## [1] 0
less_num <- as.numeric(less)
sum(less)
## [1] 1

Conversely, numeric data can be converted to logical data, with FALSE for all values equal to 0 and TRUE for all other values.

x <- 0
as.logical(x)
## [1] FALSE
y <- 5
as.logical(y)
## [1] TRUE

谢谢阅读。我希望这篇文章有助于您了解R及其特殊性的基本齐鲁风采群英会类型。如果您想从统计的角度来了解有关不同的变量类型的信息,请阅读文章“ 可变类型和示例 “。

一如既往,如果您有问题或与本文所涵盖的主题相关的建议,请将其添加为评论,以便其他读者可以从讨论中受益。



喜欢这篇文章?

获取更新 每次发布新文章。
任何垃圾邮件都没有任何垃圾邮件。
分享: