I found the exact problem posted by Balázs Szappanos on July 12, 2017 07:40, but I cannot find a response. The link to his post is https://support.rstudio.com/hc/en-us/community/posts/115007064927-Bug-report-unknown-column-warning-when-using-tibbles
His summary: Here is an annoying bug when tibbles are used in a script in RStudio. RStudio gives a lot of "Warning: Unknown or uninitialised column..."; warnings when I use tibbles and I save, modify or do anything else with my code. Not gonna lie, it is quite annoying.
Here is my code to illustrate the problem:
# Create a data frame first ####
# You won't get errors
options(warn = 1) # makes the warnings appear immediately
c1 <- 1:10
c2 <- letters[1:10]
df1 <- as.data.frame(cbind(c1,c2))
# Add columns but not in a for loop
df1$c3 <- 5:14
df1$c4 <- letters[5:14]
# Add column in a for loop but do not allocate memory
for (i in 1:nrow(df1)) {
df1$c5[i] <- LETTERS[i]
}
# Result, no error
# Add column in a for loop but allocate memory
df1$c6 <- NA
for (i in 1:nrow(df1)) {
df1$c6[i] <- LETTERS[i+3]
}
# Result, no error
# Create the same but make it a tibble
# You will get the error
library(tibble)
options(warn = 1) # makes the warnings appear immediately
tb1 <- as.tibble(cbind(c1,c2))
tb1$c3 <- 5:14
tb1$c4 <- letters[5:14]
# Add column in a for loop but do not allocate memory
for (i in 1:nrow(tb1)) {
tb1$c5[i] <- LETTERS[i]
}
# Result - Warning: Unknown or uninitialised column: 'c5'.
# Add column in a for loop but allocate memory
tb1$c6 <- NA
for (i in 1:nrow(tb1)) {
tb1$c6[i] <- LETTERS[i+3]
}
# Result no error,
# however in more complicated code,
# I often get the same warning as when I added c5 to the tibble
# even if I have allocated memory space as with c6
# This bug is not always around. Yesterday Monday, August 6, 2018
# I ran code all day without the anoying warnings.
# Today, Tuesday, August 7, 2018, the very same code
# gives all kinds of anoying warnings. I will sometimes get 4 warnings
# on simple functions like dir(pattern = ".csv")
# The problem does not appear to be predictable, more anoying!!!