emperor <- rbind(cbind('Augustus','Tiberius'),cbind('Caligula','Claudius'))
如何返回包含序列“us”的所有单元格的行号和列号,即 [1,1]、[1,2]、[2,2]?
我们可以使用grepl
得到一个vector
的逻辑索引,转换为matrix
与原件尺寸相同matrix
('emperor') 并用which
with arr.ind=TRUE
.
which(matrix(grepl('us', emperor), ncol=ncol(emperor)), arr.ind=TRUE)
# row col
#[1,] 1 1
#[2,] 1 2
#[3,] 2 2
或者另一种方式来转换grepl
输出是通过分配dim
到那个dim
'emperor' 并用which
.
which(`dim<-`(grepl('us', emperor), dim(emperor)), arr.ind=TRUE)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)