How to subset rows containing NA in a chosen column of a data frame?

0 votes

Consider a data frame from a csv file. The chosen data frame has observed values and a column that contains the date( a measurement that has been taken).

Just in case if the record isn't present then it contains the value as NA for missing data.

Col1  Col2 
10   2018/01/01
20   NA
30   2018/05/01

We would like to use the subset command to define a new data frame. This new data frame contains only rows taht have NA values from the column(Col2).

In the example given, only Row 2 will be contained in the new data frame.

The command is as follows:

new_data<-subset(data,data$Col2=="NA") 

This does not work, as the resulting data frame has no row entries.

In the original csv file, the values NA are exchanged with NULL.

The same command produces the desired result:

new_data<-subset(data,data$Col2=="NULL").

How can I get this method working, if for the character string the value NA is provided in the original .csv file?

Apr 26, 2018 in Data Analytics by DataKing99
• 8,100 points
83 views

1 answer to this question.

0 votes

I would suggest you, to never to use =='NA' to test for missing values.

Use is.na() instead.

This should serve your purpose:

new_data <- data[rowSums(is.na(data)) > 0,]

Just in case, if  you want to check a particular column, you can also use

new_data <- data[is.na(data$Col),]

OR In case you have NA character values, first run

data[data=='NA'] <- NA

to replace them with missing values.

answered Apr 26, 2018 by kappa3010
• 2,020 points

Related Questions In Data Analytics

0 votes
1 answer

How to replace all occurrences of a character in a character column in a data frame in R

If you used sub() to replace the ...READ MORE

answered Jun 28 in Data Analytics by anonymous
• 19,830 points
41 views
0 votes
1 answer

How to spilt a column of a data frame into multiple columns

it is easily achievable by using "stringr" ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,700 points
69 views
0 votes
1 answer

How to change column names of a Data frame?

Easiest way: names(prices)[1]<-paste0("old") names(prices)[2]<-paste0("new") names(prices)[3]<-paste0("best") #or by using colnames colnames(prices)<-c("old","new","best") prices old ...READ MORE

answered Apr 11, 2018 in Data Analytics by DeepCoder786
• 1,700 points
53 views
0 votes
2 answers

How to change the value of a variable using R programming in a data frame?

You are getting this error since the ...READ MORE

answered 4 days ago in Data Analytics by anonymous
• 19,830 points
114 views
0 votes
1 answer

How to remove rows with missing values (NAs) in a data frame?

You can use complete.cases in the following ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,140 points
4,091 views
0 votes
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,700 points
59 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 4, 2018 in Data Analytics by Kalgi
• 39,170 points
44 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to drop factor levels in a subsetted data frame?

You can use factor(ff) to drop levels ...READ MORE

answered Apr 17, 2018 in Data Analytics by kappa3010
• 2,020 points

edited Apr 17, 2018 by kappa3010 742 views