简体   繁体   English

R语句中的复合条件

[英]compounding conditions in a statement in R

My Data set contains 6 columns of which Ozone, Temp and Solar.R are a part. 我的数据集包含6列,其中Ozone,Temp和Solar.R是其中的一部分。 I need to find out the mean of Solar.R values based on condition $Ozone >31 and $Temp>90 I am executing the below code but it is returning NaN 我需要找出基于条件$ Ozone> 31和$ Temp> 90的Solar.R值的平均值,我正在执行以下代码,但返回的是NaN

data<-read.csv("hw1_data.csv",header=TRUE)
mean(na.omit(data[data$Ozone>31 && data$Temp>90,]$Solar.R)) 

Pls let me where I am going wrong 请让我哪里出问题了

You have one to many ampersands in there. 您那里有一对多的&符。

Try: 尝试:

mean(na.omit(data[data$Ozone > 31 & data$Temp > 90, ]$Solar.R))

From the help page at ?"&&" : 从帮助页面上的?"&&"

& and && indicate logical AND and | &&&表示逻辑AND| and || || indicate logical OR . 表示逻辑OR The shorter form performs elementwise comparisons in much the same way as arithmetic operators. 较短的形式以与算术运算符几乎相同的方式执行元素比较。 The longer form evaluates left to right examining only the first element of each vector. 较长的形式从左到右求值,仅检查每个向量的第一个元素。 Evaluation proceeds only until the result is determined. 评估仅进行到确定结果为止。 The longer form is appropriate for programming control-flow and typically preferred in if clauses. 较长的形式适用于编程控制流,通常在if子句中首选。


By the way, this pretty much sounds like the "airquality" dataset that is already available in R: 顺便说一下,这听起来很像R中已经可用的“空气质量”数据集:

mean(na.omit(airquality[airquality$Ozone > 31 & 
                          airquality$Temp > 90, ]$Solar.R)) 
# [1] 212.8

Oh, and doesn't using the space-bar make code much easier on the eyes? 哦,难道不使用空格键使代码在眼中变得容易得多吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM