Était-ce utile?

Question

How to remove empty rows from an R data frame?

R ProgrammingServer Side ProgrammingProgramming

During the survey or any other medium of data collection, getting all the information from all units is not possible. Sometimes we get partial information and sometimes nothing. Therefore, it is possible that some rows in our data are completely blank and some might have partial data. The blank rows can be removed and the other empty values can be filled with methods that helps to deal with missing information.

Example

Consider the below data frame, it has some missing rows and some missing values −

> x1<-c(rep(c(1,2,3),times=5),"","","",2,1)
> x2<-rep(c(2,4,"",4,""),each=4)
> x3<-rep(c(5,4,2,""),times=c(2,5,3,10))
> df<-data.frame(x1,x2,x3)
> df
x1 x2 x3
1 1 2 5
2 2 2 5
3 3 2 4
4 1 2 4
5 2 4 4
6 3 4 4
7 1 4 4
8 2 4 2
9 3 2
10 1 2
11 2
12 3
13 1 4
14 2 4
15 3 4
16 4
17
18
19 2
20 1

Here, we can see that the rows 17 and 18 are complete blank that means we do not have any data in them. Hence, we can remove them from the data frame as shown below −

> df[!apply(df == "", 1, all),]
x1 x2 x3
1 1 2 5
2 2 2 5
3 3 2 4
4 1 2 4
5 2 4 4
6 3 4 4
7 1 4 4
8 2 4 2
9 3 2
10 1 2
11 2
12 3
13 1 4
14 2 4
15 3 4
16 4
19 2
20 1
raja
Published on 11-Aug-2020 16:22:10
Advertisements
Était-ce utile?
Non affilié à Tutorialspoint
scroll top