Trying ?max
, you’ll see that it actually has a na.rm =
argument, set by default to FALSE
. (That’s the common default for many other R functions, including sum()
, mean()
, etc.)
Setting na.rm=TRUE
does just what you’re asking for:
d <- c(1, 100, NA, 10) max(d, na.rm=TRUE)
If you do want to remove all of the NA
s, use this idiom instead:
d <- d[!is.na(d)]
A final note: Other functions (e.g. table()
, lm()
, and sort()
) have NA
-related arguments that use different names (and offer different options). So if NA
‘s cause you problems in a function call, it’s worth checking for a built-in solution among the function’s arguments. I’ve found there’s usually one already there.
Related Posts:
- Replace a value in a data frame based on a conditional (`if`) statement
- How to read in .HTM into R as a data frame?
- Count number of occurences for each unique value
- How to change line width in ggplot?
- Error in library(ggplot2) : There is no package called ‘ggplot2’
- Transpose a data frame
- How to drop columns by name in a data frame
- How to reshape data from long to wide format
- Multiple If Statements in R
- ggplot2, change title size
- Combine two or more columns in a dataframe into a new column with a new name
- Insert picture/table in R Markdown
- Test if a vector contains a given element
- R “Error: unexpected ‘}’ in “}”
- Insert a blank column in dataframe
- “length of ‘dimnames’ [2] not equal to array extent” on one of two very similar sets
- Mean per group in a data.frame
- How to install RHadoop packages (Rmr, Rhdfs, Rhbase)?
- Error in dev.off() : cannot shut down device 1 (the null device)
- How to create an empty matrix in R?
- standard deviation on dataframe does not work
- Warning message: In `…` : invalid factor level, NA generated
- What is the difference between = and ==?
- Subscripts in plots in R
- promise already under evaluation: recursive default argument reference or earlier problems?
- What does the error “arguments imply differing number of rows: x, y” mean?
- Aggregate multiple columns at once
- Error in model.frame.default: variable lengths differ
- Non-numeric argument to mathematical function
- Position-dodge warning with ggplot boxplot?
- Plot two graphs in same plot in R
- data.table vs dplyr: can one do something well the other can’t or does poorly?
- R – Concatenate two dataframes?
- KNN in R: ‘train and class have different lengths’?
- Error in rep(1, n.ahead) : invalid ‘times’ argument in R
- Producing subscripts in R markdown
- ggplot geom_text font size control
- lme4::lmer reports “fixed-effect model matrix is rank deficient”, do I need a fix and how to?
- R multiple conditions in if statement
- Remove quotes from a character vector in R
- Convert data.frame columns from factors to characters
- Is it possible to have a multi-line comments in R?
- R – error “variable lengths differ”
- Simple manual RMarkdown tables that look good in HTML, PDF and DOCX
- how to increase the limit for max.print in R
- how to increase the limit for max.print in R
- backward elimination in R
- Error in eval(expr, envir, enclos) : object not found
- Replace NA with 0 in a data frame column [duplicate]
- How to plot 3D scatter diagram using ggplot?
- Error while creating heatmaps – NA/NaN/Inf in foreign function call (arg 11)
- Write lines of text to a file in R
- How to compute summation in r
- Plot multiple boxplot in one graph
- R will plot but won’t draw abline
- scale_fill_discrete and scale_fill_manual – legend options confusion
- R – argument is of length zero in if statement
- How to solve ‘protection stack overflow’ issue in R Studio
- Conditional mean statement
- Merging data – Error in fix.by(by.x, x)
- Warning in GLM analysis
- Error in 2:n : NA/NaN argument
- Error in generating boxplot two variable data frame: adding class “factor” to an invalid object
- Error : Unable to start png() device
- What does ‘length of ‘dimnames’ [1] not equal to array extent’ mean?
- “no function to return from, jumping to top level”
- r random forest error – type of predictors in new data do not match
- env=baseenv() in R
- How to set axis range R
- Removing punctuations from text using R
- How to find out which package version is loaded in R?
- How to melt and cast dataframes using dplyr?
- What does Continuous x aesthetic — did you forget aes(group=…) mean?
- How to unload a package without restarting R
- What does the error “object not interpretable as a factor” mean?
- How to declare a vector of zeros in R
- R Sweave: NO TeX installation detected
- Plotting legend outside plot in R
- Boolean operators && and ||
- Reset par to the default values at startup
- R avoiding “restarting interrupted promise evaluation” warning
- How to increase the font size of ggtitle in ggplot2
- do-while loop in R
- ERROR: [on_request_read] connection reset by peer in R shiny
- Error in as.vector(x, mode) : cannot coerce type ‘closure’ to vector of type ‘any’
- Suppress output of a function
- Error in G * t(hat) : non-conformable arrays
- How to counter the ‘non-numeric matrix extent’ error in R?
- What does is.na() applied to non-(list or vector) of type ‘NULL’ mean?
- error r: invalid subscript type “closure” in a simple regression
- Colour points in a plot differently depending on a vector of values
- increase legend font size ggplot2
- Mask output of `The following objects are masked from….:` after calling attach() function
- How To Create Vector of Vector In R
- Round down a numeric
- Construct a manual legend for a complicated plot
- Code to clear all plots in RStudio
- Installing R on Android
- Convert data.frame column to a vector?
- When trying to replace values, “missing values are not allowed in subscripted assignments of data frames”