I'm trying to use this basic structure to address a data reshaping problem;
for(i in 1:5) { # Head of for-loop
if(i < 4) { # First if-condition
if(i %in% seq(2, 10, 2)) { # Second if-condition
print(i) # Some output
}
}
}
Disclaimer, although I'm discussing "dates" in this code they are a Julian date system, so they're not in POSIXct format and behave as integers.
I want to use a list of values ("dates") to find cases in a list "bydates" that meet 2 conditions, and write them to a new df. "bydates" has 2275 observations of 4 variables; NatalName, JStart, JEnd, FAM (format chr, num, num, chr).
for each value in "dates" (i) I want to assess if JStart < i, and if JEnd > i, and if both conditions are met to write to the lists df in the format i, NatalNAme, FAM.
This is one of my attempts, that I keep coming back to (I also tried functions, and ifelse and if_else, without success).
lists <- c() # create a blank variable to store the result
for(i in dates)
{if(bydates$Jstart <= i) {
if(JEnd > i) {
lists <- as.df(i, bydates$FAM, bydates$NatalName)
}
}
}
This returns "Error in if (bydates$Jstart <= i) { : the condition has length > 1"
I think this means more than one value in my "bydates" df meets the condition, which is correct, but then does that mean I should be looping on "bydates" instead? I've spent more than a week researching this and I remain stuck. I'm also confused why I don't get the commonly reported "the condition has length >1 and only the first element will be used" error.
Any help very much appreciated.
EDIT: as requested by @Stefan, a snippet of the data using dput
> dput(dates[1:4])
c(744, 864, 984, 1224)
> dput(head(bydates))
structure(list(NatalName = c("AAN12", "AAN18", "AAN20", "ABI96",
"ABR12", "ABR17"), Jstart = c(1113, 1178, 1203, 914, 1105, 1175
), JEnd = c(1158, 1180, -23053, 915, -23053, -23053), FAM = c("AA",
"AA", "AA", "AA", "AA", "AA")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))