簡體   English   中英

對R中數據幀的特定行進行規范化

[英]Normalization on specific rows of a dataframe in R

我有一個包含許多列和行的數據框。 我要執行以下操作:

  • 將“ id”列中所有不包含文本“ phos”的行
  • 使用文本“ int_sam”對所有強度列上的這些行(例如,中位居中)進行標准化
  • 使用如上計算的歸一化因子/值,然后以列方式(樣本方式)減去DO列“ id”中包含文本“ phos”的每一行的數據(將數據進行log2轉換)。

提前非常感謝您。 我在R上沒有太多經驗,我也不是統計學家。 因此,可能對R代碼進行簡單的解釋將非常有幫助。 再次感謝。

int_sam_1 = c("2421432", "24242424", "NA", "4684757849", "NA", "10485040", "NA", 
          "6849400", "40300", "NA", "NA", "NA", "556456466", "4646456466", "246464266", "4564242646")
int_sam_2 = c("NA", "5342353", "14532556", "43566", "46367367", "768769769", "797899", "NA", "NA", "NA", 
          "686899", "7898979", "678568", "NA", "68886", "488")
int_sam_3 = c("11351", "NA", "NA", "NA", "1354151345", "1351351354", "314534", "1535", "3145354", "4353455", 
          "324535", "3543445", "34535", "34535534", "NA", "NA")
id = c("phos", "acet phos", "acet", "acet", "acet", "acet meth phos", "phos", "phos", "phos", "phos", "acet", 
   "meth", "meth phos", "phos", "meth phos", "phos")
df = cbind.data.frame(int_sam_1, int_sam_2, int_sam_3, id)

嘗試跟隨

您的數據

int_sam_1 = c(2421432, 24242424, NA, 4684757849, NA, 10485040, NA, 
              6849400, 40300, NA, NA, NA, 556456466, 4646456466, 246464266, 4564242646)
int_sam_2 = c(NA, 5342353, 14532556, 43566, 46367367, 768769769, 797899, NA, NA, NA, 
              686899, 7898979, 678568, NA, 68886, 488)
int_sam_3 = c(11351, NA, NA, NA, 1354151345, 1351351354, 314534, 1535, 3145354, 4353455, 
              324535, 3543445, 34535, 34535534, NA, NA)
id = c("phos", "acet phos", "acet", "acet", "acet", "acet meth phos", "phos", "phos", "phos", "phos", "acet", 
       "meth", "meth phos", "phos", "meth phos", "phos")
df = cbind.data.frame(int_sam_1, int_sam_2, int_sam_3, id)

替換為沒有phos的列並計算全局中位數

df.sub <- df %>% filter(!grepl("phos",id))
df.median <- median(as.vector(as.matrix(df.sub[,1:3])),na.rm = T)

從您有phos的1-3列中的每個值中減去世界中位數

df <- df %>% 
mutate(int_sam_1=ifelse(grepl('phos',id),int_sam_1-df.median, int_sam_1)) %>% 
mutate(int_sam_2=ifelse(grepl('phos',id),int_sam_2-df.median, int_sam_2)) %>%
mutate(int_sam_3=ifelse(grepl('phos',id),int_sam_3-df.median, int_sam_3))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM