user11916948
user11916948

Reputation: 954

Extract positions in a data frame based on a vector

In a dataset I want to know where there are missing values, therefore i use which(is.na(df)). Then I do for example imputation in this dataset and thereafter I want to extract the imputed positions. But I dont know how to extract these data. Does anyone have suggestions? Thanks!

 id <- factor(rep(letters[1:2], each=5))
 A <- c(1,2,NA,67,8,9,0,6,7,9)
 B <- c(5,6,31,9,8,1,NA,9,7,4)
 C <- c(2,3,5,NA,NA,2,7,6,4,6)
 D <- c(6,5,89,3,2,9,NA,12,69,8)
 df <- data.frame(id, A, B,C,D) 
 df

   id  A  B  C  D
1   a  1  5  2  6
2   a  2  6  3  5
3   a NA 31  5 89
4   a 67  9 NA  3
5   a  8  8 NA  2
6   b  9  1  2  9
7   b  0 NA  7 NA
8   b  6  9  6 12
9   b  7  7  4 69
10  b  9  4  6  8

pos_na <- which(is.na(df))

 pos_na
[1] 13 27 34 35 47

# after imputation
id <- factor(rep(letters[1:2], each=5))
A <- c(1,2,4,67,8,9,0,6,7,9)
B <- c(5,6,31,9,8,1,65,9,7,4)
C <- c(2,3,5,8,2,2,7,6,4,6)
D <- c(6,5,89,3,2,9,6,12,69,8)
df <- data.frame(id, A, B,C,D) 
df
       id  A  B C  D
    1   a  1  5 2  6
    2   a  2  6 3  5
    3   a  4 31 5 89
    4   a 67  9 8  3
    5   a  8  8 2  2
    6   b  9  1 2  9
    7   b  0 65 7  6
    8   b  6  9 6 12
    9   b  7  7 4 69
    10  b  9  4 6  8
    

Wanted output: 4,65,8,2 6

Upvotes: 0

Views: 237

Answers (2)

akrun
akrun

Reputation: 887213

Instead of wrapping with which, we can keep it as a logical matrix

i1 <- is.na(df[-1])

Then, after the imputation, just use the i1

df[-1][i1]
#[1]  4 65  8  2  6

Note, the -1 indexing for columns is to remove the first column which is 'character'

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389012

To store positions of NA use which with arr.ind = TRUE which gives row and column numbers.

pos_na <- which(is.na(df), arr.ind = TRUE)
pos_na

#     row col
#[1,]   3   2
#[2,]   7   3
#[3,]   4   4
#[4,]   5   4
#[5,]   7   5

So that after imputation you can extract the values directly.

as.numeric(df[pos_na])
[1]  4 65  8  2  6

Upvotes: 1

Related Questions