Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (5.3k points)

I have a dataset with empty rows. I would like to remove them:

myData<-myData[-which(apply(myData,1,function(x)all(is.na(x)))),]

It works OK. But now I would like to add a column in my data and initialize the first value:

myData$newCol[1] <- -999

Error in `$<-.data.frame`(`*tmp*`, "newCol", value = -999) : 

  replacement has 1 rows, data has 0

Unfortunately, it doesn't work and I don't really understand why and I can't solve this. It worked when I removed one line at a time using:

TgData = TgData[2:nrow(TgData),]

Or anything similar.

It also works when I used only the first 13.000 rows.

But it doesn't work with my actual data, with 32.000 rows.

What did I do wrong? It seems to make no sense to me.

1 Answer

0 votes
by

To remove rows that contain NA, you can do the following:

Sample data:

data <- data.frame(A = c(1,2,3,NA), B =c(1, NA, 4,NA), C = c(4,6,7,NA), D = c(NA, NA, 3,NA),E= c(4, 8, NA,NA))

> data

   A  B  C  D  E

1  1  1  4 NA  4

2  2 NA  6 NA  8

3  3  4  7  3 NA

4 NA NA NA NA NA

To remove NA rows:

data[rowSums(is.na(data)) != ncol(data),]

  A  B C  D  E

1 1  1 4 NA  4

2 2 NA 6 NA  8

3 3  4 7  3 NA

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...