Question :
I have a database in Excel with 12135 data and not all fields are filled and some individuals are of negative age and others are older than 17 years and others older than 70.
I need to know what SOFTWARE functions / filters to solve these two issues:
1 ° Delete all those that have some data not filled (ex: I have table of age, sex, organic unit, course, year of entry. I need to delete all data for these students and work only with students who have all the fields filled out);
2 ° Present all the information of students aged 17 years to 70 years, that is, the others are eliminated.
Answer :
Considering:
dados<-data.frame(idade=c(15,18,25,40,85,NA),
sexo=c("M","F",NA,"F","M","M"),
unidade.organica=c("EMEI CG","USP",NA,"UFSM","UFRGS","UPF"),
curso=c("TÉCNICO","SUPERIOR",NA,"SUPERIOR","SUPERIOR",NA),
ano.ingresso=c(2005,2011,NA,2014,1980,2015))
#exibindo o data.frame criado
dados
idade sexo unidade.organica curso ano.ingresso
1 15 M EMEI CG TÉCNICO 2005
2 18 F USP SUPERIOR 2011
3 25 <NA> <NA> <NA> NA
4 40 F UFSM SUPERIOR 2014
5 85 M UFRGS SUPERIOR 1980
6 NA M UPF <NA> 2015
NOTE: Please note that your missing data is represented by NA.
##Filtro dos dados ausentes (NA):
#Removendo os NA's com função na.omit()
dada.sem.NA<-na.omit(dados)
#Removendo NA's com a função indexadora which():
dados.sem.NA<-dados[-unique(which(is.na(dados),arr.ind = T)[,1]),]
For both functions: which()
or na.omit()
. The result is:
dados.sem.NA
idade sexo unidade.organica curso ano.ingresso
1 15 M EMEI CG TÉCNICO 2005
2 18 F USP SUPERIOR 2011
4 40 F UFSM SUPERIOR 2014
5 85 M UFRGS SUPERIOR 1980
The old filter can be applied to any variable dados
or dados.sem.NA
, see the cases:
#Filtro de idade na variável dados:
dados.por.idade<-dados[(dados.sem.NA$idade>17 & dados.sem.NA$idade<70), ]
The result is:
dados.por.idade
idade sexo unidade.organica curso ano.ingresso
2 18 F USP SUPERIOR 2011
3 25 <NA> <NA> <NA> NA
6 NA M UPF <NA> 2015
#Filtro de idade na variável dados.sem.NA:
dados.por.idade<-dados.sem.NA[(dados.sem.NA$idade>17 & dados.sem.NA$idade<70), ]
The result is:
dados.por.idade
idade sexo unidade.organica curso ano.ingresso
2 18 F USP SUPERIOR 2011
4 40 F UFSM SUPERIOR 2014
I hope I have helped. Good luck!