簡體   English   中英

查找大於0的最小值

[英]Find minimum value greater than 0

我有一個數據框,其中包含帶有一些NA的數值1:4。 對於每一行,我想計算出現次數最少的大於0的值的頻率(百分比)。

這是一個示例數據框架。

    df = as.data.frame(rbind(c(1,2,1,2,2,2,2,1,NA,2),c(2,3,3,2,3,3,NA,2,NA,NA),c(4,1,NA,NA,NA,1,1,1,4,4),c(3,3,3,4,4,4,NA,4,3,4)))

      V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
    1  1  2  1  2  2  2  2  1 NA   2
    2  2  3  3  2  3  3 NA  2 NA  NA
    3  4  1 NA NA NA  1  1  1  4   4
    4  3  3  3  4  4  4 NA  4  3   4

我有2分正在苦苦掙扎。 1)找到一個大於0的值的最低頻率,以及2)將函數應用於數據幀的每一行。 當我開始使用此功能時,我使用下面的代碼實現了該功能,但似乎並未將其應用於所有行。 我的value.1,value.2等結果對於每一行都是相同的。

    Low_Freq = function(x){
      value.1 = sum(x==1, na.rm=TRUE) #count the number of 1's per row
      value.2 = sum(x==2, na.rm=TRUE) #count the number of 2's per row
      value.3 = sum(x==3, na.rm=TRUE) #count the number of 3's per row
      value.4 = sum(x==4, na.rm=TRUE) #count the number of 4's per row
      num.values = rowSums(!is.na(x), na.rm=TRUE) #count total number of non-NA values in each row

      #what is the minimum frequency value greater than 0 among value.1, value.2, value.3, and value.4 for EACH row?
      min.value.freq = min(cbind(value.1,value.2,value.3,value.4)) 

      out = min.value.freq/num.values #calculate the percentage of the minimum value for each row
    }

    df$Low_Freq = apply(df, 1, function(x))

然后,我開始使用rowSums()計算value.1,value.2,value.3和value.4。 這解決了我為每一行計數value.1,value.2等的問題,但是,我隨后不得不應用該函數而不使用apply()來運行它:

    Low_Freq = function(x){
      value.1 = rowSums(x==1, na.rm=TRUE) #count the number of 1's per row
      value.2 = rowSums(x==2, na.rm=TRUE) #count the number of 2's per row
      value.3 = rowSums(x==3, na.rm=TRUE) #count the number of 3's per row
      value.4 = rowSums(x==4, na.rm=TRUE) #count the number of 4's per row
      num.values = rowSums(!is.na(x), na.rm=TRUE) #count total number of non-NA values in each row

      #what is the minimum frequency value greater than 0 among value.1, value.2, value.3, and value.4 for EACH row?
      min.value.freq = min(cbind(value.1,value.2,value.3,value.4)) 

      out = min.value.freq/num.values #calculate the percentage of the minimum value for each row
    }

    df$Low_Freq = Low_Freq(df)

因此,應用於每一行的動作似乎發生在函數本身內。 一切都很好,但是當我去做最后的計算,這將是我的輸出時,我無法弄清楚如何確定值1、2、3或4中哪一行的頻率最低。 該值必須除以每行非NA值的數量。

我期望的結果應如下所示:

      V1 V2 V3 V4 V5 V6 V7 V8 V9 V10  Low_Freq
    1  1  2  1  2  2  2  2  1 NA   2 0.3333333
    2  2  3  3  2  3  3 NA  2 NA  NA 0.4285714
    3  4  1 NA NA NA  1  1  1  4   4 0.4285714
    4  3  3  3  4  4  4 NA  4  3   4 0.4444444

我覺得我似乎正在用這個看似簡單的功能盤旋。 任何幫助,將不勝感激。

謝謝。

table函數將返回出現的每個值的頻率,而忽略NA值。 因此, min的的table結果是,你行中示出了一個值的最小頻率,並且總和是非的數目NA您的行中的值。

Low_Freq = function(x){
  tab = table(x)
  return(min(tab) / sum(tab))
}
df$Low_Freq = apply(df, 1, Low_Freq)
df
#   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10  Low_Freq
# 1  1  2  1  2  2  2  2  1 NA   2 0.3333333
# 2  2  3  3  2  3  3 NA  2 NA  NA 0.4285714
# 3  4  1 NA NA NA  1  1  1  4   4 0.4285714
# 4  3  3  3  4  4  4 NA  4  3   4 0.4444444

如果您不希望分子使用5s,而是使用分母,則可以執行以下操作:

df = as.data.frame(rbind(c(1,2,1,2,2,2,2,1,NA,2),c(2,3,3,2,3,3,NA,2,NA,NA),c(4,1,NA,NA,NA,1,1,1,4,4),c(3,3,3,4,4,4,5,4,3,4)))
Low_Freq = function(x){
  tab = table(x[x != 5])
  return(min(tab) / sum(!is.na(x)))
}
df$Low_Freq = apply(df, 1, Low_Freq)
df
#   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10  Low_Freq
# 1  1  2  1  2  2  2  2  1 NA   2 0.3333333
# 2  2  3  3  2  3  3 NA  2 NA  NA 0.4285714
# 3  4  1 NA NA NA  1  1  1  4   4 0.4285714
# 4  3  3  3  4  4  4  5  4  3   4 0.4000000

暫無
暫無

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

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