Print

Print


Hello Errol,


I am not an expert in R, but an easy fix for your problem could be just adding one more ifelse function embedded in your p1input and p2input arguments.


This code work for me, see if it solve your problem


p1input = noquote(ifelse(test = p1count >=10,
                                              yes = p1count,
                                              no = ifelse(test = p1count>0,
                                                                  yes = paste0("0",p1count),
                                                                  no  = paste0("-",p1count,sep=""))))
p2input = noquote(ifelse(test = p2count >=10,
                                              yes = p2count,
                                              no = ifelse(test = p2count>0,
                                                                  yes = paste0("0",p2count),
                                                                  no  = paste0("-",p2count,sep=""))))



Best regards,


Eduardo S. Bongiolo

Masters student

School of Forest Resources and Conservation

University of Florida

________________________________
From: UF R Users List <[log in to unmask]> on behalf of Ronje,Errol I <[log in to unmask]>
Sent: Friday, November 16, 2018 5:22:41 PM
To: [log in to unmask]
Subject: code advice


Hi,


I am formatting encounter histories for mark-recapture and have a small problem.  The desired encounter history format is combines p1 and p2 into a 4 digit string and looks like below.  For example, -007 is the combined encounter history of animal#6030, for sessions p1= -0 and p2=07.  I am formatting them like this:


p1input<-noquote(ifelse(test = p1count >0,yes=paste0("0",p1count), no = paste0("-",p1count,sep="")))
p2input<-noquote(ifelse(test = p2count >0,yes=paste0("0",p2count), no = paste0("-",p2count,sep="")))

The code above tells R to put a zero in front of p1 or p2 if the animal was observed, and a negative sign in front of the count if the count is "0" .  it works great except for when the animal was seen >=10 times, then the zero it adds invalidates the input, as you can see for animal 7000 below.


I've attached testdata and the code in its entirety below. Is there a way to change the code above to do what I need?


/* 6030 */ -007 1;
/* 6031 */ -006 1;
/* 6033 */ -005 1;
/* 6036 */ -001 1;
/* 7000 */ 01010 1;
/* 7001 */ 0209 1;
/* 7002 */ 0404 1;
/* 7004 */ 0105 1;
/* 7005 */ 0504 1;
/* 7006 */ 0204 1;
/* 7007 */ 0504 1;
/* 7008 */ 02-0 1;

#####read in data
data=read.csv("testdata.csv", header=TRUE, na.strings = FALSE)
data<-data.frame(data)
####sum sightings each secondary occassion
p1count<-rowSums(data[2:4])
p2count<-rowSums(data[5:7])
###calculate min number marked seen
p1minmark<-ifelse(test = p1count > 0, yes=1, no=0)
p2minmark<-ifelse(test = p2count > 0, yes=1, no=0)
sum(p1minmark)
sum(p2minmark)
###convert counts to mark-resight input
p1input<-noquote(ifelse(test = p1count >0,yes=paste0("0",p1count), no = paste0("-",p1count,sep="")))
p2input<-noquote(ifelse(test = p2count >0,yes=paste0("0",p2count), no = paste0("-",p2count,sep="")))
#######initialize mark-resight poisson.inp file
poisson<-data.frame(data)
#####remove unwanted columns
poisson[,2:7]<-NULL
poisson$p1<-p1input
poisson$p2<-p2input
#####collapse input columns
poisson<-unite(poisson, "input", c("p1","p2"),sep="")
#######tack on frequency for individual
poisson$ind<-"1;";
#######add tags
poisson$CatalogID<-paste("/*",poisson$CatalogID,"*/", sep=" ")
#######collapse CatalogID,sightings,frequency
poisson<-unite(poisson, "Input", c(1:3), sep = " ")

myfile <- "newdata.csv"

for (i in 1:nrow(poisson)) {
  cat(poisson[i,], "\n", file=myfile, append = TRUE)
  print(i)
  print(poisson[i,])
}


This list strives to be beginner friendly. However, we still ask that you PLEASE do read the posting guide http://www.R-project.org/posting-guide.html<https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwMFAw&c=pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=ZmXlGbWIts2KQ6jl_e7C1lL2_zmWFV89rfSbHwKrRJo&m=mj3GvtFIlmJbPPHtqQmtE2yNXxFUpIhXiarSaLnRduM&s=n6zxBRqZzsd5S5DQ9rXiy4c00F1qX65ncZ2OFOOrlgo&e=> and provide commented, minimal, self-contained, reproducible code.

This list strives to be beginner friendly.  However, we still ask that you
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.