Bryan Danson
2011-Mar-23 22:44 UTC
[R] removing data randomly based on previous observations
Hello, I have a dataset which contains multiple trap pulls over the course of a fishing season. Each trap was baited and then returned to the water to be pulled again. I am trying to remove that bait from the catch observed on the next pull (the bait is the same as the catch, lobster). The traps have been baited differently throughout the season. I would like to randomly remove two lobsters from the catch if the bait in the previous trap was two, or 3, or 4, etc. My idea was to create a new variable with the bait from the previous observation. To do this, I started by creating a column that a text variable in one column (e.g., "1 short") equals 1 in the new column. This is simple enough with an ifelse function. However, that is not my only bait left variable (e.g., I have "2 short," "3 short," etc.). If I try to include any other "yes" results in the ifelse function, it has not worked how I have tried it so far. For example,>catch$s_bait <- ifelse(bait_left=="1 short"|bait_left=="2 short", c(1,2),0)this returns repeating 1 and 2s when the bait_left variable=="2 short".>catch$s_bait <- ifelse(c(bait_left=="4 short"|bait_left=="1 short 1 legal"|+ bait_left=="5 short"|bait_left=="2 short"|bait_left=="1 short"| + bait_left=="3 short"|bait_left=="2 short 1 legal"), 4|1|5|2|1|3|2,0) this returns a 1 for every variable that matches the bait_left variables defined in the function. I have also tried the previous function without the c() around the test variable and with it around the yes variable, and neither of those have worked either. I can do the whole thing separately and then put it all together, however I am trying to not do that. Also, another option is the ability to just use ifelse functions repetitively, however, I am not sure what to use in the "no" argument so that everytime I run the function, it does not replace what I have already attempted to convert. If I use: catch$s_bait <- ifelse(bait_left=="1 short",1,)&ifelse(bait_left=="2 short",2,) it results in the following error: Error in ifelse(bait_left == "1 short", 1, ) : argument "no" is missing, with no default Once I figure out how to create the column of bait, I need to apply that to the next observation (same trap number). I'm not sure how to do that, or how to randomly select 1 or more lobsters from the catch of that trap on that observation to remove it as it may have been bait (I do not know exactly which were baits from the previous observation). the first few lines of the database for an example are:> catch[1:20,c(4:10,25:27)]date bait_left species sex size N s_bait l_bait 1 1/4/2011 4 short Panulirus argus Male 77 1 0 NA 2 11/18/2010 1 short 1 legal Panulirus argus Male 75 1 0 NA 3 9/7/2010 2 legal Scrawled cowfish NA 1 0 NA 4 1/4/2011 4 short White grunt 160 1 0 NA 5 1/4/2011 4 short Panulirus argus Female 75 1 0 NA 6 1/4/2011 4 short Panulirus argus Male 56 1 0 NA 7 1/4/2011 4 short Panulirus argus Male 76 1 0 NA 8 10/18/2010 1 short 1 legal Panulirus argus Female 72 1 0 NA 9 1/4/2011 4 short Panulirus argus Female 70 1 0 NA 10 11/18/2010 1 short 1 legal Panulirus argus Male 82 1 0 NA 11 11/18/2010 1 short 1 legal Stone crab NA 1 0 NA 12 1/4/2011 4 short Stone crab NA 1 0 NA 13 11/1/2010 1 short 1 legal Portunus gibbesii 65 1 0 NA 14 11/18/2010 1 short 1 legal Panulirus argus Female 72 1 0 NA 15 10/18/2010 1 short 1 legal Panulirus argus Male 82 1 0 NA 16 11/1/2010 1 short 1 legal Panulirus argus Male 79 1 0 NA 17 11/1/2010 1 short 1 legal Panulirus argus Male 89 1 0 NA 18 12/1/2010 1 short 1 legal Portunus gibbesii 120 1 0 NA 19 1/4/2011 4 short Stone crab NA 1 0 NA 20 12/1/2010 1 short 1 legal White grunt 140 1 0 NA Number 1 should have a s_bait quantity of 4, and number 2 should have a s_bait of 1 and a l_bait of 1. Any answers or ideas or places to look for answers are greatly appreciated. Thank you Bryan ------------------------------------------------- Bryan Danson, M.Sc. Biological Scientist I FWC - Marathon, FL [[alternative HTML version deleted]]