0

I´m working in R:

I do have two data.frames with a column for starting angles and one for ending angles of a angle interval, which is measured counter clockwise.

data.frame baumtabelle (shows column 3 to 5) data.frame baumtabelle

data.frame bereiche data.frame bereiche

The data.frame baumtabelle still has overlapping angle intervals, which I removed in the data.frame bereiche by saving all angle values in a vector and sorting them by value and readressing into a data.frame again. But now there are new intervals in bereiche without any "original" interval from the data.frame baumtabelle and I need to know which intervals from baumtabelle are overlapping with the new interval in bereiche. Those numbers of the intervals (in data.frame baumtabelle column one Nr_Baum) should be saved in data.frame bareiche column 3 - which in the picture clearly is not correct yet.

I´m not sure, if my conditions for overlapping are wrong, or if there is something wrong with adressing the numbers of the baumtabelle intervals to the bereiche intervals.

I´m adding my code here:

baumtabelle <- baumtabelle_neu <- dput(baumtabelle_neu)
structure(
list(
h = c(10, 15, 7, 20),
KM = c(7, 10, 6, 15),
Nr_Baum = c(1,
2, 3, 4),
alpha01 = c(0, 31.3669777746336, 22.6198649480404,
282.972239886185),
alpha02 = c(0, 58.6330222253664, 36.869897645844,
347.027760113815),
e = c(0, 8.48528137423857, 8.06225774829855,
5.65685424949238)
   ),
class = "data.frame",
row.names = c(NA, 4L)
 )

bereiche <- dput(bereiche)
structure(
list(
alpha01_neu = c(
22.6198649480404,
31.3669777746336,
36.869897645844,
58.6330222253664,
282.972239886185
     ),
alpha02_neu = c(
31.3669777746336,
36.869897645844,
58.6330222253664,
282.972239886185,
347.027760113815
     ),
Nr_Baum = c(NA, NA, 2, NA, NA),
relevant_Baum = c(4, 4, 2,
4, 4)
   ),
class = "data.frame",
row.names = c(NA,-5L)
 )

Define function to check for overlap
check_overlap <- function(start1, end1, start2, end2) {
Check if ranges overlap
return (((start1 < end2) & (end1 > start2)) | ((start1 == start2) & (end1 == end2)))
 }

for (i in 1:nrow(bereiche)) {
Initialize an empty vector to store the matching baumtabelle numbers
match_vec <- vector("character")

for (j in 1:nrow(baumtabelle)) {
check if any of the values in column 4 or 5 of baumtabelle_neu are between the values in column 2 and 3 of bereiche

if (check_overlap(baumtabelle[j,4], baumtabelle[j,5], bereiche[i,1], bereiche[i,2])){

if a match is found, append the number of baumtabelle column 3 to the match_vec
match_vec <- append(match_vec, as.character(baumtabelle[j, 3]))



Combine all the numbers in match_vec as a single string separated by commas
match_str <- paste(match_vec, collapse = " ")

Write the combined string to column 3 of bereiche
bereiche[i, 3] <- match_str
   }
 }

I think I could solve the problem this way:

for (i in 1:nrow(bereiche)) {
  overlapping_intervals <- c()
  for (j in 1:nrow(baumtabelle)) {
    if (baumtabelle[j,4] < bereiche[i,1]  && baumtabelle[j,5] > bereiche[i,1] ||
        baumtabelle[j,4] > bereiche[i,1]  && baumtabelle[j,4] < bereiche[i,2] ||
        baumtabelle[j,4] == bereiche[i,1] && baumtabelle[j,5] == bereiche[i,2] ||
        baumtabelle[j,4] == bereiche[i,1] && baumtabelle[j,5] > bereiche[i,2] ||
        baumtabelle_neu[j,4] < bereiche[i,1] && baumtabelle_neu[j,5] == bereiche[i,2]
        ) {
      overlapping_intervals <- append(overlapping_intervals, baumtabelle[j,3])
    }
  }
  bereiche[i,3] <- paste(overlapping_intervals, collapse = ",")
}
bereiche <- subset(bereiche, bereiche[,3] >=1)```
sonia
  • 11
  • 3

0 Answers0