There may be multiple reasons for the annoying warnings described above. On Nov. 2, 2018, I ran into the problem of getting the warning message that columns don't exist or that they are not initialized. The warning messages were caused by the functions names(df) and str(df) as I was trying to examine df.
After messing around with many suggestions above as well as others and without success, I restarted RStudio for the nth time and when back to the start of my code to see when the warnings might pop up. It is a long story and a long process of checking before I had an epiphany. I knew that the columns in the warnings were not created yet but in code below where I was working. After removing all of the code that created the warnings, the warnings went away. I pasted the code back in and the warnings came back. My problem seems to be related to RStudio.
Relevant dataframes in RStudio environment are all_matched_source and j2clm_clp_blh
To try to clarify what is going on, I decided to paste back into the code one line at a time. All the code pasted back in is below two lines of code:
names(all_matched_source)
str(all_matched_source)
First line pasted back in: j2clm_clp_blh$prin_amt <- NA, names(all_matched_source) no problem, and str(all_matched_source) no problem. Second code pasted back:
for (i in 1:nrow(j2clm_clp_blh)){
if (j2clm_clp_blh$mode__x[i] == "1"){
j2clm_clp_blh$prin_amt[i] <- 0
}
if (j2clm_clp_blh$mode__x[i] %in% c("2", "4", "5")){
if (j2clm_clp_blh$ln_bal[i] < j2clm_clp_blh$payment[i]){
j2clm_clp_blh$prin_amt[i] <- j2clm_clp_blh$ln_bal[i]
} else {
j2clm_clp_blh$prin_amt[i] <- j2clm_clp_blh$payment[i]
}
}
if (((j2clm_clp_blh$mode__x[i] == "6") | (!is.na(j2clm_clp_blh$l_cr_score_appl_cd[i])))
& ((!is.na(j2clm_clp_blh$l_cr_score_appl_cd[i]))
& ((j2clm_clp_blh$ln[i] != j2clm_clp_blh$l_cr_score_appl_cd[i])))
) {
j2clm_clp_blh$prin_amt[i] <- j2clm_clp_blh$ln_bal[i]
}
}
Now names(all_matched_source) lists all of the names and warnings:
Warning messages:
1: Unknown or uninitialised column: 'prin_amt'.
2: Unknown or uninitialised column: 'prin_amt'.
3: Unknown or uninitialised column: 'prin_amt'.
4: Unknown or uninitialised column: 'prin_amt'.
str(all_matched_source) does its job and gives the same warnings. Now for the real confusion, going back to names(all_matched_source) and executing NO WARNINGS!!! So I execute str(all_matched_source) NO WARNINGS!!! What the heck????
Now I put my cursor at the very end of the code just pasted and hit Enter to add a new line below the code. Now go back up to names(all_matched_source) and execute. I get the warnings back. Now execute str(all_matched_source) no warnings. Go to the bottom of the code and add another blank line go back up to str(all_matched_source) and execute and you guessed it, I get the warnings again. By this time I am ROFL (rolling on the floor laughing).
I hope this helps someone someday.