Use function of a variable as a part specifier?

My question is how do I use a function of a variable such as PrimePi[x], Floor[x], or IntegerPart[x] as a part specifier to index the element of a list?

Assuming I have a single-dimensional list called data, I want to index into this list similar to one of the following:

data[[PrimePi[x]]]  
data[[Floor[x]]]  
data[[IntegerPart[x]]]  

When I try to do this, I get errors such as the following:

Part::pkspec1: The expression IntegerPart[x] cannot be used as a part specification.  

All of these functions evaluate to integers, so I don’t understand why I can’t index into a list with functions like these which I need to do for my application. I’ve tried assigning the function to a variable first, and then using the variable to index into the list and I get the same type of error.

The following example is only intended to illustrate the problem, and the function being computed in the example is not intended to have any practical value.

fOfY[y, x] := Sum[x/(n y), {n, 1, Floor[x]}]  

plotFOfY[fOfY[], xmax] := Block[{y = 1, i = 1, ylist = {}}, While[i <= xmax, If[PrimeQ[i], y *= i]; ylist = AppendTo[ylist, y]; i++]; Plot[fOfY[ylist[[PrimePi[x]]], x], {x, 2, xmax}, PlotRange -> Full, GridLines -> Automatic]]  
  
discretePlotFOfY[fOfY[], xmax] := Block[{y = 1, i = 1, ylist = {}}, While[i <= xmax, If[PrimeQ[i], y *= i]; ylist = AppendTo[ylist, y]; i++]; DiscretePlot[fOfY[ylist[[PrimePi[x]]], x], {x, 2, xmax}, PlotRange -> Full, GridLines -> Automatic]]  

When I run the discretePlotFOfY, I don’t get any error messages.

discretePlotFOfY[fOfY[], 20]  

When I run the plotFOfY, I get error messages.

plotFOfY[fOfY[], 20]  



Part::pkspec1: The expression PrimePi[x] cannot be used as a part specification.

Part::pkspec1: The expression PrimePi[x] cannot be used as a part specification.

Part::pkspec1: The expression PrimePi[x] cannot be used as a part specification.

General::stop: Further output of Part::pkspec1 will be suppressed during this calculation.

Something like that ?

In[1]:= data = Table[RandomReal[{0, 100}], {10}]

Out[1]= {73.284, 56.1426, 56.781, 11.0764, 75.6524, 21.7474, 38.2588, \
12.4285, 63.7419, 48.5597}

In[2]:= SortBy[data, Floor]

Out[2]= {11.0764, 12.4285, 21.7474, 38.2588, 48.5597, 56.1426, \
56.781, 63.7419, 73.284, 75.6524}

Christian: Thanks for the response, but not exactly. Assuming I have a single-dimensional list called data, I want to index into this list similar to one of the following:
data[[PrimePi]]
data[[Floor]]
data[[IntegerPart]]

When I try to do this, I get errors such as the following:
Part::pkspec1: The expression IntegerPart[x] cannot be used as a part specification.

All of these functions evaluate to integers, so I don’t understand why I can’t index into a list with functions like these which I need to do for my application. I’ve tried assigning the function to a variable first, and then using the variable to index into the list and I get the same type of error.

So I guess you need to define a function and then evaluate:

In[67]:= data

Out[67]= {73.284, 56.1426, 56.781, 11.0764, 75.6524, 21.7474, \
38.2588, 12.4285, 63.7419, 48.5597}

In[68]:= Clear[f, g]
(*simplest*)
f[x_] := data[[PrimePi[x]]]
(*with more generality*)
g[x_, function_: PrimePi, data_List: data] := 
 data[[function[x]]] /; 
  IntegerQ[function[x]] && function[x] >= 1 && 
   function[x] <= Length[data]

In[71]:= f[7]

Out[71]= 11.0764

In[72]:= g[7]

Out[72]= 11.0764

In[73]:= PrimePi[10]
PrimePi[100]

Out[73]= 4

Out[74]= 25

In[75]:= g[10, PrimePi, data]

Out[75]= 11.0764

In[76]:= g[100, PrimePi, data]

Out[76]= g[100, PrimePi, {73.284, 56.1426, 56.781, 11.0764, 75.6524, 
  21.7474, 38.2588, 12.4285, 63.7419, 48.5597}]

What exactly does not work. Please. post your code and result here

I am getting a result with no error

In[12]:= data = Table[RandomReal[{0, 100}], {10}]
data[[PrimePi[3]]]

Out[12]= {47.8615, 54.4348, 76.9031, 74.2042, 92.5634, 63.0498, \
12.3545, 43.1661, 29.4993, 69.0345}

Out[13]= 54.4348

What exactly does not work. Please. post your code and result here

I am getting a result with no error

In[12]:= data = Table[RandomReal[{0, 100}], {10}]
data[[PrimePi[3]]]

Out[12]= {47.8615, 54.4348, 76.9031, 74.2042, 92.5634, 63.0498, \
12.3545, 43.1661, 29.4993, 69.0345}

Out[13]= 54.4348

Christian: Thanks for the response, but not exactly. Assuming I have a single-dimensional list called data, I want to index into this list similar to one of the following:
data[[PrimePi]]
data[[Floor]]
data[[IntegerPart]]

When I try to do this, I get errors such as the following:
Part::pkspec1: The expression IntegerPart[x] cannot be used as a part specification.

All of these functions evaluate to integers, so I don’t understand why I can’t index into a list with functions like these which I need to do for my application. I’ve tried assigning the function to a variable first, and then using the variable to index into the list and I get the same type of error.

The following example is only intended to illustrate the problem, and the function being computed in the example is not intended to have any practical value.
fOfY[y_, x_] := Sum[x/(n y), {n, 1, Floor[x]}]

plotFOfY[fOfY_[], xmax_] := Block[{y = 1, i = 1, ylist = {}},
While[i <= xmax, If[PrimeQ[i], y *= i]; ylist = AppendTo[ylist, y];
i++];
Plot[fOfY[ylist[[PrimePi[x]]], x], {x, 2, xmax}, PlotRange → Full,
GridLines → Automatic]]

discretePlotFOfY[fOfY_[], xmax_] := Block[{y = 1, i = 1, ylist = {}},
While[i <= xmax, If[PrimeQ[i], y *= i]; ylist = AppendTo[ylist, y];
i++];
DiscretePlot[fOfY[ylist[[PrimePi[x]]], x], {x, 2, xmax},
PlotRange → Full, GridLines → Automatic]]

When I run the discretePlotFOfY, I don’t get any error messages.
discretePlotFOfY[fOfY[], 20]

When I run the plotFOfY, I get error messages.
plotFOfY[fOfY[], 20]

Part::pkspec1: The expression PrimePi[x] cannot be used as a part specification.

Part::pkspec1: The expression PrimePi[x] cannot be used as a part specification.

Part::pkspec1: The expression PrimePi[x] cannot be used as a part specification.

General::stop: Further output of Part::pkspec1 will be suppressed during this calculation.

How to use function of a variable as a part specifier? I have more doubts related to this particular. This post helps me a lot and I have gotten some best ideas from here.

Did this question get answered?