Reputation: 961
I'm trying to select ids(patients) from each of 4 regions within the following 3 age groups:
2 - <4 yrs, 4 - <6 yrs, 6 - <8 yrs. The data is the longData
given in the code below. I need to select only half of the ids that fit these age criteria and merge them in a separate data frame.
I have written a function (subsetFunc
) to do this individually for each region but I was hoping I could write a code
using a for loop to apply a similar function to get this information from all four regions as a separate data frame (rather than applying the code 4 times for each region). I could simply apply the function 12 times and then rbind each output but this seems repetitive.
In my real dataset, I have several regions and so it would be too tedious to apply this function say 60 times. Besides, this is
the whole point of writing functions-to automate the repetitive things.
library(dplyr)
roundUp <- function(x) trunc(x+0.5)
set.seed(2016)
longData <- data.frame(patient=rep(paste(letters[1:20], sep = "_", 1:20), each=5),
age=rep(runif(20, 1, 10), each = 5),var=runif(100, 50, 1000),
time=rep(1:5, 20), region = rep(c("EUROPE", "AFRICA", "ASIA",
"OCEANIA"), c(25, 25, 25, 25)))
subsetFunc <- function(df, group_in, seedNumb, a, b){
data <- filter(df, region == group_in, age >= a, age < b)
set.seed(seedNumb)
n <- roundUp(
data%>% filter(!duplicated(data["patient"])) %>% nrow()/2)
ids <- sample_n(unique(data[, c("patient", "region")]), n)$patient
fd <- data[data$patient %in% ids, ]
return(fd)
}
# this gives half of the children in Europe between ages 2 and < 4 yrs
subsetFunc(longData, "EUROPE", 1, 2, 4)
# Desired Output (tedious method for doing this)
# 2-<4
d_1 <- subsetFunc(longData, "EUROPE", 1, 2, 4)
d_2 <- subsetFunc(longData, "ASIA", 1, 2, 4)
d_3 <- subsetFunc(longData, "OCEANIA", 1, 2, 4)
d_4 <- subsetFunc(longData, "AFRICA", 1, 2, 4)
# 4- < 6
d_5 <- subsetFunc(longData, "EUROPE", 1, 4, 6)
d_6 <- subsetFunc(longData, "ASIA", 1, 4, 6)
d_7 <- subsetFunc(longData, "OCEANIA", 1, 4, 6)
d_8 <- subsetFunc(longData, "AFRICA", 1, 4, 6)
# 6- 8
d_9 <- subsetFunc(longData, "EUROPE", 1, 6, 8)
d_10 <- subsetFunc(longData, "ASIA", 1, 6, 8)
d_11 <- subsetFunc(longData, "OCEANIA", 1, 6, 8)
d_12 <- subsetFunc(longData, "AFRICA", 1, 6, 8)
finalLongData <- rbind(d_1, d_2, d_3, d_4, d_5, d_6, d_7, d_8, d_9,
d_10, d_11, d_12)
I started by writing something along the lines of:
for (i in levels(longData$cohort)){
dFrame <- subsetFunc(longData, i, 1, a = 0, b = 2)
return(dFrame)
}
I'm a little stuck at this stage, any assistance would therefore be highly appreciated.
Upvotes: 0
Views: 162
Reputation: 10761
Alright, I came back to this post, and I think I might have cracked it. First, I needed to redefine your subsetFunc
function:
subsetFunc <- function(df, seedNumb, a, b){
data <- filter(df, age >= a, age < b)
set.seed(seedNumb)
n <- ceiling(
data%>% filter(!duplicated(data["patient"])) %>% nrow()/2)
ids <- sample_n(unique(data[, c("patient", "region")]), n)$patient
fd <- data[data$patient %in% ids, ]
return(fd)
}
Then, I was able to put all the results together without having to use as many lines:
LongDataFinal <-
do.call('rbind',
as.list(sapply(c(2,4,6), FUN = function(y)
by(longData, longData$region,
FUN = function(x) subsetFunc(df = x, seedNumb = 1, a = y, b = y+2)))))
Upvotes: 2