'I am trying to remove NA values from a row in R but getting "replacement has 863 rows, data has 940"

I have a dataset called "daily_activity". I am trying to remove the rows from the column called "TotalSteps" with values of "0". First, I converted them to values of NA. But when I try to remove them I get an error message.

daily_activity$TotalSteps[daily_activity$TotalSteps <=0] <- NA
daily_activity$TotalSteps <- na.omit(daily_activity$TotalSteps)

But I get the error message "Error in $<-.data.frame(*tmp*, TotalSteps, value = c(13162L, 10735L, : replacement has 863 rows, data has 940. I am very new to R so I apologize if this is a dumb question. Thank you so much for any help!

r


Solution 1:[1]

The daily_activity is a data.frame with 940 rows. TotalSteps is a column of that dataframe, so it has also 940 rows (cells). When you remove the NA values from the column TotalSteps, then the result would be a column of 863 rows (cells). Nevertheless, the dataframe must consist of columns with the same number rows (cells), thus the error you get.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bioblackgeorge