r中将数据框中数据类型转化

Before we delve deeper into R programming, it is important to understand the various data types used in R. R has the following basic data types:

在深入研究R编程之前 ,了解R中使用的各种数据类型很重要。R具有以下基本数据类型:

  • Character字符
  • Numeric数字
  • Integer整数
  • Logical逻辑上
  • Complex复杂

Apart from these basic types, R also supports several data structures that are constructed from the basic types. These are:

除了这些基本类型,R还支持从基本类型构造的几种数据结构。 这些是:

  • Vectors向量
  • Matrices矩阵
  • Arrays数组
  • Dataframes数据框
  • Lists清单
  • Factors因素

This tutorial will help you understand the basic data types. The working of each complex data structure will be covered in more elaborate tutorials.

本教程将帮助您了解基本数据类型。 每个复杂数据结构的工作都将在更详尽的教程中介绍。

R中的数值数据类型 (Numeric Data Types in R)

Since R is predominantly a language designed for statistical purpose, numeric and integer data types are widely used. We will begin with the most frequently used numeric data type. As the name indicates, numeric corresponds to any numerical value. These values can be floating-point, decimal or double.

由于R主要是为统计目的而设计的语言,因此广泛使用数字和整数数据类型。 我们将从最常用的数字数据类型开始。 顾名思义,数值对应于任何数值。 这些值可以是浮点数,十进制数或双精度数。

Before we assign our first variable, please note that the assignment operator in R is <- . Less than symbol followed by a hyphen.

在分配第一个变量之前,请注意,R中的赋值运算符为<- 。 小于符号,后跟连字符。

Begin by assigning random numeric values to variables a, b, and c. The typeof() function in R gives how the variable is stored internally, whereas the class() function tells us what data type class this variable belongs to. R also offers some checking functions starting with “is“. The function is.numeric() when called upon a variable returns a Boolean value of whether the object is of the numeric data type. There are respective is functions for other classes as well.

首先为变量a,b和c分配随机数值。 R中的typeof()函数给出了变量在内部的存储方式,而class()函数则告诉我们该变量属于哪种数据类型。 R还提供了一些以“ is ”开头的检查功能。 调用变量时,函数is.numeric()返回布尔值,该布尔值表示对象是否为数字数据类型。 其他类别也有各自的功能。

Run the following code in your script editor. Place the cursor on the first line and execute each line pressing Ctrl+ Enter. Also observe how these variables start to become a part of your environment window as you run each line.

在脚本编辑器中运行以下代码。 将光标放在第一行上,然后按Ctrl + Enter执行每一行。 还要观察在运行每一行时这些变量如何开始成为环境窗口的一部分。


a <- 3
b <- 5.4
c <- -9.5
typeof(a)
typeof(b)
typeof(c)
is.integer(a)
is.numeric(b)
is.numeric(c)
class(a)

Output:

输出:


> a <- 3
> b <- 5.4
> c <- -9.5
> typeof(a)
[1] "double"
> typeof(b)
[1] "double"
> typeof(c)
[1] "double"
> is.integer(a)
[1] FALSE
> is.numeric(b)
[1] TRUE
> is.numeric(c)
[1] TRUE
> class(a)
[1] "numeric"

Note how all the variables are stored as double in the internal storage, irrespective of being defined with floating-point or not, as shown by the typeof() function. The variable a with value 3 is stored as 3.0, a double value. The function call is.integer() returns a FALSE Boolean value because of this. The class(a) line gives our variable’s type numeric.

请注意,如何将所有变量都以双精度形式存储在内部存储中,而不管是否用浮点定义,如typeof()函数所示。 值为3的变量a被存储为3.0,即双精度值。 因此,函数调用is.integer()返回FALSE布尔值。 class(a)行提供了变量的类型numeric

R中的数据类型 (Data Types in R)

整数 (Integer)

Without any specification a numerical value is treated as a numeric data type by R compiler. Therefore, to define an integer variable, it is necessary to specify so. This is done by using the as.integer() function.

在没有任何说明的情况下,R编译器会将数值视为数字数据类型。 因此,要定义一个整数变量,必须指定它。 这可以通过使用as.integer()函数来完成。


a<-as.integer(a)
typeof(a)
is.integer(a)
is.numeric(a)

