[英]How to convert a 'string numeric' column into pure numeric in R matrix
提供以下數據:
GOBPID Term col1 col2 col3 col4 col5
1 GO:0001659 temperature_homeostasis 3.496906 0 0 0 0
2 GO:0001660 fever_generation 3.226069 0 0 0 0
我試圖創建一個矩陣,其中col1-5(可能超過5)為數字。
目前它看起來像這樣:
> dat <- read.table("http://dpaste.com/1486837/plain/",header=TRUE)
> as.matrix(as.numeric(dat))
GOBPID Term col1 col2 col3 col4 col5
[1,] "GO:0001659" "temperature_homeostasis" "3.496906" "0" "0" "0" "0"
[2,] "GO:0001660" "fever_generation" "3.226069" "0" "0" "0" "0"
這樣做的方法是什么?
我打算這樣:
GOBPID Term col1 col2 col3 col4 col5
[1,] "GO:0001659" "temperature_homeostasis" 3.496906 0 0 0 0
[2,] "GO:0001660" "fever_generation" 3.226069 0 0 0 0
使用此命令我失敗了: as.numeric(as.matrix(dat))
dat
是data.frame
,而不是matrix
。 data.frame
的列可以具有不同的類型。 在您的示例中,col1-5的類型已經是數字。 親自嘗試一下:
> dat <- read.table("http://dpaste.com/1486837/plain/",header=TRUE)
> sapply(dat, class)
GOBPID Term col1 col2 col3 col4 col5
"factor" "factor" "numeric" "integer" "integer" "integer" "integer"
> dat[,3] + 1
[1] 4.496906 4.226069
> dat[,4] + 1
[1] 1 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.