Note that there are some explanatory texts on larger screens.

plurals
  1. POwhich.min within reshape2's dcast()?
    primarykey
    data
    text
    <p>I would like to extract the value of var2 that corresponds to the minimum value of var1 in each building-month combination. Here's my (fake) data set:</p> <pre><code> head(mydata) # building month var1 var2 #1 A 1 -26.96333 376.9633 #2 A 1 165.38759 317.3993 #3 A 1 47.46345 271.0137 #4 A 2 73.47784 294.8171 #5 A 2 107.80130 371.7668 #6 A 2 10.16384 308.7975 </code></pre> <p>Reproducible code:</p> <pre><code>## create fake data set: set.seed(142) mydata1 = data.frame(building = rep(LETTERS[1:5],6),month = sort(rep(1:6,5)),var1=rnorm(30,50,35),var2 = runif(30,200,400)) mydata2 = data.frame(building = rep(LETTERS[1:5],6),month = sort(rep(1:6,5)),var1=rnorm(30,60,35),var2 = runif(30,150,400)) mydata3 = data.frame(building = rep(LETTERS[1:5],6),month = sort(rep(1:6,5)),var1=rnorm(30,40,35),var2 = runif(30,250,400)) mydata = rbind(mydata1,mydata2,mydata3) mydata = mydata[ order(mydata[,"building"], mydata[,"month"]), ] row.names(mydata) = 1:nrow(mydata) ## here is how I pull the minimum value of v1 for each building-month combination: require(reshape2) m1 = melt(mydata, id.var=1:2) d1 = dcast(m1, building ~ month, function(x) min(max(x,0), na.rm=T), subset = .(variable == "var1")) </code></pre> <p>This pulls out the minimum value of var1 for each building-month combo...</p> <pre><code>head(d1) # building 1 2 3 4 5 6 #1 A 165.38759 107.80130 93.32816 73.23279 98.55546 107.58780 #2 B 92.08704 98.94959 57.79610 94.10530 80.86883 99.75983 #3 C 93.38284 100.13564 52.26178 62.37837 91.98839 97.44797 #4 D 82.43440 72.43868 66.83636 105.46263 133.02281 94.56457 #5 E 70.09756 61.44406 30.78444 68.24334 94.35605 61.60610 </code></pre> <p>However, what I want is a data frame set up exactly as d1 that instead shows the value of var2 that corresponds to the minimum value pulled for <code>var1</code> (shown in <code>d1</code> above). My gut tells me it should be a variation on <code>which.min()</code>, but haven't gotten this to work with <code>dcast()</code> or <code>ddply()</code>. Any help is appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload