官术网_书友最值得收藏!

Variable Types

Variable types exist in all programming languages and will tell the computer how to store and access a variable.

First, know that all variables created in R will have a class and a type. You can look at the class or type of anything in R using the class() and typeof() functions, respectively.

The class of an object is a broad designation, for example, character, numeric, integer, and date. These are very broad categories, and type elaborates more specifically on what type of variable it is, for example, a variable of class date can be of type character or POSIXct, depending on how it is stored. Type drills down into the details of a variable and how it's been stored in R, though sometimes class and type can be the same. For example, integers are of class and type integer, and character strings are of type and class character. Let's examine the following code snippet:

x <- 4.2
class(x)
typeof(x)

The preceding code provides the following output:

In this snippet, x has a class numeric, because it is a number, but also has a type double because it is a decimal number. This is because all numeric data in R is of type double unless the object has been explicitly declared to be an integer. Let's look at some examples of different classes and types.

主站蜘蛛池模板: 浦江县| 思茅市| 梁平县| 绥德县| 福鼎市| 大兴区| 灵璧县| 徐州市| 枝江市| 菏泽市| 靖宇县| 房产| 二连浩特市| 榆林市| 清新县| 嘉义县| 包头市| 土默特左旗| 霍邱县| 广宗县| 高邑县| 莲花县| 分宜县| 紫金县| 安康市| 武鸣县| 乌鲁木齐县| 玛沁县| 慈利县| 苏尼特左旗| 泸州市| 泰顺县| 万山特区| 山丹县| 鸡泽县| 新丰县| 青岛市| 浙江省| 平顶山市| 台北县| 靖宇县|