I am having an issue filtering a dataset based on values referenced from another dataset.
I have two datasets. The first dataset, comparison_dt, contains all the comparisons I should make in the form of rows with location1, location2. The second dataset, rain_values_dt, contains the values collected from those locations at different times. My goal is to, for each row in comparison_dt, filter out rows of rain_values_dt collected from location1, filter out rows of rain_values_dt collected from location2, inner join those rows, run a paired t-test, and return the test statistic to a column appended to comparison_dt.
The issue I'm having is that I'm unable to filter rows of rain_values_dt based on the location name referenced from comparison_dt. Asking to filter based on the name stored in row one of the comparison table returns all rows of rain_values_dt. Asking to filter based on the name stored in higher row numbers returns nothing. I would like to only receive rows from the site I am referencing in the filter.
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'])
This is a simplification of a larger dataset where not all site comparisons are valid, trials must be matched based on a number of different conditions, and there are an uneven number of trials per site.
This was previously working as expected. I restarted the R session and it no longer works as expected.
I have tried changing the location names in either dataset to character or function type based on the idea that I might have imported my datasets differently. I tried referencing the location column as a vector or in quotes. I tried unloading and reloading dplyr and checking whether R was using the base stats version of filter or the dplyr version. This seems like a simple problem, but I searched this site and the filter() documentation and haven't found an answer for why the function might be behaving this way.