Hi,
I'm trying to subset a large dataframe in order to remove test ids. To do this once would be simple using subset() with the conditions, but I'd like to pass it over multiple times because the same test ids are present at times present among every site.
When I try to insert the conditions into one subset(), it returns 0 obs. in the newly created dataframe. My attempt at the function is in the reprex below to give a better idea.
Any help would be much appreciated, thanks.
#Test patients dataframe
#Test patients for each site to remove from merged data frame
test.patients.s1 <- c("-2147483648", "-2147483647", "-2147483636", "-2147483634")
test.patients.s2 <- c("-2147483647", "-2147483648")
test.patients.s3 <- c("-2147483639", "-2147483645", "-2147483646", "-2147483647")
test.patients.s4 <- c("-2147483648", "-2147483640")
test.patients.s5 <- c("-2147483648", "-2147483647")
test.patients.s6 <- c("-2147483648", "-2147483647", "-2147483646", "-2147483645", "-2147483644")
test.patients.s7 <- c("-2147483648", "-2147483646", "-2147483643", "-2147483647")
test.patients.s8 <- c("-2147483648", "-2147483647")
test.patients.s10 <- c("-2147483647", "-2147483646", "-2147483645", "-2147483639")
test.patients.s11 <- c("-2147483648", "-2147483633", "-2147483647")
test.patients.s15 <- c("-2147483648", "-2147483647", "-2147483646")
#List of test patients
testpatients.list <- list(test.patients.s1,test.patients.s2,test.patients.s3,test.patients.s4,test.patients.s5,test.patients.s6,
test.patients.s7,test.patients.s8,test.patients.s10,test.patients.s11,test.patients.s15)
testpatients.df <- as.data.frame(t(ldply(testpatients.list, rbind)))
colnames(testpatients.df) <- c("s1","s2","s3","s4","s5","s6","s7","s8","s10","s11","s15")
#This works fine when it's for one site, and will return data about that site only with test ids removed
tidyhome.site1 <- tidyhome.import %>%
#Removing tests
subset(
site == "s1" & !patientid %in% c("-2147483648", "-2147483647", "-2147483636", "-2147483634")
#When I run it with more than one sites conditions, it returns 0obs.
tidyhome.site1 <- tidyhome.import %>%
#Removing test patients
subset(
site == "s1" & !patientid %in% c("-2147483648", "-2147483647", "-2147483636", "-2147483634") &
site == "s2" & !patientid %in% c("-2147483647", "-2147483648"))
#My attempt at a function for what I want to carry out
remove.tests <- function(tidyhome.import){
tidyhome.remtests <- tidyhome.import %>%
subset(
#I want s1 to be replaceable with any of the site variables held in tests dataframe
site %in% test$s1 & !patientid %in% test$s1)
}
remove.tests(tidyhome.import)
#This is returned
[1] site patientid date time location fev1 fvc
<0 rows> (or 0-length row.names)