Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204742

R - How to remove NAs when using ASSIGN with a FOR LOOP?

$
0
0

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.


Viewing all articles
Browse latest Browse all 204742

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>