I am having difficulties adding a time trend variable within a ddply function,
Here is the simplified code:
id <- c(1,1,1,2,2,2)
price <- c(1,2,3,2,1,0)
df <- data.frame(id, price)
price_trends <- ddply(df, ~id, summarise,
mean_price = mean(price),
sd_price = sd(price))
price_trends
Now I also want to include a time trend coefficient per id (i.e., price inclining for id 1, declining for id 2), but I am having difficulties to include it in the ddply function from above. I appreciate any help.
You could use:
slope <- function(x) (tail(x,1)-x[1])/length(x)
price_trends <- ddply(df, ~id, summarise,
mean_price = mean(price),
sd_price = sd(price),
trend = price %>% slope() %>% round(0))
> price_trends
id mean_price sd_price trend
1 1 2 1 1
2 2 1 1 -1
Note that you actually get the slope if you remove the last round(0)
. Further, you can define any coefficient of interest via the function. You can also include the function directly inside ddply
like so:
price_trends <- ddply(df, ~id, summarise,
mean_price = mean(price),
sd_price = sd(price),
trend = ((tail(price,1)-price[1])/length(price)) %>% round(0)
)
price_trends