【问题标题】:Calculating Function from Variable 1 after Eliminating Level(s) from Variable 2 Using R使用 R 从变量 2 中消除级别后从变量 1 计算函数
【发布时间】:2015-07-22 11:33:28
【问题描述】:

这个问题是询问在以下情况下使用哪个命令:

目标:计算 iris$Sepal.Length 的平均值。

约束:不要包含 iris$Species 'setosa'。

我的工作

data(iris)
levels(iris$Species)

输出:setosa、versicolor 和 virginica

mean(iris$Sepal.Length, which(iris$Species != 'setosa'))

输出:错误消息“尺寸不兼容”

---

这个演示是我自己的数据集的替身,我想计算一个变量(例如 Sepal.Length)的函数,该函数从第二个变量(例如 Species)中排除水平。我相信我的个人数据集和这个替身演示具有可比性。

也许 which() 不是合适的命令。什么是?

【问题讨论】:

    标签: r mean subset


    【解决方案1】:

    你很接近,试试这个

    mean(iris$Sepal.Length[which(iris$Species != 'setosa')])
    

    mean(iris$Sepal.Length[iris$Species != 'setosa'])
    

    mean(iris[iris$Species!= "setosa", "Sepal.Length"])
    

    【讨论】:

    • 太棒了!谢谢!如何在 != 列表中添加第二个物种?
    • 使用not in 逻辑,即mean(iris$Sepal.Length[ !(iris$Species %in% c('setosa', 'virginica')])。逻辑是:取iris$Sepal.Length where NOT iris$Species in c('setosa', 'virginica')
    猜你喜欢
    • 1970-01-01
    • 2016-11-23
    • 2017-08-13
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多