Thanks in advance for any assistance!
I have two separate data frames in R, one with a start and end number, the second with a middle number. Included here is a mock data set illustrating my conundrum.
The data table with two numbers also has a GroupID as seen here.
TwoNum <- structure(list(GroupID = structure(1:10, .Label = c("Clstr001",
"Clstr002", "Clstr007", "Clstr008", "Clstr010", "Clstr011", "Clstr015",
"Clstr016", "Clstr017", "Clstr018"), class = "factor"), StartNum = c(2L,
5L, 23L, 26L, 32L, 41L, 67L, 70L, 73L, 78L), EndNum = c(4L, 7L,
25L, 27L, 40L, 43L, 68L, 72L, 75L, 80L)), .Names = c("GroupID",
"StartNum", "EndNum"), class = "data.frame", row.names = c(NA,
-10L))
head(TwoNum)
Here is the date table with a single number
OneNum <- structure(list(GroupID = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), MiddleNum = c(3L, 5L,
6L, 7L, 24L, 25L, 33L, 34L, 35L, 37L, 42L, 67L, 71L, 73L, 74L,
75L, 78L, 79L, 80L)), .Names = c("GroupID", "MiddleNum"), class = "data.frame",
row.names = c(NA,
-19L))
head(OneNum)
When the MiddleNum is between the StartNum and EndNum I am trying to replace the NA with the corresponding GroupID - i.e. replace the NA with the GroupID row that brackets the middle number.
My real data set is substantially longer and I am thus trying to build this into a for()
loop that checks if the Middle number is between ANY (i.e. all rows) of the Start and End pairs and if yes, adds the corresponding GroupID to the OneNum data frame.
Any suggestions would be appreciated. I am not necessarily looking for someone to create the entire loop (but would not turn that down either...), but new ideas would help greatly.
Thanks.