Here is how to delete all the rows from a dataframe where any of the column value had the value NA.
For Example, this is my data frame:
> data
col1 col2 col3
1 262 162513 NA
2 760 23052 23052
3 761 1000 NA
4 763 1001 1001
5 764 13592 13592
6 765 NA 165340
7 771 1002 NA
8 772 1003 1003
9 779 21542 21542
10 780 1004 1004
Now i wish to delete all the rows that has col3 value NA.
Solution 1: Using a for loop and going row by row and checking
for col3's value as NA
for(i in 1:nrow(data)){
if(is.na(data$col3[i])){
data < - data[-i,]
}
}
Result:
> data
col1 col2 col3
2 760 23052 23052
4 763 1001 1001
5 764 13592 13592
6 765 NA 165340
8 772 1003 1003
9 779 21542 21542
10 780 1004 1004
Solution 2: dataFrame < - dataFrame[!is.na(dataFrame$columnName),]
//dataFrame is the data frame you are working on and
columnName is the column you wish to delete with value NA.
> data < - data[!is.na(data$col3),]
Result:
> data
col1 col2 col3
2 760 23052 23052
4 763 1001 1001
5 764 13592 13592
6 765 NA 165340
8 772 1003 1003
9 779 21542 21542
10 780 1004 1004