{"action":"created","issue":{"url":"https://api.github.com/repos/STAT545-UBC/Discussion/issues/175","labels_url":"https://api.github.com/repos/STAT545-UBC/Discussion/issues/175/labels{/name}","comments_url":"https://api.github.com/repos/STAT545-UBC/Discussion/issues/175/comments","events_url":"https://api.github.com/repos/STAT545-UBC/Discussion/issues/175/events","html_url":"https://github.com/STAT545-UBC/Discussion/issues/175","id":112098482,"number":175,"title":"Assigning a default for an argument","user":{"login":"ahippman","id":11307637,"avatar_url":"https://avatars.githubusercontent.com/u/11307637?v=3","gravatar_id":"","url":"https://api.github.com/users/ahippman","html_url":"https://github.com/ahippman","followers_url":"https://api.github.com/users/ahippman/followers","following_url":"https://api.github.com/users/ahippman/following{/other_user}","gists_url":"https://api.github.com/users/ahippman/gists{/gist_id}","starred_url":"https://api.github.com/users/ahippman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ahippman/subscriptions","organizations_url":"https://api.github.com/users/ahippman/orgs","repos_url":"https://api.github.com/users/ahippman/repos","events_url":"https://api.github.com/users/ahippman/events{/privacy}","received_events_url":"https://api.github.com/users/ahippman/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-10-19T08:37:11Z","updated_at":"2015-10-19T19:47:47Z","closed_at":null,"body":"My function may not be sophisticated, but if it works it will be so amazing!!! (OK, maybe just amazing for me in my own little world, but that's fine with me)\r\n\r\nI have several numeric vectors in my data.frame.\r\nMy function will summarize one verctor and spit out a nice object with its mean, max, min etc based on a `dplyr::group_by` command. \r\n\r\nThe basic function seems to be working. Now I want to be able to probe different variables. Hence, I want to make the variable an argument with the default value being the variable `Average`.\r\n\r\nWith `Average` it works. Both if I specify the variable or if I don;t use it. But if I try to use a different numeric variable it doesn;t work. :rotating_light:  Any idea why this is? Do I need to specify the argument `variable` somewhere else?\r\n\r\nHere is my function so far:\r\n\r\n```{r myfunction,eval=TRUE}\r\na \r\nSummary_per_Treatment_mod03 <- function(x, variable = x$Average, na.rm=TRUE) {\r\n  if(!is.data.frame(x)){\r\n    stop(\"Sorry, you will need to provide a data.frame as input!\\n\",\r\n         \"You have provided an object of class: \", class(x))\r\n  }\r\n  if(!is.numeric(variable)){\r\n    stop(\"Sorry, the variable you want to have summarized, needs to be numeric!\\n\", \r\n          \"The variable you want to query is of class: \", class(variable))\r\n  }\r\n  return (x %>% \r\n  group_by(Ratio.type) %>% \r\n  summarise(length (variable), min(variable), max(variable), mean(variable), median(variable)))\r\n}\r\n```\r\n\r\nNow I tried to make it more general so I can paste it here and one can re-run it:\r\n```{r foo-df, eval=TRUE}\r\ndf <- as.data.frame(df)\r\nis.data.frame(df)\r\ndf$a <- as.factor(df$a)\r\nfoo(df)\r\nfoo(df, b)\r\nfoo(df, c)\r\nfoo <- function(x, variable = x$b, na.rm=TRUE) {\r\n  if(!is.data.frame(x)){\r\n    stop(\"Sorry, you will need to provide a data.frame as input!\\n\",\r\n         \"You have provided an object of class: \", class(x))\r\n  }\r\n  if(!is.numeric(variable)){\r\n    stop(\"Sorry, the variable you want to have summarized, needs to be numeric!\\n\", \r\n         \"The variable you want to query is of class: \", class(variable))\r\n  }\r\n  return (x %>% \r\n            group_by(a) %>% \r\n            summarise(length (variable), min(variable), max(variable), mean(variable), median(variable)))\r\n}\r\n```\r\nand with this one it works!!! I dont know why!\r\nI checked my `variables` and `str()` it... I don't know what's wrong\r\n\r\nIf you want to can have a direct look at my function development in my [repo](https://github.com/STAT545-UBC/anna_hippmann/blob/master/HW04_a_MyFirstOwnFunction_Works.md#IssueStart) ... I even put a link under **Overview** called \"Issue\", which takes you directly down to the area I am talking about within subsection of 4c) Breaking the Function.\r\n\r\nAny help appreciated. :)\r\n\r\nThanks\r\nAnna"},"comment":{"url":"https://api.github.com/repos/STAT545-UBC/Discussion/issues/comments/149326317","html_url":"https://github.com/STAT545-UBC/Discussion/issues/175#issuecomment-149326317","issue_url":"https://api.github.com/repos/STAT545-UBC/Discussion/issues/175","id":149326317,"user":{"login":"ahippman","id":11307637,"avatar_url":"https://avatars.githubusercontent.com/u/11307637?v=3","gravatar_id":"","url":"https://api.github.com/users/ahippman","html_url":"https://github.com/ahippman","followers_url":"https://api.github.com/users/ahippman/followers","following_url":"https://api.github.com/users/ahippman/following{/other_user}","gists_url":"https://api.github.com/users/ahippman/gists{/gist_id}","starred_url":"https://api.github.com/users/ahippman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ahippman/subscriptions","organizations_url":"https://api.github.com/users/ahippman/orgs","repos_url":"https://api.github.com/users/ahippman/repos","events_url":"https://api.github.com/users/ahippman/events{/privacy}","received_events_url":"https://api.github.com/users/ahippman/received_events","type":"User","site_admin":false},"created_at":"2015-10-19T19:47:47Z","updated_at":"2015-10-19T19:47:47Z","body":"That's the funny thing: I do not get an error message but the table I get has NAs and no values in it.\r\nHere is the function when it works on the assigned variable `Average` (and it also works when I don't specify the `argument` \"variable\", but call the function with default value, i.e. \r\nSummary_per_Treatment_mod04 (lowCu):\r\n``` {r good output, eval=TRUE}\r\n Summary_per_Treatment_mod04 (lowCu,  variable=\"Average\") #works!\r\nSource: local data frame [1 x 6]\r\n\r\n  Ratio.type length      min    max    mean median\r\n      (fctr)  (int)    (dbl)  (dbl)   (dbl)  (dbl)\r\n1      lowCu   1161 1.000253 8.2847 1.35132 1.2444\r\n```\r\nSO, now I want to look at different numerical variable `RatioA`:\r\n``` {r badoutput, eval=TRUE}\r\n> Summary_per_Treatment_mod04 (lowCu, variable=\"RatioA\")\r\nSource: local data frame [1 x 6]\r\n\r\n  Ratio.type length   min   max  mean median\r\n      (fctr)  (int) (dbl) (dbl) (dbl)  (lgl)\r\n1      lowCu   1161    NA    NA    NA     NA\r\n```\r\nNo error message, just unuseful NAs"}}