0

My dataset ('data') has 1719 cases and 6779 variables. I need to weight the data using variable 'weight', however this is missing for 69 cases.

How can I delete the rows that have NA in the weight column, without deleting variables that have NA in any of the other 6778 columns?

2 Answers2

1

Index rows by columns containing NA

data[!is.na(data[,"weight"]),]

Data frames are indexed using square braces to specify rows then columns separated by a comma: data[rows, columns]

You can then provide a vector of rows, using the is.na function and preceeded by the exclamation mark, making it effectively an is.NOT.na.

!is.na(data[,"weight"])
rg255
  • 4,119
  • 3
  • 22
  • 40
0

From my 'useful R commands' file ....

# drop a row with a NA value in a cell
df <- df[ !is.na(df$variable), ]
sorearm
  • 409
  • 2
  • 10