0

I tried to run this code (with no success):

for (i in chanel_code) {
  assign(paste("prospect_",i,sep="", collapse = NULL,recycle0 = FALSE),(aggregate(na_adss_score ~ month_year + na_appl_status, paste("new_account_",i, sep="",collapse = NULL, recycle0 = FALSE),mean) %>% 
                                                                          mutate(aggregate(na_pcn_no ~ month_year + na_appl_status,  paste("new_account_",i, sep="",collapse = NULL, recycle0 = FALSE), length))))
}

Error in eval(predvars, data, env) : argument 'envir' incorrect de type 'character'

would you mind please helping me.

thanks in advance

Bruno
  • 4,109
  • 1
  • 9
  • 27
MoLo
  • 1
  • 1
  • with chanel_code=c( "BA","CS","DM","DS","EN","IA","MG","PS","TM") – MoLo Oct 19 '21 at 13:43
  • I don't know the rest of your code or context, but ... `assign(.)` is often not a sign of good data flow. Since you're doing the same thing to multiple things and are working with frame-like objects, I suggest you look at https://stackoverflow.com/a/24376207/3358227, and become familiar with using `lapply`. – r2evans Oct 19 '21 at 13:46
  • Side note, though: the second argument to `aggregate.formula` is `data=`, but you are sending it a `paste(.)` string. Even though the column names may be evaluated directly (because this is in a `mutate` context), you must then assign all other arguments to `aggregate` *by name*. (I cannot provide a recommendation because your code is confusing, hard-to-read, and the question is not [reproducible](https://stackoverflow.com/q/5963269).) – r2evans Oct 19 '21 at 13:50
  • the code run perfectly for single argument : prospect_BA=aggregate(na_adss_score ~ month_year + na_appl_status, new_account_BA,mean) %>% mutate(aggregate(na_pcn_no ~ month_year + na_appl_status, new_account_BA, length)) as I have to do this for 9 tables :c( "BA","CS","DM","DS","EN","IA","MG","PS","TM") I wish I could use the FOR – MoLo Oct 19 '21 at 14:01
  • Hi @MoLo, were you able to figure this out? – Skaqqs Dec 15 '21 at 19:50

1 Answers1

0

You don't need assign() or all the arguments for paste(). Instead, subset your data with i and use paste0().

Assuming your dataset is called new_account_BA and new_account_BA$chanel_codes contains chanel_codes:

chanel_code=c("BA","CS","DM","DS","EN","IA","MG","PS","TM")
results <- list()
for (i in chanel_code) {
  x <- new_account_BA[new_account_BA$chanel_code == paste0("prospect_",i),]
  results[[chanel_code]] <- cbind(
    aggregate(na_adss_score ~ month_year + na_appl_status, x = x, mean),
    aggregate(na_pcn_no ~ month_year + na_appl_status, x = x, length))
}
Skaqqs
  • 4,010
  • 1
  • 7
  • 21