Q: Wanting to take the highest and lowest 25% of a table Hello as far as I could find there is no real solution for my task yet. (If there is please just let me know and I will take advice from there)
But without writing more unnecessary now:
I would like to get the highest and lowest 25% (or other percentages possible) values from a table (example see below). This involves several columns (4 to be exact) that have the highest and lowest values.
About my data:
Have a panel regression (with time series), which I analyze by influence.measures. This function gives the extreme values (similar to gvlma).
Then, since the number is quite high (+300 for a dataset of almost 3600 variables (or is this not much?)) I want to remove only the highest 25% and lowest 25% of the variables, from the table.
My function for this is as follows:
lm.01 <-lm(log(x) ~ log(intr) + log(y) + log(z) + factor(dummy)
           , data=r1.2) 

unnecessary_data1 <- influence.measures(lm.01)
table_u_d1 <- summary(unnecessary_data1)

drops1.2 <- c()
#in c() should the variables be included that should get excluded

r1.2 = r1.1
#r is my data set

r1.2 <- r1.2[-drops1.2,]

Output of table_u_d1 looks something like this (only the columns that should be necessary (there are more but those are not relevant):





example row
dffit
cov.r
cook.d
hat




9
-0.003754
-0.11643
0.972833
0.000972
0.001587


10
-0.056679
-0.09812
0.985349
0.000693
0.000687


100
0.005639
-0.23096
0.995242
0.003839
0.010623


301
0.006557
-0.24344
0.993736
0.004269
0.010729


490
-0.003771
-0.26348
1.015349
0.004991
0.010783


510
-0.020162
-0.23365
1.014367
0.003869
0.010551


530
-0.007013
0.07778
1.005749
0.004319
0.012449


720
-0.015032
-0.06255
1.010301
0.001425
0.010034


1120
-0.003564
-0.04462
1.019249
0.000053
0.010329


1320
0.000003
-0.00134
1.025679
0.004878
0.010186




I need the value of the first column as the result, because this indicates the position in the regression and only so I can use drops. And I need the values that have the highest and lowest 25% values in dffit, cov.r, cook.d and hat.
Maybe it could be useful for the exemplary data set that not the top and bottom 25% are used, but less. Need but please also a solution with 25% :).
If any further information are needed just let me know :)
Need help to loop and to get the final information
I have found now this way to get the information:
dffit.75.1<-data.frame(table_u_d1) %>% 
  filter(quantile(dffit, 0.75)<dffit)
dffit.25.1<-data.frame(table_u_d1) %>% 
  filter(quantile(dffit, 0.25)>dffit)

cov.r.75.1<-data.frame(table_u_d1) %>% 
  filter(quantile(cov.r, 0.75)<cov.r)
cov.r.25.1<-data.frame(table_u_d1) %>% 
  filter(quantile(cov.r, 0.25)>cov.r)

cook.d.75.1<-data.frame(table_u_d1) %>% 
  filter(quantile(cook.d, 0.75)<cook.d)
cook.d.25.1<-data.frame(table_u_d1) %>% 
  filter(quantile(cook.d, 0.25)>cook.d)

hat.75.1<-data.frame(table_u_d1) %>% 
  filter(quantile(hat, 0.75)<hat)
hat.25.1<-data.frame(table_u_d1) %>% 
  filter(quantile(hat, 0.25)>hat)

But afterwards it does not help to get a complete data
dffit.1<-rbind(dffit.25.1,dffit.75.1)
cov.r.1<-rbind(cov.r.25.1,cov.r.75.1)
....

Because the output would just always double the amount of the data (so duplicates are not recognised)

A: You can do something like this, just filter on the value you can find using the quantile function.
df <- data.frame(var = sample(1:100, 50))

df %>% filter(var > quantile(var, 0.75)) # gives you the highest 25%
df %>% filter(var < quantile(var, 0.25)) # gives you the lowest 25%

# combine to keep all except smallest 25% and biggest 25%
df %>% filter(var > quantile(var, 0.25) & var < quantile(var, 0.75))

