1

I'm sorry about this newbie question. I have a data frame like

l_tab$dis2_num$perc
  Var1        dis       pol        dif
1    1 0.79867550 0.2198391 0.57883635
2    2 0.14569536 0.1983914 0.05269606
3    3 0.05562914 0.5817694 0.52614030

Where I would like to find the min value of dif. I have try this:

> min(l_tab$dis2_num$perc["dif"])
>[1] 0.05269606

And that's correct, but i would like to recover also the row (in this case 2)

Searching for this stuff, i have try this:

Sorting and finding values in other data frames

that recommends the function which.min().

The problem arise when i try to apply this to my data

> which.min(l_tab$dis2_num$perc["dif"])
Erreur dans which.min(l_tab$dis2_num$perc["dif"]) : 
  the objet (list) can't be converted to type 'double'

> typeof(l_tab$dis2_num$perc)
[1] "list"

I don't understand why my data frame has become a list.

Also I have tried this

> r = as.data.frame(r)
> r

  Var1        dis       pol        dif
1    1 0.79867550 0.2198391 0.57883635
2    2 0.14569536 0.1983914 0.05269606
3    3 0.05562914 0.5817694 0.52614030

> typeof(r)
[1] "list"

without any result...

10 Rep
  • 2,217
  • 7
  • 19
  • 33
Kaervas
  • 103
  • 2
  • 16
  • It would be more insightful to have asked why did "[" not deliver an atomic vector from inside a list. Next time look at `str( l_tab$dis2_num$perc["dif"] )` – IRTFM Dec 12 '13 at 00:54
  • I'm sorry, i don't have enough level even to know what is an atomic vector... – Kaervas Dec 12 '13 at 10:13
  • Two kind of vectors in R: lists and atomic. – IRTFM Dec 12 '13 at 15:45

1 Answers1

5

A data.frame is a list and [ returns a sub-list. Instead, to extract the list item (the vector), you can do:

 which.min(l_tab$dis2_num$perc[["dif"]])

or

 which.min(l_tab$dis2_num$perc$dif)
Fernando
  • 7,785
  • 6
  • 49
  • 81