Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.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中查找函数(x、p和';first';)_R_Matlab - Fatal编程技术网

在R中查找函数(x、p和';first';)

在R中查找函数(x、p和';first';),r,matlab,R,Matlab,早上好, 我试图替换R中的函数find(通过Matlab),特别是我想替换以下函数: I=find(x,p,'first')、L=find(x,q,'last')、A=I(x)和B=L(x)其中x是向量,p和q是我感兴趣的元素数 我不明白如何替换命令:“提取向量x的第一个(最后一个)p(或q)元素的位置。”x像@Dan一样,我不确定您想要什么:第一个p元素和最后一个q元素或元素p和元素q。对于后者: x <- c(FALSE, FALSE, FALSE, TRUE, TRUE, FALSE

早上好, 我试图替换R中的函数find(通过
Matlab
),特别是我想替换以下函数:
I=find(x,p,'first')、L=find(x,q,'last')、A=I(x)和B=L(x)
其中
x
是向量,
p
q
是我感兴趣的元素数


我不明白如何替换命令:“提取向量
x
的第一个(最后一个)
p(或q)
元素的位置。”

x像@Dan一样,我不确定您想要什么:第一个p元素和最后一个q元素或元素p和元素q。对于后者:

x <- c(FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE)
p <- 2
q <- 2

I = head(which(x),p)
L = tail(which(x),q)
require(stringi)
x <- c("dog", "cat", "FALSE", "TRUE", "BLUE")
p <- 2
q <- 3
unlist(stri_extract_words(str = x))[p]
unlist(stri_extract_words(str = x))[q]
require(stringi)

x查看
?头
?尾
?哪个
可能就是您要找的。而且,它可能只是索引。请参见
?“[”
。如果我们有示例输入和输出,则更容易判断。
x
是逻辑向量吗?x