Search code examples
rsummary

R get value from summary()


Please note that my R skills are very poor... I have the DS data structure which looks like this:

 loc anno.2013 anno.2014
1    1       1.9       1.9
2    2       0.8       1.0
3    3       1.4       1.0
4    4       2.4       1.9
5    5       1.5       1.7
6    6       2.3       2.2
7    7       1.0       1.2
8    8       0.8       1.1
9    9       1.2       0.9
10  10       0.7       0.9
11  11       0.9       1.2
12  12       1.1       1.5

Applying the summary() function to it the I reach the following result:

> s<- summary(loc.dolomiti)
> s
     loc          anno.2013       anno.2014    
Min.   : 1.00   Min.   :0.700   Min.   :0.900  
1st Qu.: 3.75   1st Qu.:0.875   1st Qu.:1.000  
Median : 6.50   Median :1.150   Median :1.200  
Mean   : 6.50   Mean   :1.333   Mean   :1.375  
3rd Qu.: 9.25   3rd Qu.:1.600   3rd Qu.:1.750  
Max.   :12.00   Max.   :2.400   Max.   :2.200 

I need to extract the Min value from the 2nd column (anno.2013), that is 0.7, but both s[7] and s[[7]] commands return the "Min :0.700 " string.

How to obtain this float value? Thanks


Solution

  • We can try sub function with a regex to capture everything after the colon, i.e.

    as.numeric(sub('.*:', '', summary(df)[1,2]))
    #[1] 0.7