- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove NULL values from a list in R?
The value NULL is used to represent an object especially a list of length zero. If a list contains NULL then we might want to replace it with another value or remove it from the list if we do not have any replacement for it. To remove the NULL value from a list, we can use the negation of sapply with is.NULL.
Examples
x<-list(rep(10,5),NULL,12,"Tutorialspoint",2006,NULL,NULL,NULL,TRUE,c(rep(1,5),rep(2,5),NULL,"Education","Video Lectures","Online",NULL,"Over Million Visitors")) x [[1]] [1] 10 10 10 10 10 [[2]] NULL [[3]] [1] 12 [[4]] [1] "Tutorialspoint" [[5]] [1] 2006 [[6]] NULL [[7]] NULL [[8]] NULL [[9]] [1] TRUE [[10]] [1] "1" "1" "1" [4] "1" "1" "2" [7] "2" "2" "2" [10] "2" "Education" "Video Lectures" [13] "Online" "Over Million Visitors" x<-x[!sapply(x,is.null)] x [[1]] [1] 10 10 10 10 10 [[2]] [1] 12 [[3]] [1] "Tutorialspoint" [[4]] [1] 2006 [[5]] [1] TRUE [[6]] [1] "1" "1" "1" [4] "1" "1" "2" [7] "2" "2" "2" [10] "2" "Education" "Video Lectures" [13] "Online" "Over Million Visitors" y<-list(c(5,10,15),NULL,c(rep("A",4)),rep(5,15,10,times=4),c(78,91,56,47,58,26,36,74,84),NULL,NULL,NULL) y [[1]] [1] 5 10 15 [[2]] NULL [[3]] [1] "A" "A" "A" "A" [[4]] [1] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 [[5]] [1] 78 91 56 47 58 26 36 74 84 [[6]] NULL [[7]] NULL [[8]] NULL y<-y[!sapply(y,is.null)] y [[1]] [1] 5 10 15 [[2]] [1] "A" "A" "A" "A" [[3]] [1] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 [[4]] [1] 78 91 56 47 58 26 36 74 84
Advertisements