我在let clusterSource = new ol.source.Cluster({
distance: CLUSTER_DISTANCE,
source: features
});
let clusterLayer = new ol.layer.Vector({
source: source,
style: function(feature, resolution) { }
});
中使用if (transparent) {
if (st > 50) {
//header.removeClass('av_header_transparency');
av_change_class(header, 'remove', 'av_header_transparency');
} else {
//header.addClass('av_header_transparency');
av_change_class(header, 'add', 'av_header_transparency');
}
}
参数来融合具有多个容易定义的模式的列的数据。它正在工作,但我没有看到我如何创建一个字符索引变量而不是默认的数字细分。
例如,在A中,对狗和猫的列进行了编号...请查看“变量”列:
patterns()
但是,在B中,dog和cat列用文本编号,但“变量”列仍然是数字。
data.table::melt()
如何使用一/二/三而不是1/2/3填充“变量”列?
答案 0 :(得分:10)
可能有更简单的方法,但这似乎有效:
# grab suffixes of 'variable' names
suff <- unique(sub('^.*_', '', names(B[ , -1])))
# suff <- unique(tstrsplit(names(B[, -1]), "_")[[2]])
# melt
B2 <- melt(B, measure = patterns("^dog", "^cat"), value.name = c("dog", "cat"))
# replace factor levels in 'variable' with the suffixes
setattr(B2$variable, "levels", suff)
B2
# idcol variable dog cat
# 1: 1 one 1 101
# 2: 2 one 2 102
# 3: 3 one 3 103
# 4: 4 one 4 104
# 5: 5 one 5 105
# 6: 1 two 6 106
# 7: 2 two 7 107
# 8: 3 two 8 108
# 9: 4 two 9 109
# 10: 5 two 10 110
# 11: 1 three 11 111
# 12: 2 three 12 112
# 13: 3 three 13 113
# 14: 4 three 14 114
# 15: 5 three 15 115
请注意,此主题存在一个未解决的问题,其他一些选择:FR: expansion of melt functionality for handling names of output。
这是我认为good'ol base::reshape
更清洁的(罕见)情况之一。它的sep
参数在这里派上用场 - 'value'列的名称和'variable'列的级别一次生成:
reshape(data = B,
varying = names(B[ , -1]),
sep = "_",
direction = "long")