Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R : min and max numeric value with name

Tags:

r

max

min

NAME                                        Number
cars                                         10
people                                       340
bus                                          4

I have to find way to rapresent minimum and maximum numeric value with his correspondent name from first coulmn.

if I put command :

min(data[,2]) 
max(data[,2]) 

the results are only values

the final result should be displayed like :

  • for minimum value*

    Bus 4

  • for maximum value*

    people 340

like image 413
Oleg Navolokin Avatar asked Dec 13 '25 22:12

Oleg Navolokin


1 Answers

You can get the min and max rows separately with

df1[which.max(df1[,2]),]

Or

df1[which.min(df1[,2]),]

For plotting, may be

df2 <- subset(df1, Number %in% c(min(Number), max(Number)))
m1 <- t(df2[,2])
colnames(m1) <- df2[,1]
barplot(m1)

Update

Using the example in the image,

dfN <- data.frame(Col1=c('Controlli di Polizia Giudiziaria', 
'Ricrosi a seguito di contravvenzioni', 
'Ordinanze e inguinzioni sul commercio', 'Automezzi', 
'Chilometri percorsi', 'Infrazioni al codice della strada'),
 number = c(249, 349, 152, 8, 41658 , 8597))

 colnames(m1) <- sub('(\\S+\\s\\S+)\\s(\\S+\\s\\S+)(.*)', 
             '\\1\n\\2\n\\3', dfN[,1])
 barplot(m1, cex.names=0.7)
like image 132
akrun Avatar answered Dec 15 '25 13:12

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!