【发布时间】:2021-09-04 15:33:32
【问题描述】:
我在根据从另一个数据集引用的值过滤数据集时遇到问题。
我有两个数据集。第一个数据集 compare_dt 包含我应该以行的形式与 location1、location2 进行的所有比较。第二个数据集rain_values_dt 包含在不同时间从这些位置收集的值。我的目标是,对于 compare_dt 中的每一行,过滤掉从 location1 收集的 rain_values_dt 行,过滤掉从 location2 收集的 rain_values_dt 行,内部连接这些行,运行配对 t 检验,并将测试统计信息返回到附加的列到 compare_dt。
我遇到的问题是我无法根据从 compare_dt 引用的位置名称过滤 rain_values_dt 行。要求根据存储在比较表的第一行中的名称进行过滤,将返回 rain_values_dt 的所有行。要求根据存储在较高行号中的名称进行过滤不会返回任何内容。我只想接收来自我在过滤器中引用的网站的行。
library(data.table)
library(dplyr)
comparison_dt <- data.table(
location1= c('austin_tx','austin_tx','austin_tx','boston_ma','boston_ma','boston_ma','chicago_il','chicago_il','chicago_il'),
location2= c('austin_tx','boston_ma','chicago_il','austin_tx','boston_ma','chicago_il','austin_tx','boston_ma','chicago_il'),
test_statistic= c()
)
rain_values_dt <- data.table(
location=c('austin_tx','austin_tx','austin_tx','boston_ma','boston_ma','boston_ma','chicago_il','chicago_il','chicago_il'),
month=c('march','april','may','march','april','may','march','april','may'),
rainfall=c(1:9)
)
row_n=1
#my intended result, works as expected v
dplyr::filter(rain_values_dt, location == 'austin_tx')
#is pulling the correct name from the comparison table to filter on
comparison_dt[row_n,'location1']
#these are equivalent to each other, so I should be able to substitute, right?
'austin_tx' == comparison_dt[row_n,'location1']
#does not work, returns all values instead of filtering
dplyr::filter(rain_values_dt, location == comparison_dt[row_n,'location1'])
这是对较大数据集的简化,其中并非所有站点比较都有效,试验必须根据许多不同的条件进行匹配,并且每个站点的试验数量不均匀。
这之前按预期工作。我重新启动了 R 会话,但它不再按预期工作。
基于我可能以不同方式导入数据集的想法,我尝试将任一数据集中的位置名称更改为字符或函数类型。我尝试将位置列引用为向量或引号。我尝试卸载和重新加载 dplyr 并检查 R 是使用过滤器的基本统计版本还是 dplyr 版本。这似乎是一个简单的问题,但我搜索了这个站点和 filter() 文档,并没有找到为什么该函数可能会以这种方式运行的答案。
【问题讨论】: