0
$\begingroup$

I have a data.frame called world. This data frame lists countries in column name.y, I have a column for languages named languages. I need to List the names of countries where more than 4 languages are spoken.

My first take on this is to aggregate country and languages.

language <- data.frame(table(World$name.y,World$language))

when I run head(language) it doesn't work out.

$\endgroup$
  • $\begingroup$ library(data.table); as.data.table(World)[, .(n=length(unique(language))),by=.(country)][n>=4,] $\endgroup$ – Valentas Dec 6 '18 at 7:36
  • $\begingroup$ Sorry, but there was an error with this one. Error in [.data.table(as.data.table(World), , .(n = length(unique(language))), : column or expression 1 of 'by' or 'keyby' is type list. Do not quote column names. Usage: DT[,sum(colC),by=list(colA,month(colB))] $\endgroup$ – Chris Kehl Dec 6 '18 at 11:52
1
$\begingroup$

The dplyr package should be useful for this.

library(dplyr)

World %>%
  group_by(name) %>%
  mutate(num_distinct_languages = n_distinct(language)) %>%
  filter(num_distinct_lanagues > 4)
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.