I have some code which imports a data frame (keywordsDF), and whilst using a FOR LOOP, it (by using the colnames), creates new variables depending on how many columns there are in it:
keywordsDF = read_excel("//Users//n//Desktop//Keywords.xlsx")
keywordList = colnames(keywordsDF)
for (i in seq_along(keywordList)) {
assign(keywordList[i], keywordsDF[keywordList[i]])
}
This all works fine. However, as the columns are of a different length, it imports NAs into the data.
I would normally remove those NAs for each column like:
consumption = keywordsDF$Consumption[!is.na(keywordsDF$Consumption)]
But I am not sure how to do it in the FOR LOOP (where I don't know the column names). I.e. where do I put the !is.na
? As nothing seems to work for me.