Find the index position of the first non-NA value in an R vector?
Use a combination of is.na
and which
to find the non-NA index locations.
NonNAindex <- which(!is.na(z))
firstNonNA <- min(NonNAindex)
# set the next 3 observations to NA
is.na(z) <- seq(firstNonNA, length.out=3)
dplyr: Need help returning column index of first non-NA value in every row
You could use apply
as well:
data.frame(
"X1"=c(100,rep(NA,8)),
"X2"=c(NA,10,rep(NA,7)),
"X3"=c(NA,NA,1000,1000,rep(NA,5)),
"X4"=c(rep(NA,4),25,50,10,40,50),
"FirstNonNaPosition"=c(1,2,3,3,4,4,4,4,4)
) %>%
mutate(First_Non_NA_Pos = apply(., 1, function(x) which(!is.na(x))[1]))
X1 X2 X3 X4 FirstNonNaPosition First_Non_NA_Pos
1 100 NA NA NA 1 1
2 NA 10 NA NA 2 2
3 NA NA 1000 NA 3 3
4 NA NA 1000 NA 3 3
5 NA NA NA 25 4 4
6 NA NA NA 50 4 4
7 NA NA NA 10 4 4
8 NA NA NA 40 4 4
9 NA NA NA 50 4 4
Get index from last previous non-NA value
Using base R
y=1:length(x)
y[is.na(x)]=0
y=cummax(y)
y[!is.na(x)]=NA
y
[1] NA NA 2 NA NA 5 5 NA
squeeze to one_line from Henrik
replace(cummax(seq_along(x) * !is.na(x)), !is.na(x), NA)
Finding the first non NA value after a certain value by groups
foo = function(x, v = 5) {
ind_v = which(x == v)[1]
x[seq_along(x) <= ind_v] = NA
ind_non_na = which(!is.na(x))[1]
x[seq_along(x) > ind_non_na] = NA
x[max(ind_non_na, ind_v, na.rm = TRUE)] = max(0, ind_non_na, na.rm = TRUE)
return(x)
}
library(dplyr)
d %>%
group_by(Group) %>%
mutate(Index = foo(Value))
Get index and value of non-NA list element
1) Base R Assuming that the list L contains only scalars and NA's this returns a 2 column matrix with one row for each set of xy coordinates and an attribute recording which positions were omitted.
Omit the x=
and y=
if you don't want the column names. If you don't want the attribute recording the positions of the NA's append [,]
to the end of the line. If you know that there is only one scalar you might want to wrap it in c(...)
to produce a 2 element vector. If you prefer data frame output replace cbind
with data.frame
.
na.omit(cbind(x = seq_along(L), y = unlist(L)))
2) tidyverse or using the tidyverse
library(tibble)
library(tidyr)
drop_na(enframe(unlist(L)))
2a) which could alternately be written using pipes like this:
L %>% unlist %>% enframe %>% drop_na
Pick out the index of the first non-NA value preceding and succeeding an index in R?
If
index = 5
values=c(0.4, NA, 0.5, NA, NA, NA, 0.3 ,NA, NA, NA, 0.5, 0.3,0.5)
then the two values you are looking for are
max(which(!is.na(values))[which(!is.na(values))<index])
[1] 3
min(which(!is.na(values))[which(!is.na(values))>index])
[1] 7
dplyr::first() to choose first non NA value
Use na.omit
, compare:
first(c(NA, 11, 22))
# [1] NA
first(na.omit(c(NA, 11, 22)))
# [1] 11
Using example data:
d %>%
mutate(
value = case_when(
group == 2 & year ==2000 ~ NA_integer_,
group == 3 & year ==2002 ~ NA_integer_,
TRUE ~ value))%>%
group_by(group) %>%
mutate(
first = dplyr::first(na.omit(value)),
last = dplyr::last(na.omit(value)))
# # A tibble: 9 x 5
# # Groups: group [3]
# group year value first last
# <int> <dbl> <int> <int> <int>
# 1 1 2000 3 3 4
# 2 1 2001 8 3 4
# 3 1 2002 4 3 4
# 4 2 2000 NA 9 1
# 5 2 2001 9 9 1
# 6 2 2002 1 9 1
# 7 3 2000 5 5 9
# 8 3 2001 9 5 9
# 9 3 2002 NA 5 9
Related Topics
Reorder Rows Using Custom Order
Replace Characters from a Column of a Data Frame R
Adjusting Width of Tables Made with Kable() in Rmarkdown Documents
Align Violin Plots with Dodged Box Plots
Package Dependencies When Installing from Source in R
Run a Bash Script from an R Script
Ggplot2 Increase Space Between Legend Keys
Importing Only Every Nth Row from a .CSV File in R
Using R to "Click" a Download File Button on a Webpage
Combining Pivoted Rows in R by Common Value
Avoid Wasting Space When Placing Multiple Aligned Plots Onto One Page
Can't Load X11 in R After Os X Yosemite Upgrade
Showing Different Axis Labels Using Ggplot2 with Facet_Wrap
Create Convex Hull Polygon from Points and Save as Shapefile
Are Recursive Functions Used in R
What's the Difference in Using a Semicolon or Explicit New Line in R Code