Created
November 5, 2014 16:52
-
-
Save ilapros/28bef8cbd107d264fe09 to your computer and use it in GitHub Desktop.
a question for the masters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### how to dplyr this? | |
df1 <- data.frame(TH2020 = rnorm(200), TH2030 = rnorm(200)) | |
df2 <- data.frame(TH = c(2020,2030), mod1 = runif(2,-2,2), mod2 = runif(2,-2,2), mod3 = runif(2,-2,2), mod4 = runif(2,-2,2)) | |
out <- rbind(apply(df2[df2$TH == 2020,2:5,drop=FALSE], 2, function(x) length(df1[,"TH2020"][df1[,"TH2020"] < x])), | |
apply(df2[df2$TH == 2030,2:5,drop=FALSE], 2, function(x) length(df1[,"TH2030"][df1[,"TH2030"] < x]))) | |
out <- cbind(c(2020,2030), out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bit of prep to make tidy data, using reshape2 and dplyr:
then:
which is all the same numbers, but this is in a tidy format.
you want it back in data frame format? dcast: