TransWikia.com

Drop NA values in an imported xlxs document with R

Data Science Asked by Chris Kehl on August 20, 2021

I have the attached document. I’ve imported an xlxs document into R. I would like to get rid of all rows containing NA. I’ve tried the following but it isn’t working.

# install packages Note# tidyverse installs packages for dplyr and ggplot2
install.packages("tidyverse")
install.packages("readxl")

#open the library
library(tidyverse)
library(readxl)

setwd("~/Documents/UofL/Data_Science/CECS_635/week_3/")

un <- read_excel("UnitedNations.xlsx")

un1 %>% drop_na(un,)

enter image description here

3 Answers

its should be working

data_set<-na.omit(un)

If want to handle NA's to mean or median:

na.mean(un, option = "mean")

Source: https://www.rdocumentation.org/packages/imputeTS/versions/2.7/topics/na.mean

Answered by fuwiak on August 20, 2021

I was able to get the application to drop the NA values by converting the xlsx file to a csv file. Once the csv was uploaded into R, I was able to omit the NA rows.

# to remove the NA values I converted the xlsx file to csv  

    united_nations <- read_csv("UnitedNations.csv", col_names = TRUE)

     # used the na.omit option to remove rows with NA 
united_nations = na.omit(united_nations)

enter image description here

Answered by Chris Kehl on August 20, 2021

Or in a data frame:

mydata <- data.frame(c(1,2,NA,3))
mydata <- mydata[complete.cases(mydata),]

Answered by Peter on August 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP