Problem with Summarise_at & "Error: unexpected symbol in:"


#1

Hi everyone,
I am using R to analyse the data published by PISA (http://www.oecd.org/pisa/).

I used summarise_at to to see the results of Math score:

pisa <- read_csv('PISA1.CSV')
df <- pisa %>% group_by(CNT) %>% summarise_at(vars(MATH), funs(mean, max, min, sd, n()) %>% arrange(desc(mean) 
df

However, I got a message:

Error: unexpected symbol in:
"df <- pisa %>% group_by(CNT) %>% summarise_at(vars(MATH), funs(mean, max, min, sd, n()) %>% arrange(desc(mean)
df"

I'm sure it's a simple answer but I've spent too much time trying to figure it out and would appreciate some help very much!


#2

I think the issue is the lack of a closing bracket after arrange?

pisa <- read_csv('PISA1.CSV')
df <- pisa %>% 
      group_by(CNT) %>% 
      summarise_at(vars(MATH), funs(mean, max, min, sd, n()) %>% 
      arrange(desc(mean))

#3

Thank rywhale,

I try again, but it still doesn't work :disappointed_relieved:


#4

Can you provide a link to the specific CSV you're using or post a small subset of the data here? The PISA website has a whole bunch of data so I'm not sure which set you're using.


#5

Couple possibilities (it's hard to tell exactly without a reproducible example) illustrated using a built-in dataset.

Yes for closing the parens to arrange, but also you can't pass n to summarise_at() when you've selected a numeric variable.

You also don't need the parens following the functions in funs().

library(tidyverse)

iris %>%
  group_by(Species) %>%
  summarise_at(vars(Petal.Length), funs(mean, max, min, sd)) %>%
  arrange(desc(mean))
#> # A tibble: 3 x 5
#>   Species     mean   max   min    sd
#>   <fct>      <dbl> <dbl> <dbl> <dbl>
#> 1 virginica   5.55   6.9   4.5 0.552
#> 2 versicolor  4.26   5.1   3   0.470
#> 3 setosa      1.46   1.9   1   0.174

iris %>%
  group_by(Species) %>%
  summarise(n = n())
#> # A tibble: 3 x 2
#>   Species        n
#>   <fct>      <int>
#> 1 setosa        50
#> 2 versicolor    50
#> 3 virginica     50

Created on 2018-09-15 by the reprex package (v0.2.1)