問題描述
數值取決於所應用的應用系列,R (Numeric values depending of apply family applied, R)
我有一個包含一些數字和字符列的表格,一些是因子和其他整數。
>additional.metadata
sample_id patient_id condition SOM test
1387 1 1 CTL 22 1
7588 1 1 CTL 35 2
7429 1 1 CTL 23 3
7600 1 1 CTL 35 4
我正在嘗試將整個表格轉換為矩陣,並且取決於 apply使用了
選項(即 apply
與 sapply
),來自 $SOM 的一些值會發生變化。這是一個示例:
> apply(additional.metadata, 2, function(x) as.numeric(as.factor(x)))
sample_id patient_id condition SOM test
[1,] 1 1 1 2 1
[2,] 1 1 1 4 2
[3,] 1 1 1 3 3
[4,] 1 1 1 4 4
[5,] 1 1 1 1 5
[6,] 1 1 1 3 6
> sapply(additional.metadata, function(x) as.numeric(as.factor(x)))
sample_id patient_id condition SOM test
[1,] 1 1 1 22 1
[2,] 1 1 1 35 2
[3,] 1 1 1 23 3
[4,] 1 1 1 35 4
[5,] 1 1 1 11 5
[6,] 1 1 1 23 6
有人知道我遺漏了什麼/誤解了什麼,好嗎?提前致謝。
參考解法
方法 1:
Most likely the changes are happening because of as.numeric(as.factor(x))
.
To make sure your value stay as intended you need to convert to character
or skip the factor stage at all.
Use as.numeric(as.character(as.factor(x)))
or as.numeric(as.character(x))
instead.
An explanation of why you need to do this can be foudn on the top answer of this question:
Changing values when converting column type to numeric
(by jgarces、Andrew Haynes)