YouTip LogoYouTip

R Func Is Na

# R is.na() Function - Detect and Handle Missing Values [![Image 3: R Language Examples](https://example.com/images/up.gif) R Language Examples](https://example.com/r/r-examples.html) The R is.na() function is used to detect which elements in a vector or data frame are missing values NA. Missing value handling is an essential step in data analysis, directly affecting the accuracy of analysis results. The is.na() function syntax is as follows: is.na(x) anyNA(x) # Check if any NA exists complete.cases(x) # Check which rows have no NA **Parameter Description:** * **x** The vector, matrix, or data frame to be checked. ## Examples # Detect NA in a vector x <-c(10, NA, 20, NA, 30, 40) print("NA Detection:") print(is.na(x)) print(paste("NA Count:", sum(is.na(x)))) print(paste("Has NA:", anyNA(x))) # Handle NA in a data frame df<-data.frame( Name =c("Zhang San", "Li Si", "Wang Wu", "Zhao Liu"), Score =c(88, NA, 76, NA), Age =c(25, 30, NA, 28) ) print(" Data Frame:") print(df) # Find complete rows (without NA) print("Complete Rows:") print(df[complete.cases(df), ]) # Count NA in each column print("NA Count per Column:") print(colSums(is.na(df))) # Remove rows with NA df_clean <-na.omit(df) print(" After Removing NA:") print(df_clean) Executing the above code produces: "NA Detection:" FALSE TRUE FALSE TRUE FALSE FALSE "NA Count: 2" "Has NA: TRUE" "Data Frame:" Name Score Age1 Zhang San 88 252 Li Si NA 303 Wang Wu 76 NA 4 Zhao Liu NA 28 "Complete Rows:" Name Score Age1 Zhang San 88 25 "NA Count per Column:"Name Score Age 0 2 1 "After Removing NA:" Name Score Age1 Zhang San 88 25 [![Image 4: R Language Examples](https://example.com/images/up.gif) R Language Examples](https://example.com/r/r-examples.html)
← R Func LegendR Func Ifelse β†’