數值取決於所應用的應用系列,R (Numeric values depending of apply family applied, R)


問題描述

數值取決於所應用的應用系列,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使用了 選項(即 applysapply),來自 $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 jgarcesAndrew Haynes)

參考文件

  1. Numeric values depending of apply family applied, R (CC BY‑SA 2.5/3.0/4.0)

#R #casting #apply






相關問題

如何將均值、標準差等函數應用於整個矩陣 (How to apply mean, sd etc. function to a whole matrix)

Tạo các thùng của mỗi hàng trong bảng và vẽ hình thanh ngăn xếp trong R (Make bins of each table row and draw stack bar figure in R)

Reading not quite correct .csv file in R (Reading not quite correct .csv file in R)

包'treemap'中的線條粗細 (Thickness of lines in Package ‘treemap’)

是否需要帶有 awk 的預處理文件,或者可以直接在 R 中完成? (Is preprocessing file with awk needed or it can be done directly in R?)

rpivotTable 選擇元素下拉菜單 (rpivotTable select elements drop down menu)

優化性能 - Shiny 中的大文件輸入 (Optimizing Performance - Large File Input in Shiny)

數值取決於所應用的應用系列,R (Numeric values depending of apply family applied, R)

如何記錄全年的值? (How to note the values across year?)

R中的線性搜索 (Linear search in R)

在 dplyr/purrr 工作流程中動態連接多個數據集 (Dynamically join multiple datasets in a dplyr/purrr workflow)

如何將行值更改為列名 (R) (How change Row values to Column names (R))







留言討論