获得每个p的频率。每个国家的语言

时间:2017-12-29 17:23:23

标签: r

我正在使用以下数据集:

@MessagingGateway

我想总结一下所有的编程语言。 输出应如下所示:

  | Country          | HaveWorkedLanguage
1 | United States    | Swift 
2 | United States    | Python
3 | Austria          | JavaScript 
4 | Austria          | JavaScript
5 | United States    | Swift

我已经玩过 | Country | HaveWorkedLanguage | Frequency 1 | United States | Swift | 2 2 | United States | Python | 1 3 | Austria | JavaScript | 2 ,但无法做到正确。

3 个答案:

答案 0 :(得分:1)

使用dplyr

 df %>% group_by(Country,HaveWorkedLanguage) %>%
           dplyr::summarize(Frequency=n()) %>%
           as.data.frame()

答案 1 :(得分:0)

使用data.table,您执行countgroup by,然后执行unique: -

df <- data.table(Country = c("United States", "United States", "Austria", "Austria", "United States"), HaveWorkedLanguage = c("Swift", "Python", "JavaScript", "JavaScript", "Swift"))
df[, Frequency := .N, by = c("Country", "HaveWorkedLanguage")]
df <- unique(df)

它会为您提供所需的输出: -

   Country         HaveWorkedLanguage Frequency
1: United States   Swift               2
2: United States   Python              1
3: Austria         JavaScript          2

答案 2 :(得分:0)

使用'dplyr'使这个过程变得直观。首先'group_by'你想要总结的东西,然后按如下方式执行摘要:

{{1}}