I have a dataframe with this structure :
Note.Reco Reason.Reco Suggestion.Reco Contact
9 absent tomorrow yes
8 tomorrow yes
8 present today no
5 yesterday no
I would like to delete from this dataframe all the rows which have an empty value.
The expected result :
Note.Reco Reason.Reco Suggestion.Reco Contact
9 absent tomorrow yes
8 present today no
I try with this r instruction :
IRC_DF[!(is.na(IRC_DF$Reason.Reco) | IRC_DF$Reason.Reco==" "), ]
But I get the same input dataframe
Any idea please?
thank you
These blanks are actually inserted by using space key on computers. Therefore, if a data frame has any column with blank values then those rows can be removed by using subsetting with single square brackets.
Often you may want to filter rows in a data frame in R that contain a certain string. Fortunately this is easy to do using the filter() function from the dplyr package and the grepl() function in Base R.
We need to change the syntax to
IRC_DF[!(!is.na(IRC_DF$Reason.Reco) & IRC_DF$Reason.Reco==""), ]
# Note.Reco Reason.Reco Suggestion.Reco Contact
#1 9 absent tomorrow yes
#3 8 present today no
If multiple columns have NA or blanks (""
), then
IRC_DF[Reduce(`&`, lapply(IRC_DF, function(x) !(is.na(x)|x==""))),]
IRC_DF <- structure(list(Note.Reco = c(9L, 8L, 8L, 5L), Reason.Reco = c("absent",
"", "present", ""), Suggestion.Reco = c("tomorrow", "tomorrow",
"today", "yesterday"), Contact = c("yes", "yes", "no", "no")), .Names = c("Note.Reco",
"Reason.Reco", "Suggestion.Reco", "Contact"), class = "data.frame", row.names = c(NA,
-4L))
Or use dplyr's filter
function.
filter(IRC_DF, !is.na(Reason.Reco) | Reason.Reco != "")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With