Soon after you run the first line of the code, you see the value assigned to a become 3L, as opposed to 3. The suffix L signifies a long integer of range -2*10ˆ9 to +2*10ˆ9. The variable a is both an integer and a numeric, since all integers are numerics, but not vice versa.

运行代码的第一行后不久,您会看到分配给a的值变成3L,而不是3。后缀L表示范围为-2 * 10ˆ9到+ 2 * 10ˆ9的长整数。 变量a既是整数又是数字,因为所有整数都是数字,但反之亦然。

Output:

输出:


> a<-as.integer(a)
> typeof(a)
[1] "integer"
> is.integer(a)
[1] TRUE
> is.numeric(a)
[1] TRUE

字符 (Character)

The character data type in R is meant to be used for both strings and single characters. A character can be simply specified by using a single or double quotes pair as below.

R中的字符数据类型旨在用于字符串和单个字符。 可以通过使用单引号或双引号对来简单地指定字符,如下所示。


name="journaldev"
typeof(name)
name2='journaldev2'
typeof(name2)

Output:

输出:


> name="journaldev"
> typeof(name)
[1] "character"
> name2='journaldev2'
> typeof(name2)
[1] "character"

An integer or a numeric type can be converted into a character using as.character() function.

可以使用as.character()函数将整数或数字类型转换为字符。


average=0.558
typeof(average)
average=as.character(average)
typeof(average)

Output:

输出:


> average=0.558
> typeof(average)
[1] "double"
> average=as.character(average)
> typeof(average)
[1] "character"

逻辑上 (Logical )

Logical is the data type used to store Boolean values TRUE and FALSE that arise as a result of logical operations. We have already encountered the TRUE and FALSE outputs above as we called the is.numeric() and is.integer() functions above. Let us verify this using the following statement.

逻辑是用于存储由于逻辑运算而产生的布尔值TRUE和FALSE的数据类型。 我们在上面调用了is.numeric()is.integer()函数时,已经在上面遇到了TRUE和FALSE输出。 让我们使用以下语句对此进行验证。


typeof(is.integer(average))

Output:

输出:


> typeof(is.integer(average))
[1] "logical"

Similarly, variables of logical type can be used to work with all logical operations.

同样,逻辑类型的变量可用于所有逻辑运算。


x=TRUE; y=FALSE
x&y #Logical AND
x|y #Logical OR
!x #Logical NOT
!y #Logical NOT

Output:

输出:


> x=TRUE; y=FALSE
> x&y
[1] FALSE
&gt; x|y
[1] TRUE
> !x
[1] FALSE
> !y
[1] TRUE

复杂 (Complex)

The final basic data type we discuss here is the complex type. This is used to represent complex numbers in mathematics. Complex numbers are of the form a+bi where a and b are integers.

我们在这里讨论的最终基本数据类型是复杂类型。 这用于表示数学中的复数。 复数采用a+bi的形式,其中a和b是整数。


complex=4+2i
typeof(complex)

Output:

输出:


> complex=4+2i
> typeof(complex)
[1] "complex"

If we wish to calculate the square root of -1, which is mathematically known as i, the imaginary component of the imaginary number, We cannot calculate it by using normal sqrt (square root) function.

如果我们想计算-1的平方根,这在数学上称为i ,就是虚数的虚数分量,那么我们就不能通过使用普通的sqrt (平方根)函数来计算它。


typeof(sqrt(-1)) #Throws a warning message.

Output:

输出:


Warning message:
In sqrt(-1) : NaNs produced

This is possible by defining -1 as a complex number first.

通过首先将-1定义为复数,这是可能的。


sqrt(-1+0i)
typeof(sqrt(-1+0i))

Output:

输出:


> sqrt(-1+0i)
[1] 0+1i
> typeof(sqrt(-1+0i))
[1] "complex"

This ends our discussion of the basic data types in R.

至此,我们对R中基本数据类型的讨论结束了。

翻译自: https://www.journaldev.com/34377/data-types-in-r

r中将数据框中数据类型转化

