0

I apologize if this is elementary or has been answered before, but I haven't found an answer to my question despite extensive searching. I'm also very new to programming so please bear with me here.

I have a bunch of 25 by 2 matrices of data, however some of the cells have NA values. I'm looking to extract a subset of the matrix consisting of only the complete paired values (so no NA values).

So say I have:

3.6    4.2
9.2    8.4
4.8    NA
1.1    8.2
NA     11.6
NA     NA
2.7    3.5

I want:

3.6    4.2
9.2    8.4
1.1    8.2
2.7    3.5

Is there some function that would do this easily?

Thanks!

2 Answers2

2

Try this

df <- read.table(text = "3.6    4.2
9.2    8.4
4.8    NA
1.1    8.2
NA     11.6
NA     NA
2.7    3.5")
df[complete.cases(df), ]
#    V1  V2
# 1 3.6 4.2
# 2 9.2 8.4
# 4 1.1 8.2
# 7 2.7 3.5
lukeA
  • 53,097
  • 5
  • 97
  • 100
0
df[ apply(!is.na(df), 1, all) , ]

df <- data.frame(V1 = c(3.6,9.2,4.8,1.1,NA,NA,2.7),
                 V2 = c(4.2,8.4,NA,8.2,11.6,NA,3.5))

EDIT: I forgot na.omit or complete.cases Doh.

smci
  • 32,567
  • 20
  • 113
  • 146