1

I have a dataset of 2 columns as follows a 1 b 2 c 3

That dataset doesn't have names for both columns, how should I use select to work on it, i.e., get data with the second column number bigger than 2

myDataset[Select[#2>2]]  doesn't work,   How to refer the second column?

thanks in advance

Valeriy
  • 185
  • 10
user1470393
  • 279
  • 1
  • 8

2 Answers2

3

Two mistakes:

  • The function must end in &
  • A whole row of the dataset will be passed to that function as a list, so use #[[2]] > 2& instead of #2 > 2&.
Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
0

Try this:

define your data, let's say:

myData = {{a, 1}, {b, 2}, {c, 3}, {d, 5}, {e, 45}, {f, 2}, {g, 3}}

{{a, 1}, {b, 2}, {c, 3}, {d, 5}, {e, 45}, {f, 2}, {g, 3}}

In order to get second column

myData[[All, 2]]

{1, 2, 3, 5, 45, 2, 3}

So, your query should be something like this:

Select[myData[[All, 2]], # > 2 &]

{3, 5, 45, 3}

If you need their position, you can use this (credits to J.M., who also suggests Flatten should you need to manipulate somehow your results:

Position[myData[[All, 2]], _?(# > 2 &)]

{{3}, {4}, {5}, {7}}

Fierce82
  • 543
  • 1
  • 4
  • 14