这是我的数据集:
set.seed(327)
ID <- seq(1:50)
mou <- sample(c(2000, 2500, 440, 4990, 23000, 450, 3412, 4958, 745, 1000),
50, replace=TRUE)
calls <- sample(c(50, 51, 12, 60, 90, 16, 89, 59, 33, 23, 50, 555),
50, replace=TRUE)
rev <- sample(c(100, 345, 758, 44, 58, 334, 888, 205, 940, 298, 754),
50, replace=TRUE)
dt <- data.frame(mou, calls, rev)
我的动机是找到mou
的平均值,其中呼叫大于34且小于200且rev
大于100且小于400。
我开始使用dplyr来解决这个问题,但我不太确定如何在过滤器函数中正确使用所需的表达式。
dt %>% filter(???) %>% summarize(mean_mou=mean(mou))
请指导如何在过滤器中正确构建此表达式。
答案 0 :(得分:5)
为了完整性:
如果逻辑是 AND ,您只需在逗号后添加多个条件:
df %>%
filter(calls > 34, calls < 200, rev > 100, rev < 400)
如果逻辑 OR ,您必须使用通常的逻辑or
符号:|
df %>%
filter(calls > 34 | rev > 100)
将他们联系在一起工作,但必须注意所做的事情。 例如:
df %>%
filter(calls > 34, calls < 200 | rev > 100, rev < 400)
表示calls > 34 AND (calls < 200 OR rev > 100) AND rev < 400
答案 1 :(得分:1)
您可以将条件放在filter
函数中。你的例子几乎就在那里: - )
########
# Setup
########
set.seed(327) # Setting a seed makes the example reproducible
ID <- seq(1:50)
mou <-
sample(c(2000, 2500, 440, 4990, 23000, 450, 3412, 4958, 745, 1000),
50,
replace = TRUE)
calls <-
sample(c(50, 51, 12, 60, 90, 16, 89, 59, 33, 23, 50, 555), 50, replace = TRUE)
rev <-
sample(c(100, 345, 758, 44, 58, 334, 888, 205, 940, 298, 754), 50, replace = TRUE)
dt <- data.frame(mou, calls, rev)
library(tidyverse)
########
# Here's the direct answer to your question
########
dt %>%
filter(calls > 34 & calls < 200) %>%
filter(rev > 100 & rev < 400) %>% # Using two filters makes things more readable
summarise(mean_mou = mean(mou))
# 3349
答案 2 :(得分:0)
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th>place</th>
<th>stay cost</th>
<th>food cost</th>
<th>flight cost</th>
<th>Date of journey</th>
<th>Total cost</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['stay']; ?></td>
<td><?php echo $row['food'] ;?></td>
<td><?php echo $row['travel'] ;?></td>
<td><?php echo $row['doj'] ;?></td>
<td><?php echo $row['totalamount'] ;?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>