Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中将字符串转换为数字_R - Fatal编程技术网

在R中将字符串转换为数字

在R中将字符串转换为数字,r,R,关于使用R语言进行数据转换,我有一个问题 我有两个数据存储在变量lung.X和lung.y中,下面是我的数据描述 > str(lung.X) chr [1:86, 1:7129] " 170.0" " 104.0" " 53.7" " 119.0" " 105.5" " 130.0" ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:86] "V3" "V4" "V5" "V6" ... ..$ : chr [1:7

关于使用R语言进行数据转换,我有一个问题

我有两个数据存储在变量lung.X和lung.y中,下面是我的数据描述

> str(lung.X)
 chr [1:86, 1:7129] "  170.0" "  104.0" "   53.7" "  119.0" "  105.5" "  130.0" ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:86] "V3" "V4" "V5" "V6" ...
  ..$ : chr [1:7129] "A28102_at" "AB000114_at" "AB000115_at" "AB000220_at" ...

lung.X
是一个矩阵(行:86列:7129),而
lung.y
是一个数字数组(86个条目)

有人知道如何将上述数据转换成以下格式吗

> str(lung.X)
     num [1:86, 1:7129] 170 104 53.7 119 105.5 130...
我想我应该这样做

lung.X <- as.numeric(lung.X)
这样做的原因是因为我需要
lung.X
仅为数字


谢谢。

尝试一下:
m您可以将
矩阵的
模式
更改为
数值模式

## example data
m <- matrix(as.character(1:10), nrow=2,
            dimnames = list(c("R1", "R2"), LETTERS[1:5]))
m
#    A   B   C   D   E
# R1 "1" "3" "5" "7" "9"
# R2 "2" "4" "6" "8" "10"

str(m)
#  num [1:2, 1:5] 1 2 3 4 5 6 7 8 9 10
#  - attr(*, "dimnames")=List of 2
#   ..$ : chr [1:2] "R1" "R2"
#   ..$ : chr [1:5] "A" "B" "C" "D" ...
# NULL

mode(m) <- "numeric"
str(m)
#  num [1:2, 1:5] 1 2 3 4 5 6 7 8 9 10
#  - attr(*, "dimnames")=List of 2
#   ..$ : chr [1:2] "R1" "R2"
#   ..$ : chr [1:5] "A" "B" "C" "D" ...
# NULL
m
#    A B C D  E
# R1 1 3 5 7  9
# R2 2 4 6 8 10
##示例数据

m Try
lung.X[]只需重新添加维度属性,例如
dim(lung.X)@sgibb,第一个不起作用,但第二个正在工作!谢谢。@joran,你的也在工作!谢谢,没问题,拉菲达。
> str(lung.X)
 num [1:613094] 170 104 53.7 119 105.5 130...
## example data
m <- matrix(as.character(1:10), nrow=2,
            dimnames = list(c("R1", "R2"), LETTERS[1:5]))
m
#    A   B   C   D   E
# R1 "1" "3" "5" "7" "9"
# R2 "2" "4" "6" "8" "10"

str(m)
#  num [1:2, 1:5] 1 2 3 4 5 6 7 8 9 10
#  - attr(*, "dimnames")=List of 2
#   ..$ : chr [1:2] "R1" "R2"
#   ..$ : chr [1:5] "A" "B" "C" "D" ...
# NULL

mode(m) <- "numeric"
str(m)
#  num [1:2, 1:5] 1 2 3 4 5 6 7 8 9 10
#  - attr(*, "dimnames")=List of 2
#   ..$ : chr [1:2] "R1" "R2"
#   ..$ : chr [1:5] "A" "B" "C" "D" ...
# NULL
m
#    A B C D  E
# R1 1 3 5 7  9
# R2 2 4 6 8 10