Reputation: 9806
I have a data of following sort:
head(df)
Sometimes I have value "Nan" in the answer column.
What I wish to do create a separate dataframe of all rows with this value and one without.
I used the following code:
dfwith <-df[ !grepl("Nan", df$answer) , ]
dfwithout <-df[ grepl("Nan", df$answer) , ]
But I get incorrect results, I don't understand why, where am I going wrong. Thanks in advance. Please help.
Edit:
> dput(droplevels(head(df)))
structure(list(X.run.number. = c(16L, 9L, 3L, 18L, 1L, 19L),
density = c(0.52, 0.52, 0.52, 0.52, 0.52, 0.52), k = c(100L,
100L, 100L, 100L, 100L, 100L), knt = c(2900L, 1700L, 500L,
2900L, 500L, 2900L), threshold = c(0.2, 0.2, 0.3, 0.4, 0.1,
0.5), X.step. = c(0L, 0L, 0L, 0L, 0L, 0L), answer = structure(c(4L,
5L, 1L, 6L, 2L, 3L), .Label = c("100.2767857", "106.9588889",
"107.1467647", "53.13833333", "64.54785714", "95.61115385"
), class = "factor"), percent = c(16.04938272, 18.51851852,
38.27160494, 34.56790123, 11.11111111, 45.67901235)), .Names = c("X.run.number.",
"density", "k", "knt", "threshold", "X.step.", "answer", "percent"
), row.names = c(NA, 6L), class = "data.frame")
Edit 2:
> dput(droplevels(tail(df)))
structure(list(X.run.number. = c(4488L, 4509L, 4502L, 4537L,
4530L, 4544L), density = c(0.52, 0.52, 0.52, 0.52, 0.52, 0.52
), k = c(600L, 600L, 600L, 600L, 600L, 600L), knt = c(19700L,
23300L, 22100L, 28100L, 26900L, 29300L), threshold = c(0.1, 0.1,
0.1, 0.1, 0.1, 0.1), X.step. = c(0L, 0L, 0L, 0L, 0L, 0L), answer = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = "\"Nan\"", class = "factor"), percent = c(11.11111111,
12.34567901, 6.172839506, 8.641975309, 11.11111111, 11.11111111
)), .Names = c("X.run.number.", "density", "k", "knt", "threshold",
"X.step.", "answer", "percent"), row.names = 4545:4550, class = "data.frame")
Upvotes: 0
Views: 73
Reputation: 281
I think it has something to do with you having NaN instead of NA. Try this.
df[complete.cases(df), ]
Upvotes: 1
Reputation: 281
For only getting rows without NA, you can use:
dfwithout <- na.omit(df)
And if you need to change Nan to NA or NaN, you can use:
df <- gsub("Nan", "NA", df)
You can see which rows and columns have NA with:
which(is.na(df), arr.ind=TRUE)
Upvotes: 1