R – argument is of length zero in if statement

“argument is of length zero” is a very specific problem that comes from one of my least-liked elements of R. Let me demonstrate the problem: As you can see, comparisons to a NULL not only don’t produce a boolean value, they don’t produce a value at all – and control flows tend to expect that … Read more

Categories r

Plot multiple boxplot in one graph

You should get your data in a specific format by melting your data (see below for how melted data looks like) before you plot. Otherwise, what you have done seems to be okay. Edit: I realise that you might need to facet. Here’s an implementation of that as well: Edit 2: How to add x-labels, y-labels, title, change legend heading, add … Read more

Categories r

How to plot 3D scatter diagram using ggplot?

Since you tagged your question with plotly and said that you’ve tried to use it with plotly, I think it would be helpful to give you a working code solution in plotly: Creating some data to plot with: Graphing your 3d scatterplot using plotly’s scatter3d type: Renders the following:  ggplot as others have note, by itself does not support 3d graphics … Read more

Categories r

Replace NA with 0 in a data frame column [duplicate]

Since nobody so far felt fit to point out why what you’re trying doesn’t work: NA == NA doesn’t return TRUE, it returns NA (since comparing to undefined values should yield an undefined result). You’re trying to call apply on an atomic vector. You can’t use apply to loop over the elements in a column. … Read more

Categories r