结合tapply和'not in'逻辑,使用R

bub*_*uie 7 r tapply notin

如何将tapply命令与'not in'逻辑结合起来?

目标:获得每个物种的中位萼片长度.

tapply(iris$Sepal.Length, iris$Species, median)
Run Code Online (Sandbox Code Playgroud)

约束:删除花瓣宽度为1.3和1.5的条目.

!iris$Petal.Width %in% c('1.3', '1.5')
Run Code Online (Sandbox Code Playgroud)

尝试:

tapply(iris$Sepal.Length, iris$Species, median[!iris$Petal.Width %in% c('1.3', '1.5')])
Run Code Online (Sandbox Code Playgroud)

结果:错误消息'类型'对象'的对象'不是子集表'.

---

我在这里使用iris数据集的尝试是我自己的数据集的替代演示.我使用自己的数据集尝试了相同的方法并收到了相同的错误消息.我想我的语法有问题.它是什么?

Dav*_*urg 9

尝试

with(iris[!iris$Petal.Width %in% c('1.3', '1.5'),], tapply(Sepal.Length, Species, median))
# setosa versicolor  virginica 
#    5.0        5.8        6.5 
Run Code Online (Sandbox Code Playgroud)

这里的想法是首先对子集数据进行操作.

你的行不起作用,因为FUN参数应该应用于X(Sepal.Length在你的情况下)而不是整个数据集.