1

So I have a data frame: df and I plot it but there are too many Na's and it is not nice.

So I try to remove Na's with 1):

 df <- na.omit(df)

But my data are getting messed up. 2):

 df <- df[!is.na(df$column_name),]

This work for a specific column name but in the plot I have multiple column names with Na's and when I try to use the same command but for other column name it changes my data complitely. So can anyone help me? Is there a way to !is.na(Multiple column names) Or Ignore NA's In a ggplot?

I am using this:

df<-Ass1MatrixNoNa %>% gather(test, value, 3:5)
ggplot(df,aes(x=test,fill=value)) +
  geom_bar(position=position_dodge(preserve="single"))

enter image description here

And I get a plot but with NA's
Then I try to remove the NA's:

Ass1MatrixNoNa <- Ass1Matrix[!is.na(Ass1Matrix$Ass_1_hearingA),]

Removes the Ass_1_hearingA Na's But I want also hearingB but ovverides the first one and the NA's are removed only in the second one:

Ass1MatrixNoNa <- Ass1Matrix[!is.na(Ass1Matrix$Ass_1_hearingB]
Anders Ellern Bilgrau
  • 9,928
  • 1
  • 30
  • 37
Alex Rika
  • 141
  • 2
  • 14
  • Please post a little bit of sample data, show the code you are trying, and describe your desired outcome better. `ggplot` ignores `NA`s (does not plot them) by default, so it is not clear how your goal is different from the result you see. You will probably be helped if you read [some of the "How to ask" section in the Help center](https://stackoverflow.com/help/mcve). Also see [How to make a reproducible example in R/](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for great R-specific advice on asking questions. – Gregor Thomas Mar 12 '19 at 19:49
  • I posted a sample code that I am trying to use and what I get – Alex Rika Mar 12 '19 at 19:58
  • From the error that you mention below, you are using a data.table object, which can act differently from a data.frame object at times. It is important to include all of the information about your objects in your question. Please follow Gregor's advice above and produce a reproducible example. – lmo Mar 13 '19 at 00:36
  • Thank you for the code. We still cannot help without data. Please see the "How to make a reproducible example in R" link from my comment above, it will tell you how to add data to your question. We cannot help more until you share some data. – Gregor Thomas Mar 13 '19 at 20:03

1 Answers1

1

I'm unsure if this is what you want. But if you are trying to deal with warnings from geom_bar regarding NAs, you may notice from the documentation (help("geom_bar")) that that the function has the argument na.rm. So the function can remove the NAs for you. Try

ggplot(df,aes(x=test,fill=value)) +
  geom_bar(position=position_dodge(preserve="single"), na.rm = TRUE)

Does that do what you want?

So you may not necessarily need to remove the NAs in df.

EDIT: Otherwise, the complete.cases function might help you:

df <- data.frame(x = c(1, NA, 3, 4), value = c(1, 2, 3, 4), fill= c(1, 2, NA, 4))
print(df)
#   x value fill
#1  1     1    1
#2 NA     2    2
#3  3     3   NA
#4  4     4    4

ccol <- c("value", "fill") # Cols to keep 'complete'
df_complete <- df[complete.cases(df[, ccol]), ]
print(df_complete)
#   x value fill
#1  1     1    1
#2 NA     2    2
#4  4     4    4

Running complete.cases(x) returns a logical vector with TRUE where no NAs appear in the rows of x.

Alternatively, using the tidyverse/dplyr, something like the following

df_complete2 <- df %>% filter(!is.na(fill) & !is.na(value))

should do it too.

Anders Ellern Bilgrau
  • 9,928
  • 1
  • 30
  • 37
  • It is not clear how do you plot an '`NA`'? Can you try to save the plot you get and put in the question (just drag-and-drop)? – Anders Ellern Bilgrau Mar 12 '19 at 20:08
  • Complete cases wont work because it will remove the other columns value and I dont want that. I want to show it... in the plot – Alex Rika Mar 12 '19 at 20:12
  • Then just subset the `df` inside `complete.cases` to only the columns you want to have complete cases for? – Anders Ellern Bilgrau Mar 12 '19 at 20:20
  • how? do I subset df` inside complete.cases? – Alex Rika Mar 12 '19 at 20:22
  • I get this error: Error: j (the 2nd argument inside [...]) is a single symbol but column name 'ccol' is not found. Perhaps you intended DT[, ..ccol]. This difference to data.frame is deliberate and explained in FAQ 1.1. – Alex Rika Mar 12 '19 at 20:31
  • This is what I tried: `ccol <- c("Ass_1_hearingE", "Ass_1_hearingC","Ass_1_hearingA") # Cols to keep 'complete' Ass1MatrixNoNa <- Ass1MatrixNa[complete.cases(Ass1MatrixNa[, ccol]), ] ` – Alex Rika Mar 12 '19 at 20:32
  • Well, unless you post some of your actual data, it is not easy to help you further. I just assumed your `df` is a standard `data.frame` or perhaps a `tibble`. But it appears it is not. – Anders Ellern Bilgrau Mar 12 '19 at 20:59
  • Can I contact you somehwere else? Because I cant start chat yet here...? – Alex Rika Mar 12 '19 at 21:06
  • Well the data is as you see in the plot. I have Variables HearingA,HearingC,HearingE with values NA,not fullfilled, partly fulfilled and fulfilled. And I just have many NA's and want to show in the plot all the other values except the NA's and just cant do that... – Alex Rika Mar 12 '19 at 21:11
  • 2
    Yes, I/we can sort of guess what data you have based on the code snippets and plot. But that tells us nothing about the precise structure of you data. I have showed you examples that work with common data structures. Please see the link on making a good minimal reproducible example given by Gregor in the other comments. – Anders Ellern Bilgrau Mar 12 '19 at 22:11