我有一個數字向量var
具有名稱(來自predict.cv.glmnet輸出)薩姆列具有相似名稱
var<-c(5.74,0.00,0.15,0.00,0.04,0.00,0.00,0.00,1.81,0.00)
names(var)<- cbind("(Intercept)","as.factor(holiday)1","as.factor(season)2","as.factor(season)3","as.factor(season)4","as.factor(weathersit)2", "as.factor(weathersit)3","windspeed","temp","hum")
(Intercept) as.factor(holiday)1 as.factor(season)2 as.factor(season)3 as.factor(season)4 as.factor(weathersit)2
5.74 0.00 0.15 0.00 0.04 0.00
as.factor(weathersit)3 windspeed temp hum
0.00 0.00 1.81 0.00
我想提取具有非零值,並且還聚集因子級別的變量名稱(即如果一個因子的至少一個水平不爲零,那麼應該包括整個因子,輸出應該省略因子水平。我正在尋找一段代碼,它會給我這個結果:
"(Intercept)" "as.factor(season)" "temp"
我也有一個因子名稱變量fac
可供選擇:
fac<-c("as.factor(holiday)","as.factor(season)","as.factor(weathersit)")
"as.factor(holiday)" "as.factor(season)" "as.factor(weathersit)"
,並在心裏對類似名稱的骨料因素而忽略自己的水平,並檢查彙總因素的總和> 0,但我不能去對其進行編碼。
請考慮製作一個可重現的例子 – Sotos