1
$\begingroup$

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

$\endgroup$

2 Answers 2

3
$\begingroup$

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&.
$\endgroup$
0
$\begingroup$

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}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.