r - "if" function for year and day -
in dataset have data 2 years, there 730 entries. problem is, column "day of year" has form 1 365 year 1 , same year 2.
i want values in column "day of year" 1 730.
my "newbie-function":
set$doynew = if (seq(set$year==2002)) {set$doy <- set$doy+365}
doesn´t me further. error gets is:
in if (z < 0) { :> condition has length > 1 , first element used
where problem?
the error telling clause if
should single value.
ie, either true
or false
, not eg c(true, true, false)
.
so if trying accomplish
if (c(true, false, true)) < when true > < different when false >
a basic if
statement not work.
statement @ first value (as warning indicates).
for such situations, statement use ifelse
(as @joran pointed out in answer)
ifelse ( c(true, false, true), < when true > , < different when false > )
or
set$doynew <- ifelse(set$year == 2002,doy + 365,doy)
additionally, if data in data.frame, can subset needed:
mydf$day[mydf$year==2002] <- 365 + mydf$day[mydf$year==2002] # or if using data.table: mydt[year==2002, day := day + 365]
Comments
Post a Comment