Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:选择或,但不能同时选择两者_R_Operators_Xor_Grepl - Fatal编程技术网

R:选择或,但不能同时选择两者

R:选择或,但不能同时选择两者,r,operators,xor,grepl,R,Operators,Xor,Grepl,我对编码完全是新手,所以如果这应该很容易解决或找到,请原谅我-可能它太简单了,到目前为止没有人费心解释,或者我只是没有用正确的关键字搜索 我的数据集中有一列包含字母f、n、I的所有可能组合。现在我只想找到那些包含f或n的行,而不是同时包含这两个行。这可能是f,或fi,或n,或ni。 然后我想在箱线图中比较这两组行。理想情况下,我会有两个框:一个是所有属于f组的数据点,包括fi,另一个是所有属于n组的数据点,包括ni 我的数据集示例: df <- data.frame(D = c("f",

我对编码完全是新手,所以如果这应该很容易解决或找到,请原谅我-可能它太简单了,到目前为止没有人费心解释,或者我只是没有用正确的关键字搜索

我的数据集中有一列包含字母f、n、I的所有可能组合。现在我只想找到那些包含f或n的行,而不是同时包含这两个行。这可能是f,或fi,或n,或ni。 然后我想在箱线图中比较这两组行。理想情况下,我会有两个框:一个是所有属于f组的数据点,包括fi,另一个是所有属于n组的数据点,包括ni

我的数据集示例:

df <- data.frame(D = c("f", "f", "fi", "n", "ni", "ni", "fn", "fn"), y = c(1, 0.8, 1.1, 2.1, 0.9, 8.8, 1.7, 5.4))

   D   y
1  f 1.0
2  f 0.8
3 fi 1.1
4  n 2.1
5 ni 0.9
6 ni 8.8
7 fn 1.7
8 fn 5.4
然后以某种方式将1,2,3和4,5,6分别放在一组中,以箱线图的形式绘制

到目前为止,我只成功地获得了一个子集,其中只有f或n的条目,但没有fi、ni等,这不是我想要的,使用以下代码:

df2<-df[df$D==c("f","n"),]

df2我认为您的最后一个示例非常接近
xor
仅适用于返回
logical
的对象,如
TRUE
FALSE
,但
match
实际返回整数位置。因此,只需将
grepl
xor
一起使用即可:

xor(grepl("f", df$D), grepl("n", df$D))
或者你可以想象:

library(functional)
Reduce(xor, lapply(c("f", "n"), grepl, df$D))

我们都在某一点上切齿,所以我将尝试为您构建一个适合这个问题的示例。那么:

# simulate a data.frame with "all possible combinations" of singles and pairs
df <- data.frame(txt = as.character(outer(c("i", "f", "n"), c("", "i", "f", "n"), paste0)),
                 stringsAsFactors = FALSE)
# create an empty factor variable to contain the result
df$has_only <- factor(rep(NA, nrow(df)), levels = 1:2, labels = c("f", "n"))
# replace with codes if contains either f or n, not both(f, n)
df$has_only[which(grepl("f", df$txt) & !grepl("f.*n|n.*f", df$txt))] <- "f"
df$has_only[which(grepl("n", df$txt) & !grepl("f.*n|n.*f", df$txt))] <- "n"
df
##    txt has_only
## 1    i     <NA>
## 2    f        f
## 3    n        n
## 4   ii     <NA>
## 5   fi        f
## 6   ni        n
## 7   if        f
## 8   ff        f
## 9   nf     <NA>
## 10  in        n
## 11  fn     <NA>
## 12  nn        n
plot(df$has_only)

可再现性示例?显示数据集的几行(具有所有不同的组合)并指出您希望选择的行将提供可再现的数据集和解决方案中可能针对的答案。听起来很简单。请
test@Pascal,抱歉,添加了一个示例!
xor(grepl("f", df$D), grepl("n", df$D))
library(functional)
Reduce(xor, lapply(c("f", "n"), grepl, df$D))
# simulate a data.frame with "all possible combinations" of singles and pairs
df <- data.frame(txt = as.character(outer(c("i", "f", "n"), c("", "i", "f", "n"), paste0)),
                 stringsAsFactors = FALSE)
# create an empty factor variable to contain the result
df$has_only <- factor(rep(NA, nrow(df)), levels = 1:2, labels = c("f", "n"))
# replace with codes if contains either f or n, not both(f, n)
df$has_only[which(grepl("f", df$txt) & !grepl("f.*n|n.*f", df$txt))] <- "f"
df$has_only[which(grepl("n", df$txt) & !grepl("f.*n|n.*f", df$txt))] <- "n"
df
##    txt has_only
## 1    i     <NA>
## 2    f        f
## 3    n        n
## 4   ii     <NA>
## 5   fi        f
## 6   ni        n
## 7   if        f
## 8   ff        f
## 9   nf     <NA>
## 10  in        n
## 11  fn     <NA>
## 12  nn        n
plot(df$has_only)
# simulate some continuous data
set.seed(50)
df$myvalue <- runif(nrow(df))

boxplot(myvalue ~ has_only, data = df)