[R] Repeat elements of matrix based on vector counts

Rui Barradas ruipbarradas at sapo.pt
Thu Dec 13 17:17:08 CET 2012


Hello,

Something like this?

rep(df2$load, table(df1$plot.id))


Hope this helps,

Rui Barradas
Em 13-12-2012 14:15, Sarah Haas escreveu:
> I have two dataframes (df) that share a column header ("plot.id").  In the
> 1st df, "plot.id" records are repeated a variable number of times based on
> the number of trees monitored within each plot. The 2nd df only has a
> single record for each "plot.id", and contains a variable named "load" that
> is collected at the plot-level and is only listed once per plot record.
>
> *OBJECTIVE:*  I need to repeat the "load" values from the 2nd df based on
> how many times "plot.id" is repeated in the 1st df (all plots are repeated
> a different number of times). My example dfs are below:
>
> ************************************************************************************************
>       df1 <- data.frame(plot.id = rep(c("plot1", "plot2", "plot3"),
> c(3,2,5)),
>                   tree.tag = c(111,112,113,222,223,333,334,335,336,337))
>
>       df2 <- data.frame(plot.id = c("plot1", "plot2", "plot3"), load=c(17,
> 6, 24))
> ************************************************************************************************
>
> I have gotten close to solving this, but alas I'm on day 2 of
> problem-shooting and can't get it! Thanks for any help you might provide.
>
> --Sarah
>
> 	[[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.




More information about the R-help mailing list