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,250 points
10,404 views

2 answers 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,090 points
0 votes
You can give this a try.

subset(dataframe, is.na(dataframe$col2)) .

This will return you a subset of a data frame with col2 having NA.
answered Aug 21, 2019 by anonymous
• 33,030 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 29, 2019 in Data Analytics by anonymous
• 33,030 points
17,898 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,720 points
1,721 views
+1 vote
3 answers

How to change column names of a Data frame?

Hi, To change the name of a column ...READ MORE

answered Oct 30, 2020 in Data Analytics by MD
• 95,460 points
1,667 views
+1 vote
3 answers

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

Try this: df$symbol <- as.character(df$symbol) df$symbol[df$sym ...READ MORE

answered Jan 11, 2019 in Data Analytics by Tyrion anex
• 8,700 points
35,749 views
0 votes
2 answers

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

Hi, The below code returns rows without ...READ MORE

answered Aug 20, 2019 in Data Analytics by anonymous
• 33,030 points
14,725 views
+1 vote
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,720 points
1,619 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 5, 2018 in Data Analytics by Kalgi
• 52,350 points
1,050 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,090 points

edited Apr 17, 2018 by kappa3010 5,362 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP