tags:

views:

68

answers:

2

I have a table with >2M rows. I am only interested in percentiles of one variable vs. percentiles of number of observations (e.g. Lorentz curve).

  • How do I create a smaller dataframe that contains e.g. observations number 1,101,201,301,...,last , or observations that corresponds to e.g. the 1,2,3,...,100 percentile of total number of observations?

  • Is there a quick way to get the lorenz curve of (index, variable) with axes on a percentage basis? Right now I was thinking of adding variables for percentiles of index and variables and then plot them against each other.

Thanks,

Roberto

+1  A: 

As for the first question, I would use the quantile function, to get a subset of the dataframe according to the 1,2,3,...,100 percentile of the total number of (say) first column's observations (assuming integer values in column 1)

df[df[,1] %in% round(quantile(df[,1], probs = c(1:100)/100)),]
gd047
@gd047: I agree that this is something that Roberto asked for, but I'm not sure it's a useful subset, since it could have very different properties to the original. There could be several rows that match some percentiles, and none matching others.
Richie Cotton
George, I tried the following (I am interested in quantiles of row numbers):`df_small <- subset(df, row(df) %in% round(nrow(df)/100*(1:100),0))`I get: `Error: (subscript) logical subscript too long` and I can't figure out why this is. Ideas?Richie: your observation is right but I just want a summary Pareto plot with 100 datapoints, so this should be fine.
Roberto
@Roberto I would suggest `df_small <- df[round(nrow(df)/100*(1:100),0),]`
gd047
+1  A: 

For a 'big' dataset

dfr <- data.frame(x = 1:1000, y = runif(1000))

You can take subsets of regularly spaced rows with

dfr[!(seq_len(nrow(dfr)) %% 50),]

Or random subsets with

dfr[sample(nrow(dfr), 20),]

As gd047 mentioned, use quantile to get quantiles/percentiles.

Richie Cotton