[R] function completing properly

Jim Lemon jim at bitwrit.com.au
Thu Jul 10 13:50:19 CEST 2014


On Wed, 9 Jul 2014 04:47:39 PM Janet Choate wrote:
> Hi R community,
> i created a function (mkdate) as follows:
> 
> mkdate = function(x) {
> x$date = as.Date(paste(x$year, x$month, x$day, sep="-"))
> x$wy = ifelse(x$month >=10, x$year+1, x$year)
> x$yd = as.integer(format(as.Date(x$date), format="%j"))
> x$wyd = cal.wyd(x)
> x
> }
> 
> the function results in adding the new columns date, wy, yd, and 
wyd to the
> table i apply it to.
> this has always worked in R version 2.14.2.
> however, in R version 3.1.0 - instead of my mkdate function adding 
those
> columns to my existing table, it just overwrites my table and leaves 
me
> with just a list of the last variable created by my mkdate function. so 
i
> end up with just a list of numbers representing wyd, and lose all the 
data
> in my original table.
> 
> does anyone know what would now be causing this to occur, and 
what i need
> to do to make my function work properly again?
> 
> thank you for any assistance,
> Janet
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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.
Hi Janet,
It looks to me as though x should be at least a three column list 
containing a year, month and day in numeric format. You then add 
four other fields to it and return the resulting (at least) seven column 
list. What you may have been doing was to pass your entire data 
frame (?) to the function with one or more incomplete rows, 
whereupon the function would calculate the four fields for all the rows 
and fill in the incomplete rows. This is not a very efficient way to do this 
and you should probably consider just passing the new values and 
appending the object returned to your data frame.

Jim



More information about the R-help mailing list