如何将矩阵的字符列转换为 R 中的数字?
如果我们有一个包含字符列的矩阵,并且我们想将单列转换为数字,那么我们首先需要使用 as.data.frame 函数将矩阵转换为数据框,然后该as.numeric函数可用于将特定列更改为数字类型,如以下示例所示。
示例 1
以下代码段创建了一个矩阵 -
M1<-matrix(factor(rpois(25,5)),ncol=5)M1
创建以下矩阵 -
[,1][,2][,3][,4][,5][1,] "1" "2" "9" "8" "8"
[2,] "8" "5" "5" "9" "6"
[3,] "6" "5" "5" "2" "6"
[4,] "4" "2" "4" "8" "5"
[5,] "2" "5" "6" "5" "10"
将以下代码添加到上述代码段中 -
M1<-matrix(factor(rpois(25,5)),ncol=5)M1<-as.data.frame(M1)
M1[,1]<-as.numeric(M1[,1])
M1
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
V1 V2 V3 V4 V51 1 2 9 8 8
2 8 5 5 9 6
3 6 5 5 2 6
4 4 2 4 8 5
5 2 5 6 5 10
要将矩阵的字符列转换为 R 中的数字,请将以下代码添加到上述代码段中 -
M1<-matrix(factor(rpois(25,5)),ncol=5)输出结果M1<-as.data.frame(M1)
M1[,1]<-as.numeric(M1[,1])
str(M1)
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
'data.frame': 5 obs. of 5 variables:$ V1: num 1 8 6 4 2
$ V2: chr "2" "5" "5" "2" ...
$ V3: chr "9" "5" "5" "4" ...
$ V4: chr "8" "9" "2" "8" ...
$ V5: chr "8" "6" "6" "5" ...
示例 2
以下代码段创建了一个矩阵 -
M2<-matrix(factor(rpois(80,10)),ncol=4)M2
创建以下矩阵 -
[,1] [,2] [,3] [,4][1,] "14" "18" "20" "7"
[2,] "10" "8" "9" "13"
[3,] "5" "7" "10" "9"
[4,] "10" "9" "12" "12"
[5,] "13" "15" "9" "5"
[6,] "5" "6" "8" "9"
[7,] "12" "5" "13" "10"
[8,] "9" "17" "4" "17"
[9,] "8" "8" "17" "7"
[10,] "8" "11" "8" "17"
[11,] "16" "9" "6" "14"
[12,] "10" "15" "11" "12"
[13,] "6" "9" "8" "3"
[14,] "13" "10" "14" "13"
[15,] "17" "11" "9" "9"
[16,] "13" "11" "12" "8"
[17,] "13" "10" "12" "12"
[18,] "8" "12" "10" "8"
[19,] "11" "5" "10" "8"
[20,] "12" "8" "8" "9"
将以下代码添加到上述代码段中 -
M2<-matrix(factor(rpois(80,10)),ncol=4)M2<-as.data.frame(M2)
M2[,4]<-as.numeric(M2[,4])
M2
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
V1 V2 V3 V41 14 18 20 7
2 10 8 9 13
3 5 7 10 9
4 10 9 12 12
5 13 15 9 5
6 5 6 8 9
7 12 5 13 10
8 9 17 4 17
9 8 8 17 7
10 8 11 8 17
11 16 9 6 14
12 10 15 11 12
13 6 9 8 3
14 13 10 14 13
15 17 11 9 9
16 13 11 12 8
17 13 10 12 12
18 8 12 10 8
19 11 5 10 8
20 12 8 8 9
要将矩阵的字符列转换为 R 中的数字,请将以下代码添加到上述代码段中 -
M2<-matrix(factor(rpois(80,10)),ncol=4)输出结果M2<-as.data.frame(M2)
M2[,4]<-as.numeric(M2[,4])
str(M2)
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
'data.frame': 20 obs. of 4 variables:$ V1: chr "14" "10" "5" "10" ...
$ V2: chr "18" "8" "7" "9" ...
$ V3: chr "20" "9" "10" "12" ...
$ V4: num 7 13 9 12 5 9 10 17 7 17 ...
示例 3
以下代码段创建了一个矩阵 -
M3<-matrix(factor(rpois(100,5)),ncol=5)M3
创建以下矩阵 -
[,1] [,2] [,3] [,4] [,5][1,] "1" "5" "3" "5" "6"
[2,] "4" "5" "8" "10" "10"
[3,] "6" "3" "5" "2" "4"
[4,] "7" "5" "3" "4" "5"
[5,] "5" "4" "5" "8" "3"
[6,] "4" "5" "8" "3" "8"
[7,] "3" "5" "0" "4" "5"
[8,] "11" "10" "1" "8" "2"
[9,] "4" "4" "5" "3" "5"
[10,] "8" "7" "3" "3" "7"
[11,] "5" "6" "5" "6" "3"
[12,] "8" "12" "6" "4" "4"
[13,] "5" "6" "5" "9" "5"
[14,] "9" "7" "3" "5" "2"
[15,] "6" "4" "7" "6" "4"
[16,] "6" "4" "4" "4" "7"
[17,] "4" "3" "3" "4" "5"
[18,] "2" "4" "3" "2" "2"
[19,] "3" "4" "3" "6" "4"
[20,] "6" "8" "6" "6" "6"
将以下代码添加到上述代码段中 -
M3<-matrix(factor(rpois(100,5)),ncol=5)M3<-as.data.frame(M3)
M3[,1]<-as.numeric(M3[,1])
M3
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
V1 V2 V3 V4 V51 1 5 3 5 6
2 4 5 8 10 10
3 6 3 5 2 4
4 7 5 3 4 5
5 5 4 5 8 3
6 4 5 8 3 8
7 3 5 0 4 5
8 11 10 1 8 2
9 4 4 5 3 5
10 8 7 3 3 7
11 5 6 5 6 3
12 8 12 6 4 4
13 5 6 5 9 5
14 9 7 3 5 2
15 6 4 7 6 4
16 6 4 4 4 7
17 4 3 3 4 5
18 2 4 3 2 2
19 3 4 3 6 4
20 6 8 6 6 6
要将矩阵的字符列转换为 R 中的数字,请将以下代码添加到上述代码段中 -
M3<-matrix(factor(rpois(100,5)),ncol=5)输出结果M3<-as.data.frame(M3)
M3[,1]<-as.numeric(M3[,1])
str(M3)
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
'data.frame': 20 obs. of 5 variables:$ V1: num 1 4 6 7 5 4 3 11 4 8 ...
$ V2: chr "5" "5" "3" "5" ...
$ V3: chr "3" "8" "5" "3" ...
$ V4: chr "5" "10" "2" "4" ...
$ V5: chr "6" "10" "4" "5" ...
以上是 如何将矩阵的字符列转换为 R 中的数字? 的全部内容, 来源链接: utcz.com/z/347481.html