Actuarial Outpost
 
Go Back   Actuarial Outpost > Actuarial Discussion Forum > Software & Technology
FlashChat Actuarial Discussion Preliminary Exams CAS/SOA Exams Cyberchat Around the World Suggestions


Fill in a brief DW Simpson Registration Form
to be contacted when new jobs meet your criteria.


Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 05-31-2018, 12:15 PM
we7dude we7dude is offline
Member
 
Join Date: Jan 2009
College: Temple Alumni
Posts: 322
Default R newbie - error in function writing

Hi R user,

A newbie question in R function writing. I am trying to re-produce the sas proc freq macro in R and get the following error " Error in grouped_df_impl(data, unname(vars), drop) :
Column `a` is unknown"

thanks for the help in advance.


library(dplyr)

toread <- "id sex age inc r1 r2 r3
1 F 35 17 7 2 2
17 M 50 14 5 5 3
33 F 45 6 7 2 7
49 M 24 14 7 5 7
65 F 52 9 4 7 7
81 M 44 11 7 7 7
2 F 34 17 6 5 3
18 M 40 14 7 5 2
34 F 47 6 6 5 6
50 M 35 17 5 7 5"

survey <- read.table(textConnection(toread), header = TRUE)
closeAllConnections()
survey

#row count by sex
row_count_by_sex <- survey %>%
group_by(sex) %>%
summarise(row_count=n(),avg_age=mean(age))

#print result
row_count_by_sex

#macro try ERROR
myavg<-function(a,b){
by_macro <- survey %>%
group_by(a) %>%
summarise(row_count=n(),avg_age=mean(b))
}

myavg(sex,age)
Reply With Quote
  #2  
Old 05-31-2018, 12:58 PM
examsarehard examsarehard is offline
Member
CAS
 
Join Date: May 2011
Posts: 551
Default

If you have a recent version of dplyr, you can use !! to refer to function variables. This should work:

Code:
myavg<-function(a,b){
  a<-enquo(a)
  b<-enquo(b)
  survey %>% 
    group_by(!!a) %>% 
    summarise(row_count=n(),avg_age=mean(!!b))
}

myavg(sex,age)
See:
https://cran.r-project.org/web/packa...ogramming.html
Reply With Quote
  #3  
Old 05-31-2018, 01:56 PM
we7dude we7dude is offline
Member
 
Join Date: Jan 2009
College: Temple Alumni
Posts: 322
Default

@examsarehard - works like a charm. Thanks. I like your username and luckily exams are things in the past for me. Could you explain a little bit about a<-enquo(a) and !!a are doing? Appreciate. From first glance, !!a is like &a. in SAS.
Reply With Quote
  #4  
Old 05-31-2018, 02:54 PM
examsarehard examsarehard is offline
Member
CAS
 
Join Date: May 2011
Posts: 551
Default

Yes, the !!a is exactly the same concept as the &a in SAS. Essentially dplyr uses custom evaluation rules to make it easier to use, and the enquo() and !! gets around those custom rules. enquo() tells R to hold off on evaluating what "a" is until it gets to !!a where the substitution then happens.

If you want more details, they're in the provided link.
Reply With Quote
  #5  
Old 05-31-2018, 02:55 PM
AMedActuary AMedActuary is offline
Member
SOA
 
Join Date: May 2007
College: UCLA Alumni
Posts: 389
Default

Also check out summarise_all and summarise_if.

See the below:

survey %>%
group_by(sex) %>%
mutate(row_count=n()) %>%
summarise_all(mean)

A tibble: 2 x 8
sex id age inc r1 r2 r3 row_count
<fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 F 27.0 42.6 11.0 6.00 4.20 5.00 5.00
2 M 43.0 38.6 14.0 6.20 5.80 4.80 5.00

Last edited by AMedActuary; 05-31-2018 at 03:25 PM..
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


All times are GMT -4. The time now is 08:20 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
*PLEASE NOTE: Posts are not checked for accuracy, and do not
represent the views of the Actuarial Outpost or its sponsors.
Page generated in 0.30821 seconds with 11 queries