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,130 points
123 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,020 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 by anonymous
• 31,840 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
• 31,840 points
77 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
106 views
0 votes
2 answers

How to change column names of a Data frame?

Try this way - names(data frame) <- ...READ MORE

answered Aug 26 in Data Analytics by anonymous
• 31,840 points
92 views
0 votes
2 answers
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 in Data Analytics by anonymous
• 31,840 points
5,588 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
130 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
• 42,410 points
61 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 1,032 views