我想使用R对let obj = {"contract_no":"1","history_no":"","f_sin":"","line_no":"","p_typeid":"3","compare_typeid":1,"val_typeid":"1","goal_val":0,"count_typeid":1,"f_pb_ignore":0,"all_products":0,"judge_typeid":"","reach_condition_array":[{"up_s_line_no":1,"t_from":111,"t_to":222,"rebate":11},{"up_s_line_no":2,"t_from":222,"t_to":null,"rebate":22}],"series_appoint":[{"down_s_line_no":1,"deptCD":"0","categoryCD":"12","singleJan":"","seriesJan":"","fExclude":0},{"down_s_line_no":1,"deptCD":null,"categoryCD":"4","singleJan":"","seriesJan":"","fExclude":0},{"down_s_line_no":1,"deptCD":"55","categoryCD":"","singleJan":"","seriesJan":"","fExclude":0},{"down_s_line_no":1,"deptCD":"","categoryCD":2222,"singleJan":"58796","seriesJan":"1000000009","fExclude":0}]};
function blank(value) {
return value === null || value.trim() === '';
}
let series = obj.series_appoint;
console.log('deptCD', series.filter(item => blank(item.deptCD)).length);
console.log('singleJan', series.filter(item => blank(item.singleJan)).length);
的列表进行排序,首先使用c("ABC-L1","DEF-L2","XYZ-B12","AAA-B1")
,然后使用/-([LB])(\d+)/
。{p>
这意味着我需要订单$1
。
如何执行此操作?
答案 0 :(得分:3)
我们可以使用sub
:
vec <- c("ABC-L1","DEF-L2","XYZ-B12","AAA-B1")
vec[order(sub('^[A-Z]+-', '', vec))]
# [1] "AAA-B1" "XYZ-B12" "ABC-L1" "DEF-L2"
sub
删除所有内容,直到-
,然后我们order
其余部分。
答案 1 :(得分:2)
以下内容可满足您的需求。
x <- c("ABC-L1","DEF-L2","XYZ-B12","AAA-B1")
sp <- do.call(rbind, strsplit(x, "-"))
inx <- order(sp[, 2], sp[, 1])
x[inx]
#[1] "AAA-B1" "XYZ-B12" "ABC-L1" "DEF-L2"
答案 2 :(得分:0)
使用str_plit以“-”分隔,然后按列排序
text <- c("ABC-L1","DEF-L2","XYZ-B12","AAA-B1")
text<- t(as.data.frame(str_split(text, "-")))
res <- text[order(text[, 2], text[, 1]),]