I am attempting to write a function which removes certain rows from the dataframe. For simplicity, let us assume that the condition for removal is that there is atleast one NA in the row.
df = data.frame(c("A","B","C"),c(1,NA,3))
fn = function (d) {
for (x in 1:nrow(d)) { for (y in 1:ncol(d)) {
if(is.na(d[x,y])) d = d[-x,]
}}}
fn(df)
PS: I am aware that there are better ways to remove rows with atleast one NA, i.e. df = df[-which(!complete.cases(df)] but I am interested to find out why the code I wrote does not work.
Aucun commentaire:
Enregistrer un commentaire