[R] Repeat elements of matrix based on vector counts
arun
smartpink111 at yahoo.com
Thu Dec 13 17:20:16 CET 2012
Hi,
Try ?merge() or ?join() from library(plyr)
res<-merge(df1,df2,by="plot.id")
head(res,6)
# plot.id tree.tag load
#1 plot1 111 17
#2 plot1 112 17
#3 plot1 113 17
#4 plot2 222 6
#5 plot2 223 6
#6 plot3 333 24
A.K>
----- Original Message -----
From: Sarah Haas <haaszoology at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Thursday, December 13, 2012 9:15 AM
Subject: [R] Repeat elements of matrix based on vector counts
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