If I have the following list:


How can I find the closest value to "89" in the "T[C]" column and its corresponding value in the "DH,aged-DH,unaged (J/g)" column?.

Thank you in advanced,

  • $\begingroup$ Is someone working on this so far?. Thank you $\endgroup$ – John May 3 at 18:49
  • $\begingroup$ Can toy explain the first row, which appears to be column headers, but is a list of 5 elements. Which column is T[C]? {"Delta t (s)", 1.*10^-8}, {"q (K/s)", 1000}, {""}, {""}, {"Time(s)", "T[C]", "K(T)=k^(1/n)", "dx/dT", "x(t)", "DH,aged-DH,unaged (J/g)", "Check dx"} $\endgroup$ – Rohit Namjoshi May 3 at 19:15
  • $\begingroup$ @RohitNamjoshi Yes, each column header will be the "Column". So, the '"T[C]"' column includes all the values that start from 90.01 up to 89.2592 (those in the second element of the list). The "DH,aged-DH,unaged (J/g)" column would be the values that start in 0.0000159219 up to 0.0123268 (those in the sixth position of the lists). I hope that helps clarify it. $\endgroup$ – John May 3 at 19:20

Given your data

data[[5]] // InputForm

(* {"Time(s)", "T[C]", "K(T)=k^(1/n)", 
 "dx/dT", "x(t)", 
 "DH,aged-DH,unaged (J/g)", 
 "Check dx"} *)

values = data[[6 ;;]];

You are asking for data that corresponds to headers for columns {2, 6}.

The entry for the value of T[C] (column 2) closest to 89

entry = values[[Position[values[[All, 2]], 
     Nearest[values[[All, 2]], 89][[1]]][[1, 1]]]]

(* {3.87*10^-6, 89.2592, 5.13099, 0.0107504, 0.0102723, 0.0123268, 0.0000417117} *)

The desired values are

entry[[{2, 6}]]

(* {89.2592, 0.0123268} *)
| improve this answer | |
  • $\begingroup$ Bob, for some reason your code is not working. What does "values" do? it shows as a not defined variable. $\endgroup$ – John May 3 at 20:40
  • $\begingroup$ I forgot to copy definition. Added. $\endgroup$ – Bob Hanlon May 3 at 20:44
  • $\begingroup$ thank you that works!. Last quick question if you dont mind: I realized that they way I have my "data" is in TableForm so literally when I inpur data I will get rows and columns as if it were an excel file, where the headers are in the 5 row the data starts in the 6 row. When I pasted the input from data it looks like what I pasted in here where the output is not in TableForm. Is there any way to transform my data to into how I have it in here?.The reason I ask if because it seems that when it is as in TableForm the code does seem to work. Thank you $\endgroup$ – John May 3 at 21:07
  • 1
    $\begingroup$ I do not understand your comment. However, in general since TableForm is a wrapper and intended only for display, you do not want it included in a definition of data. That is rather than data = TableForm[datavalues] you would instead use TableForm[data = datavalues]. That way the definition of data does not contain the wrapper. $\endgroup$ – Bob Hanlon May 3 at 21:37
  • $\begingroup$ Bob Thank you very much ! $\endgroup$ – John May 3 at 21:58
data = Import["~/Downloads/data.txt"] // ToExpression // Part[#, 6 ;;] &;

nf = Nearest[data[[All, 2]] -> {"Index", "Element"}];
data[[nf[89][[1, 1]]]]

(* {3.87*10^-6, 89.2592, 5.13099, 0.0107504, 0.0102723, 0.0123268, 0.0000417117} *)
| improve this answer | |
  • $\begingroup$ Rohit, I am a little new to Mathematica and since I have my data already in mathematica as "data" and and do not need to imported, when I put data // ToExpression // Part[#, 6 ;;] &; it does not work. What should I put her to make it work if I already have the data and I don't need to import it?. Thank you $\endgroup$ – John May 3 at 20:45
  • 1
    $\begingroup$ @John If the data is already a List, then you do not need the ToExpression. Not sure if you need the Part[#, 6::]&, depends on the list structure. $\endgroup$ – Rohit Namjoshi May 3 at 21:47

With your data, assuming the column headings are in row 5:

(* {"Time(s)","T[C]","K(T)=k^(1/n)","dx/dT","x(t)","DH,aged-DH,unaged (J/g)","Check dx"} *)

... find the column numbers for columns "T[C]" and "DH,aged-DH,unaged (J/g)":

{c1, c2} = Flatten@{
   Position[data[[5]], "T[C]"], 
   Position[data[[5]], "DH,aged-DH,unaged (J/g)"]};

It's easier to search the data without the extra rows at the top. When there are multiple values that match, we find only the first one. Get the nearest value and the matching column:

values = data[[6 ;;]];
v = 89.;
First@Extract[values[[All, {c1, c2}]], 
  Position[values[[All, c1]], First@Nearest[values[[All, c1]], v]]]
(* {89.2592, 0.0123268} *)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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