-1

I have a data frame where there's one missing value, i need to ignore the missing value and perform the further calculation

df1:

x1    x2    x3    x4    x5
      8      6     3    5

expected output:

x2   x3    x4    x5
8    6     3     5

i need to ignore the missing value. please help, thanks

Robby star
  • 281
  • 2
  • 13
  • https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – jogo Jun 18 '19 at 19:01

2 Answers2

0

An option is to use colSums to remove the columns having at least one NA

df1[colSums(is.na(df1))== 0]
#  x2 x3 x4 x5
#1  8  6  3  5

If it is blank ("") instead of NA

df1[colSums(df1 == "") == 0]

data

df1 <- structure(list(x1 = NA, x2 = 8L, x3 = 6L, x4 = 3L, x5 = 5L), 
   class = "data.frame", row.names = c(NA, 
-1L))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

As Akrun notes, the solution depends on whether the missing value is an NA or blank (or space etc.):

x <- c(NA,"",8,6,3,5)

x[!is.na(x)]
# "" "8" "6" "3" "5"
x[x!='' & x!=' ']
# NA "8" "6" "3" "5"
x[!is.na(x) & x!='' & x!=' ']
# "8" "6" "3" "5"
rg255
  • 4,119
  • 3
  • 22
  • 40