0

I have the following table that I want to plot in R

     A    B    C  NA
0   500  200  200 0

This table is generated from a variable. I had previously removed the NAs using data<-data[!(data$pid3==""),]

If I use the plot() function with this variable, the NA and "" shows up in the plot. How do I get rid of this in the plot?

Thank you!

Jennifer
  • 285
  • 1
  • 3
  • 14
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Dec 03 '19 at 16:38
  • Is ``NA``/Are ``NAs`` here only a colname with 0 as value? – Gainz Dec 03 '19 at 16:42

2 Answers2

2

There is a difference between "" and NA in R:

> is.na("")
[1] FALSE
> is.na(NA)
[1] TRUE

If you want to remove NAs, you should use something like this:

data <- data[!is.na(data$pid3),]

(It may be a good idea to remove empty strings as well, so you can run the command above in addition to your previous filtering step.)

mrhd
  • 1,036
  • 7
  • 16
1

I figured it out. I was able to achieve this using the ggplot2 package.

I generate the table using

table <- data %>% group_by(pid3) %>% summarise(n = n())

Then I plot using ggplot2

ggplot(table, aes(x = pid3, y = n)) +
  geom_bar(stat="identity", position=position_dodge()) + theme_classic()
r2evans
  • 141,215
  • 6
  • 77
  • 149
Jennifer
  • 285
  • 1
  • 3
  • 14
  • 1
    What is `%<%`? That looks similar to `magrittr`'s `%>%` operator, but is not the same. (If it's a typo, it's generally best -- in both questions and answers -- to use code from your console or source file after testing/using it, as otherwise it is possibly to give code that is syntactically invalid.) – r2evans Dec 03 '19 at 17:06