{"action":"closed","issue":{"url":"https://api.github.com/repos/STAT545-UBC/Discussion/issues/157","labels_url":"https://api.github.com/repos/STAT545-UBC/Discussion/issues/157/labels{/name}","comments_url":"https://api.github.com/repos/STAT545-UBC/Discussion/issues/157/comments","events_url":"https://api.github.com/repos/STAT545-UBC/Discussion/issues/157/events","html_url":"https://github.com/STAT545-UBC/Discussion/issues/157","id":111550782,"number":157,"title":"Function breaks when `group_by()` is done","user":{"login":"AdrianLJones","id":14188583,"avatar_url":"https://avatars.githubusercontent.com/u/14188583?v=3","gravatar_id":"","url":"https://api.github.com/users/AdrianLJones","html_url":"https://github.com/AdrianLJones","followers_url":"https://api.github.com/users/AdrianLJones/followers","following_url":"https://api.github.com/users/AdrianLJones/following{/other_user}","gists_url":"https://api.github.com/users/AdrianLJones/gists{/gist_id}","starred_url":"https://api.github.com/users/AdrianLJones/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AdrianLJones/subscriptions","organizations_url":"https://api.github.com/users/AdrianLJones/orgs","repos_url":"https://api.github.com/users/AdrianLJones/repos","events_url":"https://api.github.com/users/AdrianLJones/events{/privacy}","received_events_url":"https://api.github.com/users/AdrianLJones/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-10-15T05:58:17Z","updated_at":"2015-10-15T22:23:18Z","closed_at":"2015-10-15T22:23:18Z","body":"I have a function very similar to the regression we did in class except that it does a polynomial regression and I can choose which columns of the dataset to regress.\r\n\r\n``` r\r\npoly_coeffs <- function (data, x, y, order, offset, raw = TRUE) {\r\n   #This funciton fits input vector x at order = order to input vecotor y using R's standard lm() function.\r\n    #It then returns the resulting coefficients using the broom::tidy\r\n\r\n   fit <- lm(data[,y] ~ poly(data[,x] - offset, order, raw = raw))\r\n   poly_coeffs <- tidy(fit)\r\n}\r\n```\r\n\r\nIt works fine on a single country\r\n\r\n``` r\r\nx <- \"year\"\r\ny <- \"lifeExp\"\r\norder <- 2\r\noffset <- 1952\r\nSA_test <- gapminder %>% filter(country == \"South Africa\") %>% do(poly_coeffs(., x, y, order, offset))\r\n```\r\n\r\nBut gets upset when I try to `do` it to several countries\r\n\r\n``` r\r\ngap_sub <- gapminder %>% filter(country == \"South Africa\" | country == \"France\" | country == \"Rwanda\"| country == \"Canada\") %>% group_by(country) %>% do(poly_coeffs(., x, y, order, offset))\r\n## Error in FUN(X, Y, ...) : non-numeric argument to binary operator \r\n```\r\n\r\nInterestingly the single country also fails when I make gapminder a `tbl_df`  I think there might be something slightly different about the way dataframe column names are handled that prevents me using the `data.frame[ , column.name]` system of grabbing the column.  Is that right?  Any suggestions on other ways to define which column to use of a data frame inside a function that might work around this problem?\r\n\r\nThanks\r\n\r\nAdrian\r\n\r\nps [here](https://github.com/STAT545-UBC/adrian_jones/blob/master/Homework/Homework04/Homework04_function.Rmd) is my current .rmd file for HW04 in case my description is insufficient,"}}