简体   繁体   English

R-数字矩阵

[英]R - as.numeric matrix

I am new to R and I am trying to convert a dataframe to a numeric matrix using the below code 我是R的新手,正在尝试使用以下代码将数据框转换为数值矩阵

expData <- read.table("GSM469176.txt",header = F)
expVec <- as.numeric(as.matrix(exp_data))

When I use as.matrix, without as.numeric, it returns some numbers (as below) 当我使用as.matrix而不使用as.numeric时,它返回一些数字(如下所示)

0.083531    0.083496    0.083464    0.083435    0.083406    0.083377    0.083348"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
[9975] "-0.00285  -0.0028274  -0.0028046  -0.0027814  -0.0027574  -0.0027319  -0.0027042  

but when I put in the as.numeric, they are all converted to "NA" 但是当我输入as.numeric时,它们都被转换为“ NA”

I apologize if someone has asked this question before but I can't find a post that solves my problem. 如果有人之前曾问过这个问题,我深感抱歉,但是找不到解决我问题的帖子。 Thanks in advance 提前致谢

You have 2 issues. 您有2期。 First, if you examine the structure of the data frame, you'll note that the first column is characters: 首先,如果您检查数据框的结构,您会注意到第一列是字符:

head(expData)[, 1:4]

            V1         V2         V3         V4
1 YAL002W(cer) 6.1497e-02 6.2814e-02 6.4130e-02
2 YAL002W(par) 7.1352e-02 7.3262e-02 7.5171e-02
3 YAL003W(cer) 2.2428e-02 3.8252e-02 5.4078e-02
4 YAL003W(par) 2.6548e-02 3.6747e-02 4.6947e-02
5 YAL005C(cer) 2.4023e-05 2.3243e-05 2.2462e-05
6 YAL005C(par) 2.0252e-02 2.0346e-02 2.0440e-02

Therefore, trying to convert the complete data frame to numeric will not work as expected. 因此,尝试将完整的数据框转换为数字将无法正常工作。

Second, you are running as.numeric() after as.matrix(), which is converting the matrix to a vector: 第二,在as.matrix()之后运行as.numeric(),它将矩阵转换为向量:

x <- as.numeric(as.matrix(expData))
# Warning message:
# NAs introduced by coercion 
class(x)
[1] "numeric"
dim(x)
# NULL not a matrix
length(x)
# [1] 14261302

I suggest you try this: 我建议你试试这个:

rownames(expData) <- expData$V1
expData$V1 <- NULL
expData <- as.matrix(expData)
dim(expData)
# [1] 7502 1900
class(expData[, 1])
# [1] "numeric"

You get the NA's when R doesn't know how to convert something to a number. R不知道如何将某物转换为数字时,您会得到NA。

Specifically, the quotation mark in your output tells me that you have one (several) LNG string of numbers. 具体来说,输出中的引号告诉我您有一个(几个)LNG数字字符串。 To see why this is bad, try: as.nmeric("-0.00285 -0.0028274") 要查看为什么这不好,请尝试: as.nmeric("-0.00285 -0.0028274")

I don't know what your raw data is like, but as @alexwhan mentioned, the culprit is probably in your call to read.table 我不知道您的原始数据是什么样的,但是正如@alexwhan所提到的,罪魁祸首可能是您对read.table的调用

To fix it, try explicitly setting the sep argument (ie, next to where you have header) 要解决此问题,请尝试显式设置sep参数(即,在具有标题的位置旁边)

I would suggest opening up the raw file in a simple text editor (TextEdit.app or notepad, not Word) and seeing how they are separated. 我建议在一个简单的文本编辑器(TextEdit.app或记事本,而不是Word)中打开原始文件,并查看它们如何分离。 M guess is 我猜是

   ..., sep="\t" 

should do the trick. 应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM