根據單獨的.csv文件中行中的值重命名jmp文件中的列名你好,
我是JSL的新手,非常感謝您的幫助。
我有三個文件。
文件#1:這是參考文件,[as ref.csv]包含3個列,如下所示。
類別,寬度,標籤
--------------------------------
A,0,L1.abc
A,1,L2.xt
A,2,L8.dr
A,3,L20.ws
A,4,L72.oz
B,0,L11.qq
....
B,6,L61.er
文件#2:JMP中的數據文件#1,保存類別,數據和許多其他列。
文件#3:JMP中的數據文件#2,保存類別,數據和許多其他列。
兩個數據文件[文件#2和文件#3]的區別在於數據列中類別的類型和值的寬度。
數據文件1:
類別|數據|許多列.....
----------------------------------------
A 123.23 ....
A 321.45 ....
.....
多行
對於給定類別,“數據”列中的值寬度(不包括點)與文件#1中的行數匹配
例:
數據文件1中第1行的123.23寬度為5。
在參考文件中,類別A的行數為5。無需對每個說法進行測試。這就是表格的輸入方式(即從源中拉出時)
將來,在參考文件中,例如對於類別A,行數增加到6個或更多,當從數據庫中提取信息時,保存在文件數據文件#1中的數據列中的值的寬度也會增加。
數據文件2:
類別|數據|許多列.....
----------------------------------------
B 1236.23 ....
B 3213.45 ....
.....
多行
目標1:根據數據文件#1 [和其他數據文件]中“數據”列中保存的值的寬度創建新列
我已經完成了目標1!
例如:
更新後的數據文件1看起來像這樣:
類別|數據| c1 | c2 | c3 | c4 | c5 |許多列.....
----------------------------------------
A 123.23 1 2 3 2 3 ....
A 321.45 3 2 1 4 5 ....
.....
目標2:按照參考文件中“標籤”列中的定義,將數據文件#1中的新列名重命名為L1 ...至L72 ...單元格值保持不變,只需更新所選的列名即可。
目標2完成後的預期輸出:
更新數據文件1如下所示:
類別|數據| L1.abc | L2.xt | L8.dr | L20.ws | L72.oz |許多列.....
----------------------------------------
A 123.23 1 2 3 2 3 ....
A 321.45 3 2 1 4 5 ....
.....
我需要目標2的幫助。
對於目標2,我打開了參考文件,但不確定前進。
謝謝
大衛
數據表回复:按照單獨的.csv文件中各行中的值重命名jmp文件中的列名這是未經測試的腳本,應該非常接近能夠實現第二個目標
dtCat = Data Table( "file#1 name" );
dt1 = Data Table( "file#2 name" );
dt2 = Data Table( " file#3 name" );
// add dt3, dt4 etc. for each of the categories you have
// Find the number of data tables to process....A B C......
// Create a temporary new column in data table dtCat that has just the category
dtCat << New Column( "temp", character, formula( Word( 1, :data, "." ) ) );
// Summarize the data getting one element in the ByGroup list for each Category found
Summarize( dt, Categories = ByGroup( :temp ) );
numCategories = N Items( Categories );
For( i = 1, i <= numCategories, i++,
// Subset the dtCat table into the a new data table with just the one
// category being processed
dtCat << select where( :temp == Categories[i] );
dtSub = dtCat << subset( selected rows == 1, selected columns( 0 ) );
// set the offset of where to start the renaming
offset = 3;
// Loop
// Change names in dt1 to match the Label in
For( k = 1, k <= N Rows( dtSub ), k++,
Eval(
Substitute(
Expr(
Column( __dt__, k + offset ) << set name( Word( 3, dtSub:Data[k], "." ) ),
Expr( __dt__ ),
Parse( "dt" || Char( i ) )
)
)
)
);
Close( dtSub, nosave );
);回复:按照單獨的.csv文件中各行中的值重命名jmp文件中的列名謝謝吉姆。它不起作用。
因此,對於目標2,我將整個邏輯分解為價值步驟。
步驟1:從數據表中獲取類別名稱[例如dt1],並將其存儲在變量中[例如,它持有A的類別]-完成
步驟2:從步驟1中讀取變量,並找到與category = A]匹配的所有行,並從ref.csv中的“標籤”列中獲取標籤名稱,[您將其稱為dtCat],並將其存儲在列表或變量中。 [稱它為category_list]
因此,請使用討論中所述的示例。 我期望category_list保持低於值。
category_list = L1.abc, L2.xt L8.dr, L20.ws, L72。盎司
我不知道該怎麼做。 感謝幫助。
步驟3:檢查列表中的計數==否。在dt1中創建的新列。這是我為提高魯棒性而要做的事情。
在category_list中的計數是5個元素
在dt1中創建的新列為5。 [我從目標1得到這個,因為我拿著一個變量來查找需要多少個新列]。
步驟4:使用i = 5的循環,將dt1中的cols名稱(即c [i])替換為列表[i]
c1取代
我現在處於第2步,需要一些幫助。
謝謝,
大衛
回复:按照單獨的.csv文件中各行中的值重命名jmp文件中的列名- 您指出我的上一個代碼不起作用.....為什麼不呢?
- 這是一種創建您要的列表的方法
Category_List = {};
For( i=1, i<= N Rows( dtCat ), i++,
insert into(Category_List, word(3,:data[i],".") || "." || word(4,:data[i], "." ) );
);
回复:按照單獨的.csv文件中各行中的值重命名jmp文件中的列名我查看了您提供的代碼。 似乎有些混亂。
這就是我攔截並糾正我的方式,這是錯誤的。
dtcat是具有col名稱[類別,寬度,標籤]。
有多個具有相同結構的數據表dt1,dt2等,其列名為[Category | data | many .....]
當我運行代碼時,它什麼也沒做
在dtcat表中沒有名為data的列,並調查了調試。
可能是您的意思是dt1表,因為它是數據列。
我嘗試玩耍,這無濟於事。
我認為我們在表格上的定義保持一致,請使用我在原始帖子中描述的內容,我認為可能會有所幫助。
再次感謝您的幫助。
讓我推薦第二步
謝謝
大衛
回复:按照單獨的.csv文件中各行中的值重命名jmp文件中的列名吉姆
對於具有以下修改的步驟2 [如下所示]
++++
這是我的ref.csv:
類別|寬度|標籤
ABC 0標籤0.0
ABC 1標籤0.1
ABC 2標籤0.2
ABC 3標籤0.3
ABC 4標籤0.4
ABC 5標籤0.5
ABC 6標籤0.6
ABC 7標籤0.7
ABC 8標籤0.8
ABC 9標籤0.9
ABC 10標籤0.10
ABC 11標籤0.11
ABC 12標籤0.12
ABC 13標籤0.13
ABC 14標籤0.14
ABC 15標籤1.15
ABC 16標籤0.16
ABC 17標籤0.17
ABC 18標籤0.18
ABC 19標籤0.19
ABC 20標籤0.20
B 0標籤11.22
C 1標籤11.21
D 2標籤11.23
E 0標籤11.24
我的代碼:
dt_ref =打開(“ ref.csv”);
對於(i = 1,i <= N Rows(dt_ref),i ++,
插入(Category_List,word(1,:label [i],“。”) || “。” || word(2,:label [i],“。”));
);
show(Category_List);
+++
我在日誌中得到以下內容:
Category_List = {“ label0.0”,“ label0.1”,“ label0.2”,“ label0.3”,“ label0.4”,“ label0.5”,“ label0.6”,“ label0.7” ,“ label0.8”,“ label0.9”,“ label0.10”,“ label0.11”,“ label0.12”,“ label0.13”,“ label0.14”,“ label1.15”,“ label0.16”,“ label0.17”,“ label0.18”,“ label0.19”,“ label0.20”,“ label11.22”,“ label11.21”,“ label11.23”,“ label11。 24“,”。“,”。“};
我希望僅當類別為ABC時才選擇標籤。我怎麼做?
我嘗試了以下操作,但Category_List卻沒有返回任何內容:
prod_found = {“ ABC”};
如果(:product == prod_found,
對於(i = 1,i <= N Rows(dt_ref),i ++,
插入(Category_List,word(1,:name [i],“。”) || “。” || word(2,:name [i],“。”));
);
);
謝謝,
大衛
回复:按照單獨的.csv文件中各行中的值重命名jmp文件中的列名對不起我的錯誤消息。我誤讀了類別數據表的結構。下面是一個腳本,該腳本創建幾個數據表和一個類別表,然後展開數據表,最後根據類別數據表中的條目重命名列。我希望這能使我們重回正軌。該腳本將根據需要在盡可能多的數據表上運行。所有要做的就是將所有要處理的數據表添加到dataTableList列表中
Names Default To Here( 1 );
// This example script opens up 2 data tables and 1 category table.
// In reality, as many as necessary data tables could be used
// Create the tables
dt1 = New Table( "Table1",
Add Rows( 3 ),
New Column( "Category", Character, "Nominal", Set Values( {"ABC", "ABC", "ABC"} ) ),
New Column( "DATA", Character( 33 ), "Nominal", Set Values( {"123.56.8.342", "431.02.0.567", "318.00.1.816"} ), Set Display Width( 182 ) )
);
dt2 = New Table( "Table2",
Add Rows( 3 ),
New Column( "Category", Character, "Nominal", Set Values( {"B", "B", "B"} ) ),
New Column( "Data", Character, "Nominal", Set Values( {"13.24", "98.44", "63.89"} ) )
);
dtCat = New Table( "The Categories",
Add Rows( 13 ),
New Column( "Category",
Character,
"Nominal",
Set Values( {"ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "B", "B", "B", "B"} ),
Set Display Width( 67 )
),
New Column( "Width", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3] ) ),
New Column( "Label",
Character( 16 ),
"Nominal",
Set Values(
{"label0.0", "label0.1", "label0.2", "label0.3", "label0.4", "label0.5", "label0.6", "label0.7", "label0.8", "thelabel.1", "thelabel.2",
"thelabel.3", "thelabel.4"}
),
Set Display Width( 99 )
)
);
// Below this line, is the script that will add the new columns to each of the data tables
// and will take the names from the dtCat (Categories) data table and change the names to
// the columns in each of the data tables
// Create a list of the pointers to the data tables. This will allow for the using of the list
// entries to point to the data tables using a subscript
dataTableList = {};
insert into(dataTableList, dt1 );
insert into(dataTableList, dt2 );
// Loop across the data table list and expand the columns based upon the the column called Data
For( i = 1, i <= N Items( dataTableList ), i++,
ncols_to_add = length( substitute((dataTableList[i]:Data)[1],".",""));
For( k = 1, k <= ncols_to_add, k++, // the || operator joins two strings
dataTableList[i] << New Column( "c" || Char( i ),
Numeric,
Continous,
<<SET each="" value=""></SET>