r - Learning to understand plyr, ddply -


i've been attempting understand , how plyr works through trying different variables , functions , seeing results. i'm more looking explanation of how plyr works specific fix answers. i've read documentation newbie brain still not getting it.

some data , names:

mydf<- data.frame(c("a","a","b","b","c","c"),c("e","e","e","e","e","e") ,c(1,2,3,10,20,30), c(5,10,20,20,15,10)) colnames(mydf)<-c("model", "class","length", "speed") mydf 

question 1: summarise versus transform syntax

so if enter: ddply(mydf, .(model), summarise, sum = length+length)

i get:

`model ..1 1 2 2 4 3 b 6 4 b 20 5 c 40 6 c 60 

and if enter: ddply(mydf, .(model), summarise, length+length) same result.

now if use transform: ddply(mydf, .(model), transform, sum = (length+length))

i get:

 model class length speed sum 1 e 1 5 2 2 e 2 10 4 3 b e 3 20 6 4 b e 10 20 20 5 c e 20 15 40 6 c e 30 10 60 

but if state first summarise : ddply(mydf, .(model), transform, (length+length))

 model class length speed 1 e 1 5 2 e 2 10 3 b e 3 20 4 b e 10 20 5 c e 20 15 6 c e 30 10 

so why adding "sum =" make difference?

question 2: why don't these work?

ddply(mydf, .(model), sum, length+length) #error in function (i) : object 'length' not found

ddply(mydf, .(model), length, mydf$length) #error in .fun(piece, ...) : 

2 arguments passed 'length' requires 1

these examples more show somewhere i'm fundamentally not understanding how use plyr.

any anwsers or explanations appreciated.

the syntax is:

ddply(data.frame, variable(s), function, optional arguments) 

where function expected return data.frame. in situation,

  • summarise function transparently create new data.frame, results of expression provide further arguments (...)

  • transform, base r function, transform data.frames (first split variable(s)), adding new columns according expression(s) provide further arguments. these need named, that's way transform works.

if use other functions subset, transform, mutate, with, within, or summarise, you'll need make sure return data.frame (length , sum don't), or @ least vector of appropriate length output.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -