r - 从行中选择值以用于计算
问题描述
我正在尝试从列具有特定值的行中选择值,以便可以在计算中使用它。我曾尝试使用 dput 但它似乎不起作用 - 至少使用 stackoverflow 上的说明来生成可重现的示例。
total.history <- sum(recommendation$Spent[recommendation$Account=="History"])
$ Account: chr "Art" "Biology" "Botany" "Languages" ...
$ Product: chr "Humanities" "Science" "Science" "Humanities" ...
$ Region : chr "Art" "Biology" "Botany" "Languages" ...
$ Revenue: int 2000 75 1000 1000 200 200 5000 100 100 200
$ Spent : int 1000 30 444 234 100 123 250 56 23 156
$ Left : int 1000 45 556 766 100 77 4750 44 77 44
解决方案
我们可以做的
with(recommendation, sum(Revenue[Account == "Biology"]))
#[1] 75
如果有多个元素,请使用%in%
(或|
与另一个表达式一起添加)
with(recommendation, sum(Revenue[Account %in% c("Biology", "Botany")]))
#[1] 1075
推荐阅读
- javascript - 空格去除器脚本不适用于电子邮件输入
- c# - C# Form - MouseClick 会在失去表单焦点后点击实际控件
- javascript - 如何从下拉列表中选择多个项目并将多个值插入 MYSQL
- javascript - 如何使用canvas js在特定条件下停止绘制实时图
- c++ - C++ 中涉及模运算符优先级和大括号的错误
- c# - 使用析构函数分离事件
- c++ - 按值返回与右值引用
- tensorflow - 如何将重新训练的模型转换为 tflite 格式?
- php - 使用 PHP 解析具有多级的 json 对象的一部分
- socialengine - Engine_Api::_()->getTable 返回的内容