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:
- Poker hand range chart visualization in R
- ggplot wrong color assignment
- How to rename a single column in a data.frame?
- Critical t values in R
- What does %>% function mean in R?
- t-stat for feature selection
- Reasons for using the set.seed function
- R on MacOS Error: vector memory exhausted (limit reached?)
- Remove legend ggplot 2.2
- How do I replace NA values with zeros in an R dataframe?
- How to change legend title in ggplot
- Changing column names of a data frame
- R: Using equation with natural logarithm in nls
- How to change legend title in ggplot
- Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)
- R on MacOS Error: vector memory exhausted (limit reached?)
- kmeans complains “NA/NaN/Inf in foreign function call (arg 1)”, when there are none?
- R issue “object not found”
- Error in Confusion Matrix : the data and reference factors must have the same number of levels
- Why do I get “number of items to replace is not a multiple of replacement length”
- SummarySE (Rmisc package) to produce a barplot with error bars (ggplot2)
- R ggplot2 scale_y_continuous : Combining breaks & limits
- “Error: Mapping should be created with `aes()` or `aes_()`.”
- Error in file(file, “rt”) : invalid ‘description’ argument in complete.cases program
- Longer object length is not a multiple of shorter object length?
- rmarkdown error “attempt to use zero-length variable name”
- cannot coerce type ‘closure’ to vector of type ‘character’
- Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : NA/NaN/Inf in ‘y’, tried every possible way
- Logistic regression – eval(family$initialize) : y values must be 0 <= y <= 1
- R – longer object length is not a multiple of shorter object length
- Opposite of %in%: exclude rows with values specified in a vector
- Arcsine transformation of percentage data
- R loop that adds following numbers to each element in list
- R: Using equation with natural logarithm in nls
- remove legend title in ggplot
- scale_color_manual() not working
- Can I calculate z-score with R?
- Clear R environment of all objetcs & packages
- Function to clear the console in R and RStudio
- “replace” function examples
- R: need finite ‘ylim’ values in function
- Convert a list to a data frame
- Manually colouring plots with `scale_fill_manual` in ggplot2 not working
- Why I get this error writing data to a file
- Correlation between multiple variables of a data frame
- Remove grid, background color, and top and right borders from ggplot2
- R: “Unary operator error” from multiline ggplot2 command
- What does the double percentage sign (%%) mean?
- not a Stata version 5-12 .dta file
- How to subset a Data frame column wise using column names?
- Error : length of ‘dimnames’ [2] not equal to array extent
- Mean of a column in a data frame, given the column’s name
- How can I plot data with confidence intervals?
- No non-missing arguments warning when using min or max in reshape2
- Unimplemented type list when trying to write.table
- How to convert a factor to integer\numeric without loss of information?
- Having trouble setting working directory
- Custom legend for multiple layer ggplot
- file.path function in R
- predict.lm() in a loop. warning: prediction from a rank-deficient fit may be misleading
- Convert categorical variables to numeric in R
- How to sum data.frame column values?
- Error: invalid subscript type ‘list’ in R
- “non-numeric argument to binary operator” error in R
- How to sum a variable by group
- readOGR() cannot open file
- What does “The following object is masked from ‘package:xxx'” mean?
- grep using a character vector with multiple patterns
- How to avoid “Error in stripchart.default(x1, …) : invalid plotting method” error?
- R error in unique.default(x) unique() applies only to vectors
- Unused arguments in R
- Combine two data frames by rows (rbind) when they have different sets of columns
- Boxplot dplyr: Error: non-numeric argument to binary operator
- How do I install an R package from source?
- Getting invalid model formula in ExtractVars when using rpart function in R
- Error: ggplot2 doesn’t know how to deal with data of class matrix?
- Aggregate multiple columns at once
- Error in match.fun(FUN)
- Error in file(filename, “r”, encoding = encoding) : cannot open the connection
- installing data.table results in “there is no package”
- What is integer overflow in R and how can it happen?
- R histogram range error: some ‘x’ not counted; maybe ‘breaks’ do not span range of ‘x
- Correct syntax for mutate_if
- Plotting data in R; error: default method not implemented for type ‘list’
- Error:attempt to apply non-function
- Error in contrib.url(repos, “source”) in R trying to use CRAN without setting a mirror Calls: install.packages -> contrib.url Execution halted
- Changing fonts in ggplot2
- Error in
: target of assignment expands to non-language object - How do I combine two data-frames based on two columns?
- How to eliminate “NA/NaN/Inf in foreign function call (arg 7)” running predict with randomForest
- Indicator function in R
- Batch convert columns to numeric type
- R4DS error comparison (1) is possible only for atomic and list types
- How can I make a list of lists in R?
- Error in x[[i]] : recursive indexing failed at level 2
- How to avoid “operations are possible only for numeric, logical or complex types” when computing top 3 elements in each group
- Warning: non-integer #successes in a binomial glm! (survey packages)
- Remove columns from dataframe where some of values are NA
- Difference between as.POSIXct/as.POSIXlt and strptime for converting character vectors to POSIXct/POSIXlt
- How to deal with “data of class uneval” error from ggplot2?