根据R中的频率排序

时间:2018-04-17 16:56:02

标签: r sorting dplyr frequency

structure(list(MSISDN = c(800, 800, 783, 
975, 800)), .Names = "Number", row.names = c(NA, 
-5L), class = "data.frame")

我在R中有以下数据帧,我需要按输出频率排序每个数字的计数

输出应为

Num Freq

800 3

975 1

783 1

3 个答案:

答案 0 :(得分:1)

这应该有用。

<强>基

df <- data.frame(table(xx$Number))
df[rev(order(df$Freq)),]

<强>结果

  #    Var1 Freq
  #    800    3
  #    975    1
  #    783    1

您也可以使用dplyr进行排序。

library(dplyr)
df %>% arrange(desc(Freq))

数据

xx <- structure(list(MSISDN = c(800, 800, 783, 
975, 800)), .Names = "Number", row.names = c(NA, 
-5L), class = "data.frame")

答案 1 :(得分:1)

从管理员程序中检出Tabyl功能。它完成了您想要的任务,再加上一点

library(janitor)
    ds <- structure(list(MSISDN = c(800, 800, 783,975, 800)), .Names = "Number", row.names = c(NA,-5L), class = "data.frame")

    tabyl(ds$Number)

答案 2 :(得分:0)

仅使用dplyr

func getOrCreateRecord(uid: String) -> Records{
    var record: Records?
    do {
        let fetchRequest : NSFetchRequest<Records> = Records.createFetchRequest()
        fetchRequest.predicate = NSPredicate(format: "uid = %@", uid)
        let result: [Records] = try container.viewContext.fetch(fetchRequest)
        record = result.first
    } catch {
        print(error.localizedDescription)
    }

    return record ?? Records(context: container.viewContext)
}