Skip to content

Instantly share code, notes, and snippets.

@apinstein
Created December 27, 2016 18:31
Show Gist options
  • Save apinstein/917346f203b68d06df7a19e4ed52a2f5 to your computer and use it in GitHub Desktop.
Save apinstein/917346f203b68d06df7a19e4ed52a2f5 to your computer and use it in GitHub Desktop.
R data.table function issue
is_established_customerF <- function(customer_segment, credits_purchased) {
print(typeof(customer_segment)) # prints only once?
established_customer_threshold <- 100000000 # infinitely large
customer_segment <- as.character(customer_segment)
if (customer_segment == "multiple_photographer_company") {
established_customer_threshold <- 3000
} else if (customer_segment == "in_house_photography_department") {
established_customer_threshold <- 1000
} else if (customer_segment == "full_time_single_re_photographer") {
established_customer_threshold <- 1000
} else if (customer_segment == "part_time_single_re_photographer") {
print("matched PT")
established_customer_threshold <- 350
} else if (customer_segment == "real_estate_agent") {
print("matched RE")
established_customer_threshold <- 150
}
# prints N times?
print(paste(customer_segment, credits_purchased, ">", established_customer_threshold, credits_purchased > established_customer_threshold))
return(credits_purchased > established_customer_threshold)
}
data[1:50][is_established_customerF(customer_segment, tx_cum_amount) == TRUE]
###
output
###
expect the `established_customer_threshold` to be different for these rows...
[20] "real_estate_agent 300 > 150 TRUE"
[21] "full_time_single_re_photographer NA > 150 NA"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment