我在查找數字中最高的重複數字的頻率時遇到了麻煩。我想要輸出如下:R中的數字中最重複的數字的頻率
Number Output
1111125436 5
9999266613 4
2346275210 3
1234567890 1
依此類推。
我試過freqency
,Biostrings
但是做不到。感謝你的幫助。
我在查找數字中最高的重複數字的頻率時遇到了麻煩。我想要輸出如下:R中的數字中最重複的數字的頻率
Number Output
1111125436 5
9999266613 4
2346275210 3
1234567890 1
依此類推。
我試過freqency
,Biostrings
但是做不到。感謝你的幫助。
一種可能base
R染料溶液:
df <- data.frame(Number = c(1111125436, 9999266613, 2346275210, 1234567890))
df$Output <- sapply(df$Number, function(x) tail(sort(table(strsplit(as.character(x), ''))), 1))
df
# Number Output
# 1 1111125436 5
# 2 9999266613 4
# 3 2346275210 3
# 4 1234567890 1
也很可能比這更好的方式,但分割數爲字符串和表ING似乎是一個可能性:
vapply(strsplit(as.character(dat$Number),""), function(x) max(table(x)), FUN.VALUE=1L)
#[1] 5 4 3 1
這裏是stri_count
和pmax
library(stringi)
do.call(pmax,lapply(0:9, stri_count_fixed, str=df1$Number))
#[1] 5 4 3 1
另一種選擇
或用rowMaxs/stri_count
library(matrixStats)
rowMaxs(sapply(0:9, stri_count_fixed, str=df1$Number))
#[1] 5 4 3 1
@thelatemail感謝注意到,。 –
這個問題可以在O(n)時間使用Moore的線性時間多數表決算法http://www.cs.utexas.edu/~moore/best-ideas/mjrty/完成。 Python實現在這裏:http://stackoverflow.com/questions/27652492/python-find-majority-number-in-on-time-and-o1-memory – kilojoules
@kilojoules R的問題。 –