我在這個格式的CSV:創建並從現有列填充基於價值的數據幀新列
Col1_Status Col1_Value Col2_Status Col2_Value Col3_Status Col3__Value
LOW 5 HIGH 5 LOW 5
LOW 8 HIGH 8 LOW 8
HIGH 82 HIGH 8 LOW 7
HIGH 83 NORMAL 8 LOW 7
HIGH 82 NORMAL 8 LOW 7
我想創建一個具有高,低欄目,例如一個新的數據幀:
Col1_High Col1_Low Col2_High Col2_Low Col3_High Col3_Low
82 5 5 NA NA 5
83 8 8 NA NA 8
82 NA 8 NA NA 7
NA NA NA NA NA 7
NA NA NA NA NA 7
什麼是最好的方式去做這件事?
到目前爲止,我認爲:
#extract the Status Columns from original file into DataFrame
statusDF <- ret[grepl("Status", colnames(ret))]
#extract the Value Columns from original file into DataFrame
originalValueDF <- ret[grepl("Value", colnames(ret))]
#create new columns attribute_high and attribute_low
for(i in names(originalValueDF)){
newValueDF <- originalValueDF[[paste(i, 'High', sep = "_")]]
newValueDF <- originalValueDF[[paste(i, 'Low', sep = "_")]]
}
#populate both columns based on value in attribute status column
for(i in names(originalValueDF)){
if (originalValueDF$i == "High"){
temp <- # stuck here
}
}
任何建議表示讚賞
'Col3_Low = C(5,8)'...其中是7?你的標準是什麼? – Sotos
對不起,我只是把前兩個元組作爲所需的輸出。標準是查看狀態列並將其提取到高或低的新列中。 – ukbaz
已更新輸出數據幀 – ukbaz