Creating multiple subsets in one data.frame file (possibly with ddply)

I have a large data.frame file, and I would like to be able to reduce it using a subset of the quantiles of one of the variables. For instance:

x <- c(1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10)
df <- data.frame(x,rnorm(100))

df2 <- subset(df, df$x == 1)
df3 <- subset(df2, df2[2] > quantile(df2$rnorm.100.,0.8))

What I would like to get is a data.frame which contains all the quantiles for x = 1,2,3 ... 10.

Is there a way to do this with ddply?

0
source share
2 answers

You can try:

ddply(df, .(x), subset, rnorm.100. > quantile(rnorm.100., 0.8))

And disable the topic: you can use df <- data.frame(x,y=rnorm(100))to indicate the column "on the fly."

+3
source

Here, a different approach is used with the underutilized ave () command. (very quickly figure out this way)

Create a new column that contains a quantile calculation at each level x

df$quantByX <-  ave(df$rnorm.100., df$x, FUN = function (x) quantile(x,0.8))

x.

df2 <- unique(df[,c(1,3)])

x x.

+2

Source: https://habr.com/ru/post/1757435/


All Articles