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,680 points
5,253 views

4 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,700 points
0 votes
data = data[!is.na(data)]\
answered Apr 5 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
answered Jun 18 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 Jul 31 by anonymous
• 19,510 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 26, 2018 in Data Analytics by shams
• 3,580 points
89 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,680 points
111 views
0 votes
1 answer

How to replace NA values in a dataframe with Zero's ?

It is simple and easy: df1<-as.data.frame(matrix(sample(c(NA, 1:10), 100, ...READ MORE

answered Apr 10, 2018 in Data Analytics by CodingByHeart77
• 3,680 points
99 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,071 views
0 votes
1 answer

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

You have various options to count the ...READ MORE

answered Apr 12, 2018 in Data Analytics by darklord
• 6,140 points
78 views
0 votes
1 answer

R programming: Removing NA values

You can make use of the "na.rm" ...READ MORE

answered Jan 31 in Data Analytics by Tyrion anex
• 8,280 points
78 views
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,700 points
79 views
0 votes
1 answer

In a dpylr pipline how to use sample and seq?

For avoiding rowwise(), I prefer to use ...READ MORE

answered Apr 6, 2018 in Data Analytics by DeepCoder786
• 1,700 points
71 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,700 points
94 views
0 votes
2 answers

How to write lines to a text file in R?

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

answered May 24 in Data Analytics by anonymous
1,411 views