I have 2 data table as well as using the "iptools" package to check ip_in_range. But I cant simply get the ifelse to work.
For example - Data Table 1
source_ip
1 10.0.0.200
2 10.0.1.200
3 15.0.0.3
4 NA
5 NA
Data Table 2
IP_Address
1 10.0.0.0/24
2 10.0.1.0/30
3 10.0.0.0/28
4 10.0.1.0/24
5 15.0.0.0/24
So what I will like to do is to loop through data table 2 using data table 1 address to see whether does it fit in or not.
So I did the following code to check whether does source ip fits in the range of the IP address in the Data Table 2
ipData$Result <-ifelse(ip_in_range(sourceData$source_ip, ipData$IP_Address), 1, 0)
This made my Data Table 2 into the following
IP_Address Result
1 10.0.0.0/24 1
2 10.0.1.0/30 0
3 10.0.0.0/28 0
4 10.0.1.0/24 0
5 15.0.0.0/24 0
From what I can see, the code actually works.. but only for #1 in source_ip. How am I going to make #2 and #3 works as well in the ifelse statement? Isn't the ifelse statement supposedly read in the vector and check each element of the vector one by one so you don’t need indices or a loop? Been stucked at this problem for a really long time already so any help will be greatly appreciated!
Thank you, a R noob.
Aucun commentaire:
Enregistrer un commentaire