Delete columns that have NA

Asked

Viewed 43 times

2

I have a row with 10 columns. In some of these columns you have NA. I want to delete these columns and leave only those that have values. I’m a beginner in R.

Type:

Name       Nota1  Nota2 Nota3  Nota4  Nota5
Patricia     9            8      6      

I want to stay in this case only with the columns of Name, Nota1, Nota3, Note.

Thank you in advance for your attention.

1 answer

3

The following code removes all columns with some NA in them.

dados <- dados[!sapply(dados, anyNA)]

head(dados)
# Nome Nota2 Nota8
#1    4     4     1
#2    4     3     1
#3    2     4     1
#4    2     4     4
#5    1     4     2
#6    4     3     2

Code to create test data.

Although in the question there is a column of names (characters) I believe that this is not relevant as to the purpose of the question, remove all columns where there is at least one NA. So these test data are just numerical, it’s easier to create them with replicate.

set.seed(1234)
dados <- replicate(10, sample(4, 20, TRUE))
is.na(dados) <- sample(200, 10)
dados <- as.data.frame(dados)
names(dados) <- c("Nome", paste0("Nota", 1:9))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.