R language Removal missing value na.rm

Source: Internet
Author: User

> a <- 2:12> b <- seq (2,23,2) > c <- c (1:11) ^3 > d <- c (5:8, 30:36) > df <- data.frame (a,b,c,d) > df$a[ df$a==8] <- na > df$b[df$b==8] <- na > df$c[df$c==8]  <- na > df$d[df$d==8] <- na > df$d[df$d==32] <- na > df    a  b    c  d1   2   2    1  52   3  4   NA   63   4  6   27  74   5 NA    64 NA5   6 10  125 306   7 12   216 317  na 14  343 na8   9 16  512  339  10 18  729 3410 11 20 1000 3511 12 22 1331 36//  Remove the corresponding line > bad.d <- is.na (DF$D) based on the fourth column, which is d   na > bad.d [1]  false false false  true false false  true false false  false false> df[!bad.d,]    a  b    c   d1   2  2    1  52   3   4   na  63   4  6   27  75    6 10  125 306   7 12  216 318    9 16  512 339  10 18  729 3410 11 20  1000 3511 12 22 1331 36//  removes the corresponding row > df[complete.cases based on the na  of the second and third columns (DF [, 2:3]),]    a  b    c  d1   2  2     1  53   4  6   27  75   6  10  125 306   7 12  216 317  na 14   343 NA8   9 16  512 339  10 18   729 3410 11 20 1000 3511 12 22 1331 36//  according to the second column and the NA in column   fourth, Remove the corresponding line > df[complete.cases (Df[,c (2,4)),]    a  b     C  d1   2  2    1  52   3   4   NA  63   4  6   27   75   6 10  125 306   7 12  216 318    9 16  512 339  10 18  729 3410 11 20 1000 3511  12 22 1331 36//  removes the corresponding row > df[complete.cases (DF), based on the Na of all columns,]    a   b    c  d1   2  2    1   53   4  6   27  75   6 10   125 306   7 12  216 318   9 16   512 339  10 18  729 3410 11 20 1000 3511 12  22 1331 36> //  This effect is the same as the above df[complete.cases (DF),]  > na.omit (DF)      a  b    c  d1   2  2     1  53   4  6   27  75    6 10  125 306   7 12  216 318   9 16   512 339  10 18  729 3410 11 20 1000 3511 12  22 1331 36//  calculates the average of a column,  removes the NA value > mean (df$d, na.rm=true)


This article is from the "R Language Learning history" blog, please be sure to keep this source http://matrix6ro.blog.51cto.com/1746429/1791239

R language Removal missing value na.rm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.