Hello, I am trying to create a function that will move through each record of a data frame, find the value in the "HUC" column, then randomly select another observation from the dataframe with the same value in "HUC" column, as well as the same value in "Yr" column as the first observation. I want the function to produce a list of the RchID of the first observation, the RchID of the second randomly chosen observation, and several other characteristics of the randomly chosen observation. Below is the code I have written, but it doesn't work.
Thanks for any help. Christy ################test roads=read.csv("streamland23.csv") for (i in 1:nrow (roads)){ Sitetype= roads$Sitetype yr=roads$REACH_Yr initRchid=roads$RchID huc1=roads$HUC sample.df <- function(df, n) df[sample(nrow(df), n), , drop = FALSE] selected=sample.df(roads[roads$HUC == "huc1"& roads$REACH_Yr =="yr" , ], 1) output=cbind (initRchid,selected$RchID,selected$Sitetype,selected$REACH_Yr) } Christy Meredith USDA Forest Service Rocky Mountain Research Station PIBO Monitoring Data Analyst Voice: 435-755-3573 Fax: 435-755-3563 This electronic message contains information generated by the USDA solely for the intended recipients. Any unauthorized interception of this message or the use or disclosure of the information it contains may violate the law and subject the violator to civil or criminal penalties. If you believe you have received this message in error, please notify the sender and delete the email immediately. [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.