Returning values after last NA in a vector

r replace missing values with na
r remove rows with na in one column
remove na values in r
find rows with na in r
find columns with na in r
replace missing values in r
is.na r
na.rm = true

Returning values after last NA in a vector

I can remove all NA values from a vector

v1 <- c(1,2,3,NA,5,6,NA,7,8,9,10,11,12)
v2 <- na.omit(v1)
v2

but how do I return a vector with values only after the last NA

c( 7,8,9,10,11,12)

Thank you for your help.

You could detect the last NA with which and add 1 to get the index past the last NA and index until the length(v1):

 v1[(max(which(is.na(v1)))+1):length(v1)]
[1]  7  8  9 10 11 12

Missing Values in R, The is.na() function will find missing values for you: this function returns a logical vector the same length as its argument, with T for missing values and F for� Access Last Value with length Function A commonly used R function for accessing the last value of a vector or array is the length function. The length function returns (as the name of the function indicates) the length of a data object. We can use this information to create a subset of our data containing only the last element of our data:

Here’s an alternative solution that does not use indices and only vectorised operations:

after_last_na = as.logical(rev(cumprod(rev(! is.na(v1)))))
v1[after_last_na]

The idea is to use cumprod to fill the non-NA fields from the last to the end. It’s not a terribly useful solution in its own right (I urge you to use the more obvious, index range based solution from other answers) but it shows some interesting techniques.

Dealing with Missing Values � UC Business Analytics R , Recode missing values; Exclude missing values. Test for missing values. To identify missing values use is.na() which returns a logical vector� std:: vector < int > create_vector (const size_t N) {std:: vector < int > v; v. resize (N, 0xDEADC0DE); return v;} Here the vector instance is being returned by value, which means potential deep copying of the object to the context of the caller. Immediately a question raises: if the vector is huge, such deep copying can be expensive but

You could detect the last NA with which

v1[(tail(which(is.na(v1)), 1) + 1):length(v1)]
# [1]  7  8  9 10 11 12

However, the most general - as @MrFlick pointed out - seems to be this:

tail(v1, -tail(which(is.na(v1)), 1))
# [1]  7  8  9 10 11 12

which also handles the following case correctly:

v1[13] <- NA
tail(v1, -tail(which(is.na(v1)), 1))
# numeric(0)

To get the null NA case, too,

v1 <- 1:13

we can do

if (any(is.na(v1))) tail(v1, -tail(which(is.na(v1)), 1)) else v1
# [1]  1  2  3  4  5  6  7  8  9 10 11 12 13

Data

v1 <- c(1, 2, 3, NA, 5, 6, NA, 7, 8, 9, 10, 11, 12)

nth: Extract the first, last or nth value from a vector in dplyr, A vector. n. For nth_value() , a single integer specifying the position. Negative integers index from the end (i.e. -1L will return the last value in� Returning values after last NA in a vector I can remove all NA values from a vector v1 <- c(1,2,3,NA,5,6,NA,7,8,9,10,11,12) v2 <- na.omit(v1) v2 but how do I return a vector with values only

v1 <- c(1,2,3,NA,5,6,NA,7,8,9,10,11,12)
v1[seq_along(v1) > max(0, tail(which(is.na(v1)), 1))]
#[1]  7  8  9 10 11 12

v1 = 1:5
v1[seq_along(v1) > max(0, tail(which(is.na(v1)), 1))]
#[1] 1 2 3 4 5

v1 = c(1:5, NA)
v1[seq_along(v1) > max(0, tail(which(is.na(v1)), 1))]
#integer(0)

How to remove NA values from a Vector in R?, I have to sum the vector without including NA values? Want to remove NA from the vector Hi, The below code returns rows without READ� So, you can use the indices to order the values the way you want. You also can store the indices you want to retrieve in another vector and give that vector as an argument, as in the following example: > indices <- c(5,11,3) > numbers[indices] [1] 26 20 28. You can use indices to drop values from a vector as well.

The following will do what you want.

i <- which(is.na(v1))
if(i[length(i)] < length(v1)){
  v1[(i[length(i)] + 1):length(v1)]
}else{
  NULL
}
#[1]  7  8  9 10 11 12

R Return Value from Function, Functions without return(). If there are no explicit returns from a function, the value of the last evaluated expression is returned automatically in R. Filling in NAs with last non-NA value Problem. You want to replace NA’s in a vector or factor with the last non-NA value. Solution. This code shows how to fill gaps in a vector. If you need to do this repeatedly, see the function below. The function also can fill in leading NA’s with the first good value and handle factors properly.

How does R handle missing values?, NA is not a string or a numeric value, but an indicator of missingness. It will return a TRUE/FALSE vector with as any elements as the vector we provide. be last or first in a sorted order by indicating TRUE or FALSE for the na.last argument. When you execute find with a relational operation like X>1, it is important to remember that the result of the relational operation is a logical matrix of ones and zeros. For example, the command [row,col,v] = find (X>1) returns a column vector of logical 1 ( true ) values for v.

Vector Indexing, If we start with a simple vector, we can extract each element from the vector by [ 1] NA. Positional indices can also involve an R expression For example, you may want to extract the last element of a vector of unknown length To do But note that positions outside of the length of vector will be returned as missing values: Why did you define your vector instance as a pointer? I'd start by just getting rid of the pointer (everywhere) and see if that works. Jun 6, 2016 at 1:17pm UTC

Using vectors and matrices in R, We'll see later that R provides an object known as a list that can store different We must use the builtin is.na function, which will return TRUE if a value is missing The last two arguments to matrix tell it the number of rows and columns the� na.last: for controlling the treatment of NAs. If TRUE, missing values in the data are put last; if FALSE, they are put first; if NA, they are removed. partial: NULL or an integer vector of indices for partial sorting. method: character string specifying the algorithm used. index.return

Comments
  • Fails in limit case v1[13] <- NA.
  • By far the fastest
  • What do you mean fail? It returns NA, and I assume thats the correct response, apart from returning 2 NA's, I guess would be a problem in some niche applications @RuiBarradas
  • @HectorHaffenden The question ask for the vector elements "after the last NA". WHen the last element is NA, it should return nothing, meaning, NULL.
  • Think I have misunderstood, what would you expect the output to be if the vector is c(1,2,3,NA) @RuiBarradas
  • Or if you want to return the values of v1 when the vector does not contain consecutive indices (like v1 <- c(5,NA,20,30), something like tail(v1, -tail(which(is.na(v1)),1)) should work more generally
  • 13 is not in v1.
  • @RuiBarradas Thanks for the pointer, there was something missing.
  • @MrFlick Clever, this nesting, noted!
  • @MrFlick We'll fail if there's no NA at all, though; v1 <- 1:13.
  • But a bit slow, due to tail. (Mine is an order of magnitude faster, tested with microbenchmark.)