2015-09-16 29 views
0

讓我們說我有尺寸NXM的表,我婉找到排名中他們是如何增加獨特的行數方面列有系統的方式,最好是在R.如何根據它們增加多少個唯一行來對列進行排名?

+1

請提供一個小示例數據和基於此的預期輸出。 – akrun

+1

當你問一個問題時,提供[可重現的例子](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610)是一個好習慣 – Jaap

回答

0
library(dplyr) 

test = data_frame(a = c(1, 1, 1), 
        b = c(1, 2, 2), 
        c = c(1, 2, 3)) 

base = test %>% distinct 

nrow(base) - 
    names(base) %>% sapply(function(name) 
    base %>% 
     select_("-" %>% paste(name)) %>% 
     distinct %>% 
     nrow) 
1

試試這個例如:

#dummy data 
df <- data.frame(a = c(1, 1, 1, 1), 
       b = c(1, 2, 3, 4), 
       c = c(1, 2, 2, 4)) 
# a b c 
# 1 1 1 1 
# 2 1 2 2 
# 3 1 3 2 
# 4 1 4 4 

#re order data.frame 
df[,order(sapply(colnames(df), function(i) length(unique(df[,i]))),decreasing = TRUE)] 
# b c a 
# 1 1 1 1 
# 2 2 2 1 
# 3 3 2 1 
# 4 4 4 1 
相關問題