Hello,
I have two data frames, each with three rows:
```
df_a <- data.frame(a = letters[1:3], b = LETTERS[1:3], q = c("", "", ""),
stringsAsFactors = FALSE)
df_b <- data.frame(a = letters[4:6], b = LETTERS[4:6], q = c("", "", "1.5"),
stringsAsFactors = FALSE)
```
I need to test whether the dataframe has been selected and if there is
a value in the q column. I combined in the following test:
```
if (nrow(df_a) == 0 || unique(df_a$q) == "") {
print("empty")
}
if (nrow(df_b) == 0 || unique(df_b$q) == "") {
print("empty")
}
```
The test for df_a worked as expected:
```
> nrow(df_a) == 0
[1] FALSE
> unique(df_a$q) == ""
[1] TRUE
> (nrow(df_a) == 0 || unique(df_a$q) == "")
[1] TRUE
> if (nrow(df_a) == 0 || unique(df_a$q) == "") {
+ print("empty")
+ }
[1] "empty"
```
but the one for df_b did not:
```
> nrow(df_b) == 0
[1] FALSE
> unique(df_b$q) == ""
[1]  TRUE FALSE
> (nrow(df_b) == 0 || unique(df_b$q) == "")
[1] TRUE
> unique(df_b$q)
[1] ""    "1.5"
```
I say that it did not work because unique(df_b$q) IS NOT "", hence
`(nrow(df_b) == 0 || unique(df_b$q) == "")` should be FALSE, instead R
evaluated the first element of unique(df_b$q) == "", which is TRUE.
How can I properly implement a logic test on vectors?
 Thank you

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to