How to get 2 outputs from a function in R ('list' is not working)? -
my data looks following:
> dput(head(updata[,c(1,4,5,6)],10)) structure(list(vehicle = c(2l, 5l, 8l, 9l, 10l, 12l, 13l, 14l, 18l, 20l), class = c(2l, 2l, 2l, 2l, 2l, 2l, 2l, 3l, 2l, 2l), velocity = c(42.48, 39.81, 40.63, 47.11, 41.2, 38.92, 35.38, 38.62, 38.94, 43.24), lane = c(2l, 4l, 4l, 3l, 1l, 1l, 2l, 5l, 5l, 3l)), .names = c("vehicle", "class", "velocity", "lane"), row.names = c(32l, 707l, 1392l, 1772l, 2125l, 2501l, 2896l, 3262l, 3726l, 3941l), class = "data.frame")
i want find cumulative distribution of 'velocity' 'class' , 'lane'. used following 2 functions , ddply
:
speedu <- function(x){ hi <- hist(x) speedmph=round(hi$breaks*0.68,1)} cumdistu <- function(x){ hi <- hist(x) prob=c(0, round(cumsum(hi$counts)/sum(hi$counts),digits=2))} spdistu <- ddply(updata, c('lane', 'class'), summarise,speed=speedu(velocity),cprob=cumdist(velocity))
here, don't need 2 functions. 1 function can find both speed breaks , cumulative probabilities problem function
not give 2 outputs, gives last variable in output. tried list(speedmph, prob)
in function output contained speeds in 1 row. using above configuration, can following output, wondering if there easy way achieve same:
> dput(spdistu) structure(list(lane = c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 4l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l, 5l), class = c(1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l, 3l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l, 3l), speed = c(6.8, 13.6, 20.4, 27.2, 34, 40.8, 0, 3.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2, 30.6, 34, 37.4, 27.2, 34, 13.6, 20.4, 27.2, 34, 40.8, 3.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2, 30.6, 34, 37.4, 20.4, 21.8, 23.1, 24.5, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2, 30.6, 34, 17.7, 19, 20.4, 21.8, 23.1, 24.5, 25.8, 27.2, 28.6, 23.8, 24.5, 25.2, 25.8, 26.5, 27.2, 0, 3.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2, 30.6, 34, 37.4, 13.6, 17, 20.4, 23.8, 27.2, 30.6, 23.1, 24.5, 25.8, 27.2, 28.6, 29.9, 0, 3.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2, 30.6, 34, 37.4, 40.8, 13.6, 17, 20.4, 23.8, 27.2, 30.6 ), cprob = c(0, 0.14, 0.29, 0.71, 0.86, 1, 0, 0.01, 0.04, 0.08, 0.14, 0.22, 0.32, 0.5, 0.74, 0.95, 0.99, 1, 0, 1, 0, 0.17, 0.67, 0.83, 1, 0, 0, 0.03, 0.07, 0.16, 0.3, 0.51, 0.8, 0.99, 1, 1, 0, 0.5, 0.5, 1, 0, 0.03, 0.05, 0.11, 0.21, 0.49, 0.85, 0.99, 1, 0, 0.07, 0.07, 0.27, 0.47, 0.67, 0.73, 0.93, 1, 0, 0.25, 0.25, 0.25, 0.75, 1, 0, 0, 0.01, 0.01, 0.06, 0.1, 0.17, 0.4, 0.75, 0.96, 1, 1, 0, 0.09, 0.09, 0.52, 0.91, 1, 0, 0.29, 0.43, 0.57, 0.57, 1, 0, 0, 0.01, 0.01, 0.02, 0.04, 0.06, 0.26, 0.66, 0.95, 1, 1, 1, 0, 0.07, 0.21, 0.43, 0.93, 1)), .names = c("lane", "class", "speed", "cprob"), row.names = c(na, -107l), class = "data.frame")
are looking this
f <- function(x) { hi <- hist(x) speedmph=round(hi$breaks*0.68,1) prob=c(0, round(cumsum(hi$counts)/sum(hi$counts),digits=2)) cbind(speedmph, prob) } spdistu <- ddply(updata, c('lane', 'class'), summarise, speed=f(velocity))
Comments
Post a Comment