0

I have several variables (id.type and id.subtype in this example) I would like to check for distinct values in a tibble all.snags using the dplyr package. I would like them sorted and all values printed out in the console (a tibble typically prints only the first 10). The output would be equivalent to the following code:

distinct(all.snags,id.type)  %>% arrange(id.type) %>% print(n = Inf)
distinct(all.snags,id.subtype) %>% arrange(id.subtype) %>% print(n = Inf)

I think this is better done by looping over the values in a vector, but I can't get it to work.

distinct.vars  <- c("id.type","id.subtype")
for (i in distinct.vars) {
    distinct(all.snags,distinct.vars[i]) %>% 
    arrange(distinct.vars[i]) %>% 
    print(n = Inf)
}
GForce
  • 153
  • 2
  • 9

1 Answers1

1

I think this function is what you want:

library(dplyr)

df = iris

print_distinct = function(df, columns) {
  for (c in columns) {
    print(df %>% distinct_(c) %>% arrange_(c))
  }
}

print_distinct(df, c("Sepal.Length", "Sepal.Width"))
LetEpsilonBeLessThanZero
  • 2,395
  • 2
  • 12
  • 22