0

I have list of name of data.frames with same columns, which I would like to remove. And I intend to iterate them so as not to manually remove those all, as follows:

AllAcctDataNames <- c("MyAcctContinuous1","MyAcctContinuous2","MyAcctDiscrete1",
                      "MyAcctDiscrete2","MyAcctDates","MyAcctOthers")
#Cleansing and Removing Unnecessary Variables
for (i in AllAcctDataNames) {
  get(eval(i))$person_id <- NULL
  get(eval(i))$customer_id <- NULL
  get(eval(i))$account_id <- NULL
  get(eval(i))$person_bkey <- NULL
  print(paste0(i," done."))
}

However when I ran them, this error occurs.

Error in get(eval(i))$person_id <- NULL : could not find function "get<-"

I have also tried to use assign as follows:

assign(paste0(i,"$person_id"),NULL)

But it assigns to the variable name MyAcctContinuous1$person_id instead.

Really appreciate your help on this. Thank you so much!

Regards,

Marlon

mcmalicsi
  • 11
  • 1
  • You can't use `$` with string column names, you need to use `[[` instead, as in `data[[column_name_var]]`, or `[` for more than one column. However, you also can't assign back to a `get(eval())` variable, which is the error R is complaining about here. Really, you should be [using a list of data frames](https://stackoverflow.com/a/24376207/903061), and then all of this will be easier. – Gregor Thomas Dec 14 '21 at 04:00
  • `AcctData <- mget(AllAcctDataNames)` will get you a list of all those data frames. Stop using the versions that aren't in the list. Then a simple for loop `cols_to_remove <- c("person_id", "customer_id", "account_id", "person_bkey")` and `for(i in seq_along(AcctData)){AcctData[[i]][cols_to_remove] <- list(NULL)}` – Gregor Thomas Dec 14 '21 at 04:03

0 Answers0