r中将数据框中数据类型转化_R中的数据类型相关推荐

  1. java中将查询数据导出_如何在R中将数据框导出到Excel

    java中将查询数据导出 What if I tell, that you can export data frames to excel in R within a couple of minute ...

  2. R语言数据框中的stringsAsFactors参数

    R语言数据框中的stringsAsFactors参数 数据框基本建立 参数:stringsAsFactors 数据框基本建立 (注:本文章中的Name变量区分大小写) Name <- c(&qu ...

  3. Scrapy中将数据保存到Excel和MySQL中

    目录标题 1. Excel 1.1 openpyxl 1.1.1 代码说明 1.1.2 注意 1.2 pandas 1.2.1 代码说明 1.2.2 常见错误 1.3 openpyxl和pandas对 ...

  4. java离群值,R从数据框中按组别移除离群值

    离群值(outlier)通常被定义为小于 QL - l.5 IQR 或者 大于 Qu + 1.5 IQR的值,QL称为下四分位数, Qu称为上四分位数,IQR称为四分位数间距,是Qu上四分位数和QL下 ...

  5. 【R】数据框data.frame的基本操作【1】

    data.frame(数据框)是R语言中的一种数据结构,下面将从多个方面介绍data.frame的基本操作. 1.建立数据框 ID<-c(1,2,3,4) age<-c(23,29,34, ...

  6. android r中的变量_R中的变量

    android r中的变量 Variables in R are the same as the notion of variables in any other programming langua ...

  7. R语言 如何搭建和操作R语言数据框 R语言基础知识rbind,cbind,merge和str用法

    数据框是一种特殊的数据结构,它通常用来存储数据表格或电子表格形式的数据.数据框中的毎一列代表一个特定的属性或字段,而行由这些列的具体取值组成.这种数据结构在处理有大量字段和属性的数据集时十分有用. 建 ...

  8. R语言数据框中创建新的变量的三种方法

    方法1 > mydata<-data.frame(x1=c(1,2,3,4),x2=c(5,6,7,8)) > mydata$sum<-mydata$x1+mydata$x2 ...

  9. r语言中检测异常值_R中的异常值检测

    r语言中检测异常值 介绍 (Introduction) An outlier is a value or an observation that is distant from other obser ...

最新文章

  1. char[]数组与char *指针的区别
  2. 用springmvc作接口时返回json数据中文乱码
  3. 电气期刊论文实现:基于改进遗传算法的电力机组组合(程序讲解)
  4. Flutter文本输入框TextField控制器TextEditingController,TextField预设内容,获取TextField中的输入内容,兼听TextField中的内容变化
  5. alexa api php,php 读取 alexa信息
  6. pass by value pass by reference
  7. 在线修改域控的IP和机器名
  8. 组合,聚合,关联,依赖的区别
  9. mysql查询4-6_MySQL学习(四)查询
  10. 使用Liquid实现简单的数据交换
  11. Word 关闭拼写检查 (去掉Word中拼写检查的所有红色和绿色的浪线)
  12. centos7安装N卡驱动和conda pytorch1.7.1深度学习环境
  13. Python语言画蓝色妖姬
  14. 前端渲染和后端渲染,要说的都在这里?
  15. python 爬虫如何使用代理IP
  16. 利用git提交网站到码云出现权限问题,弹框要求输入用户名和密码老是出错
  17. 怎么测试t470p性能软件,ThinkPad T470p值得买吗?ThinkPad T470p商务本全面详细评测图解...
  18. 《视觉SLAM十四讲 第二版》笔记及课后习题(第一讲)
  19. React基础学习笔记(一)-react前端项目的两种搭建方式
  20. java 地址反查邮编_地址查邮编示例代码

热门文章

  1. 概率论与数理统计 第一周作业
  2. wms仓库管理系统,进销存和ERP系统三者之间联系
  3. python创建excel文件报错_python-通过openpy操作excel
  4. IDC预测华为最快今年超苹果丨国家网络安全中心喊话OFFICE,请尽快升级补丁【软件网每日新闻播报│第10-17期】
  5. 云服务器显示内存不足怎么调整,云服务器扩充内存
  6. Kubernetes Deployment控制器(二十)
  7. 嗅探HTTP网页用户账户密码
  8. 二维火视频菜单,开启沉浸式点餐新体验
  9. qq客服不需要加好友的html方法
  10. 反三角函数之间的关系以及导数关系