How to remove NA values from a Vector in R

0 votes
Is there any way to remove NA values from a vector?

If we have a vector consisting of lot values with NA values, how to remove it?

Suppose I have to sum the vector without including NA values?
Apr 23, 2018 in Data Analytics by CodingByHeart77
• 3,740 points
193,145 views

5 answers to this question.

0 votes

To calculate sum we can use "sum()" Func by passing argument "na.rm=TRUE"

x<-c(1,23,45,NA,155,78,NA)

sum(x,na.rm=TRUE)

Want to remove NA from the vector

x<-x[!is.na(x)]

answered Apr 23, 2018 by DeepCoder786
• 1,720 points
0 votes
data = data[!is.na(data)]\
answered Apr 5, 2019 by anonymous
Yup!! Very accurate to remove NA values.
0 votes

using the function "complete.cases"

for example, if you want to remove the NA in dataset ,

> x <- c(1, 2, NA, 4, NA, 5)
> y <- c("a", "b", NA, "d", NA, "f")
> good <- complete.cases(x, y)
> good
[1] TRUE TRUE FALSE TRUE FALSE TRUE
> x[good]
[1] 1 2 4 5
> y[good]
[1] "a" "b" "d" "f"
 

above is from coursera lecture R from Professor Peng

Interested in a career in data analysis? Our Data Analyst Certification Course will equip you with the tools and techniques you need to succeed.

answered Jun 18, 2019 by anonymous
That's a nice way to go about it. It's an unusual approach but got the same expected output. Thanks
0 votes

You can try na.omit() or na.exclude() too. It might help you.

When you view y1,y2, they would be shown as below.

It would show you rows with indices and those with nulls.

answered Aug 1, 2019 by anonymous
• 33,030 points
0 votes

Hello team,

you can use na.omit

x <- c(NA, 3, NA, 5)
na.omit(x)
[1] 3 5
attr(,"na.action")
[1] 1 3
attr(,"class")
[1] "omit"
answered Dec 9, 2020 by anonymous
• 82,880 points

Related Questions In Data Analytics

0 votes
1 answer

How can I delete multiple values from a vector in R?

The %in% operator tells  which elements are ...READ MORE

answered Apr 27, 2018 in Data Analytics by shams
• 3,670 points
6,037 views
0 votes
1 answer

How to write a custom function which will replace all the missing values in a vector with the mean of values in R?

Consider this vector: a<-c(1,2,3,NA,4,5,NA,NA) Write the function to impute ...READ MORE

answered Jul 4, 2018 in Data Analytics by CodingByHeart77
• 3,740 points
4,324 views
0 votes
1 answer

How to access the last value from a vector in R?

Hi@akhtar, You can use the tail function in ...READ MORE

answered Oct 30, 2020 in Data Analytics by MD
• 95,440 points
952 views
0 votes
1 answer

How to remove an element from a list in R?

Hi@akhtar, You can remove a value from a ...READ MORE

answered Oct 30, 2020 in Data Analytics by MD
• 95,440 points
4,053 views
+1 vote
2 answers

How to count the number of elements with the values in a vector?

Use dplyr function group_by(). > n = as.data.frame(num) > ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
4,692 views
0 votes
2 answers

R programming: Removing NA values

Hi, Use na.rm = TRUE or Fetch values which has ...READ MORE

answered Aug 30, 2019 in Data Analytics by anonymous
• 33,030 points
888 views
0 votes
1 answer
0 votes
1 answer

By using dpylr package sum of multiple columns

Basically here we are making an equation ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
2,075 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,718 views
0 votes
3 answers

How to write lines to a text file in R?

sink("outfile.txt") cat("hello") cat("\n" ...READ MORE

answered May 24, 2019 in Data Analytics by anonymous
18,696 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