Skip to content

Instantly share code, notes, and snippets.

@chainsawriot
Last active January 13, 2016 09:15
Show Gist options
  • Save chainsawriot/4fb057ffb8539eb026c5 to your computer and use it in GitHub Desktop.
Save chainsawriot/4fb057ffb8539eb026c5 to your computer and use it in GitHub Desktop.
gendf <- function() {
a <- data.frame(x = rnorm(200, 1, 1), y = rnorm(200, 3, 1))
b <- data.frame(x = rnorm(200, 9, 1), y = rnorm(200, 10, 1))
z <- c(rep(1, 200), rep(2, 200))
cbind(rbind(a, b), z)[sample(1:400),]
}
df1 <- gendf()
df2 <- gendf()
df3 <- gendf()
## Suppose you have three data.frames, df1, df2, df3
## task: remove rows with z == 1 and calculate the mean of x * y
df1no1 <- df1[df1$z!=1,]
mean(df1no1$x * df1no1$y)
## Copy and paste?
df2no1 <- df2[df2$z!=1,]
mean(df2no1$x * df2no1$y)
## and again?
df3no1 <- df3[df3$z!=1,]
mean(df3no1$x * df3no1$y)
## Don't repeat yourself
removez1mean <- function(df) {
dfno1 <- df[df$z != 1,]
mean(dfno1$x * dfno1$y)
}
removez1mean(df1)
removez1mean(df2)
removez1mean(df3)
## actual this itself is repeat also. Further remove the repetition
sapply(list(df1, df2, df3), removez1mean)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment