df <- data.frame(x=c("a", "b", "c"), start=1:3, end=4:6, val=c(100, 200, 300))
plyr::ddply(df, .(x), function (row) data.frame(n=row$start:row$end, val=row$val/length(row$start:row$end)))
--
You received this message because you are subscribed to the Google Groups "manipulatr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to manipulatr+...@googlegroups.com.
To post to this group, send email to manip...@googlegroups.com.
Visit this group at http://groups.google.com/group/manipulatr.
For more options, visit https://groups.google.com/d/optout.
This is something I’d really like to find a simple way to do! Here is a modification of Alain’s answer:
df %>%
group_by(x) %>%
mutate(val = val/(end + 1
- start)) %>%
do(with(., data_frame(x, n = start:end, val = val)))
If you don’t name the result of data_frame
within do
, then the output won’t be stored in a list.
What do we think about using with()
inside do()
? It certainly cleans up the syntax a bit in cases like this, where every variable was preceded by .$
> df <- data.frame(x=c("a", "b", "c"), start=1:3, end=4:6, val=c(100, 200, 300))
> test1=plyr::ddply(df, .(x), function (row) data.frame(n=row$start:row$end, val=row$val/length(row$start:row$end))) > test2=full_join(mutate(df,com=1),data.frame(n=1:max(df$end),com=1)) %>% filter(n>=start & n<=end) %>% transmute(x,n,val=val/(end-start+1)) Joining by: "com" > identical(test1,test2)
[1] TRUE