C1 <- c("made in Italy", "100% silk", "dry clean only")
C2 <- c("80% cotton, 20% polyester","made in France", "hand wash")
C3 <- c("made in Italy", "Designer color : vanilla", " 100% nylon")
eg <- as.data.frame(rbind(C1,C2,C3))
我想添加一个可靠的&#34;组合&#34;通过提取包含&#34;%&#34;的所有值标志。如您所见,每个观察的组成值不在同一列中...... 我尝试了几种方法但未能实现这一目标。 例如:
fcompo <- function(x){
if (grepl('%',x) = TRUE){eg$composition <- paste(x)}
else {eg$composition=NA}
}
然后我迷失在这个功能中...对R语言来说很新... 我也尝试过:
library(stringr)
eg$composition <- str_extract(eg[,c(1:3)], "%$" )
非常感谢您的慷慨帮助
答案 0 :(得分:1)
您可以apply
margin=1
使用eg$composition <- apply(eg, 1, function(x) grep("%", x, value=TRUE))
eg
# V1 V2 V3 composition
#C1 made in Italy 100% silk dry clean only 100% silk
#C2 80% cotton, 20% polyester made in France hand wash 80% cotton, 20% polyester
#C3 made in Italy Designer color : vanilla 100% nylon 100% nylon
按行保留带有合成的字段(即包含至少一个“%”符号的字段):
public downloadItemsExcel() {
$items = Item::all();
Excel::create('items', function($excel) use($items) {
$excel->sheet('ExportFile', function($sheet) use($items) {
$sheet->fromArray($items);
});
})->export('xls');
}
答案 1 :(得分:1)
您可以使用regmatches
。
unlist(regmatches(unlist(eg),gregexpr("\\d+%.*",unlist(eg))))
V1.C2 V2.C1 V3.C3
"80% cotton, 20% polyester" "100% silk" "100% nylon"