成长值: 66850
|
How do I extract data from one column based on text in another column?
Even within the restrictions of a rectangular grid there are numerous ways to arrange data that all make perfect sense. Unfortunately, many arrangements make it difficult to plot or analyze your data in Origin. Let's examine one fairly common arrangement and see how LabTalk can be used to rearrange the data into a form suitable for easy plotting and data analysis in Origin.
We often gather data in a random order and then need to look at characteristics of different parts of the data. Suppose we have data such as:
Product | Shipped | Gadjet | 50 | Widjet | 25 | Thingamajig | 25 | Gadjet | 50 | Widjet | 25 | Gadjet | 100 | Widjet | 50 | Thingamajig | 25 | Gadjet | 100 | Widjet | 50 | Gadjet | 150 | Thingamajig | 25 | Gadjet | 200 | Widjet | 25 | Thingamajig | 50 | and we wish to view and analyze the Shipped values by Product group. It's easy in Origin to sort a worksheet based on the values in one or more columns:
Highlight a column
Select Analysis : Sort Worksheet
Choose Ascending or Descending or refine the sort by choosing Custom
With the sorted worksheet, you could highlight a range of rows in the Shipped column and plot or analyze the sub-range as needed. But what happens when we have large numbers of groups (making selection tedious) or a plot type does not easily support sub-ranges? The answer is a script that extracts these groups to new columns. We can then plot and analyze the groups as needed.Note 1
Before we begin, let's assume that our Worksheet is the active window and that the first column contains our groups and the second column contains our measurements for these groups. Our first step is to sort the worksheet. We will use the SORT object and we will need to find the number of rows of data and the column names:
get wcol(1) -e last; // Find the number of rows
%A=%(%H,@C,1); // What's the name of the first column?
%B=%(%H,@C,2); // What's the name of the second column?
With that information, we can sort the worksheet:
sort.C1=0;
sort.CNAME1$=%A: A;
sort.CNAME2$=%B: A; // This is optional - delete if you want to keep Shipped values in natural order
sort.R1=1;
sort.R2=last;
sort.WKSNAME$=%H;
sort.wks();
Initialize two variables:
%B=wcol(1)[1]$;
count=0;
define a macro:
def NewGroup {
wo -a 1;
wks.col$(wks.ncols).name$=%B;
for(jj=ii-count,row=1;jj<=ii-1;jj++,row++) wcol(wks.ncols)[row]=wcol(2)[jj];
}
and extract all the groups to new columns:
loop(ii,1,last) {
%A=wcol(1)[ii]$;
if("%A"!="%B") {
NewGroup;
%B=%A;
count=1;
} else {
count+=1;
}
}
NewGroup;
and clean up afterwards:
del -m NewGroup;
del -v count;
del -v last;
del -v ii;
del -v jj;
Now you can highlight any or all of the new group columns and plot or analyze as needed.
The algorithm works by looping through all the sorted group names. If the group name does not change, then just count it. When a name changes, a new column is created and the previous 'count' number of rows are copied to the new column
|
|