R语言 转换函数
有时为了使用R分析数据,我们需要将数据转换成另一种数据类型。正如我们所知,R有以下数据类型:数值型、整数型、逻辑型、字符型等,同样,R有各种转换函数,用于转换数据类型。
在R语言中,转换函数有两种类型。
- 数据类型的转换函数
- 数据结构 的转换函数
数据类型的转换函数
有各种转换函数可用于数据类型。这些函数是
- as.numeric()
十进制值在R中称为数值。它是R中实数的默认数据类型。在R中,as.numeric()将任何数值转换为数值。
语法:
// Conversion into numeric data type
as.numeric(x)
示例:
# A simple R program to convert
# character data type into numeric data type
x<-c('1', '2', '3')
# Print x
print(x)
# Print the type of x
print(typeof(x))
# Conversion into numeric data type
y<-as.numeric(x)
# print the type of y
print(typeof(y))
输出:
[1] "1" "2" "3"
[1] "character"
[1] "double"
- as.integer()
在R语言中,Integer数据类型是一个所有整数的集合。为了在R中创建一个整数变量,并将任何数据类型转换为整数,我们使用as.integer()函数。
语法:
// Conversion of any data type into Integer data type
as.integer(x)
示例:
# A simple R program to convert
# numeric data type into integer data type
x<-c(1.3, 5.6, 55.6)
# Print x
print(x)
# Print type of x
print(typeof(x))
# Conversion into integer data type
y<-as.integer(x)
# Print y
print(y)
# Print type of y
print(typeof(y))
输出:
[1] 1.3 5.6 55.6
[1] "double"
[1] 1 5 55
[1] "integer"
- as.character()
在R语言中,字符数据被用来存储字符值和字符串。为了在R中创建一个字符变量,我们调用as.character()函数,如果我们想把任何数据类型转换为字符,我们也可以使用as.character()函数。
语法:
// Conversion of any data type into character data type
as.character(x)
示例:
x<-c(1.3, 5.6, 55.6)
# Print x
print(x)
# Print type of x
print(typeof(x))
# Conversion into character data type
y<-as.character(x)
# Print y
print(y)
# Print type of y
print(typeof(y))
输出:
[1] 1.3 5.6 55.6
[1] "double"
[1] "1.3" "5.6" "55.6"
[1] "character"
- as.logical()
逻辑值是用来比较变量的,这些变量要么返回真,要么返回假。为了比较变量并将任何数值转换成真或假,R使用as.logical()函数。
语法:
// Conversion of any data type into logical data type
as.logical(x)
示例:
x = 3
y = 8
# Conversion in to logical value
result<-as.logical(x>y)
# Print result
print(result)
输出:
[1] FALSE
- as.date()
在R语言中,as.date()函数被用来将字符串转换成日期格式。
语法:
// Print string into date format
as.date(variable, "%m/%d/%y")
示例:
dates <- c("02/27/92", "02/27/92",
"01/14/92", "02/28/92",
"02/01/92")
# Conversion into date format
result<-as.Date(dates, "%m/%d/%y")
# Print result
print(result)
输出:
[1] "1992-02-27" "1992-02-27" "1992-01-14" "1992-02-28" "1992-02-01"
数据结构的转换函数
数据结构有多种转换功能。这些函数是
- as.data.frame()
数据框架用于存储数据表。它是长度相同的向量列表。在R中,有时为了分析数据,我们需要将向量列表转换成数据框。因此,R使用as.data.frame()函数将向量列表转换成数据框架。
语法:
// Conversion of any data structure into data frame
as.data.frame(x)
示例:
x<- list( c('a', 'b', 'c'),
c('e', 'f', 'g'), c('h', 'i', 'j'))
# Print x
print(x)
# Conversion in to data frame
y<-as.data.frame(x)
# Print y
print(y)
输出:
[[1]]
[1] "a" "b" "c"
[[2]]
[1] "e" "f" "g"
[[3]]
[1] "h" "i" "j"
c..a....b....c.. c..e....f....g.. c..h....i....j..
1 a e h
2 b f i
3 c g j
- as.vector()
R有一个函数as.vector(),用于将分布式矩阵转换成非分布式向量。向量产生一个给定长度和模式的向量。
语法:
// Conversion of any data structure into vector
as.vector(x)
示例:
x<-c(a=1, b=2)
# Print x
print(x)
# Conversion into vector
y<-as.vector(x)
# Print y
print(y)
输出:
a b
1 2
[1] 1 2
- as.matrix()
在R中,有一个函数as.matrix(),用来将data.table转换成矩阵,可以选择使用data.table中的一列作为矩阵的行名。
语法:
// Conversion into matrix
as.matrix(x)
示例:
# Importing library
library(data.table)
x <- data.table(A = letters[1:5], X = 1:5, Y = 6:10)
# Print x
print(x)
# Conversion into matrix
z<-as.matrix(x)
# Print z
print(z)
输出:
A X Y
1: a 1 6
2: b 2 7
3: c 3 8
4: d 4 9
5: e 5 10
A X Y
[1,] "a" "1" " 6"
[2,] "b" "2" " 7"
[3,] "c" "3" " 8"
[4,] "d" "4" " 9"
[5,] "e" "5" "10"