I am Trying to make a list out of a certain Column of my DataFrame.
But it should depend on another column, if it schould be added to the list.
So for example if the dataframe is:
names <- c('Word1','Word2','Word3')
quant <- c(5, NA, 10)
my.data <- data.frame(names, quant)
Now I would only like to List the words with a quantity and leave out the ones with "NA". Is there a smart and fast way to do this? The final List in this case should be (Word1, Word3)
Thank you very much for your help. I am a beginner with R and my only soution would be a loop going through the dataFrame and check for every value.
CodePudding user response:
We could use complete.cases
. na.omit
removes all NA
's. With complete.cases
we are able to partially select the columns. See my.data[complete.cases(my.data[1,1]),]
vs. my.data[complete.cases(my.data[1:2,2]),]
:
[complete.cases(my.data),]
names quant
1 Word1 5
3 Word3 10
and is faster:
library(microbenchmark)
microbenchmark(
na.omit(my.data)$names,
my.data[complete.cases(my.data),]
)
Unit: microseconds
expr min lq mean median uq max neval cld
na.omit(my.data)$names 46 46.95 50.457 47.60 49.4 90.9 100 b
my.data[complete.cases(my.data), ] 22 22.80 25.522 23.65 24.3 133.5 100 a
CodePudding user response:
Is this what you are looking for?
na.omit(my.data)
names quant
1 Word1 5
3 Word3 10