数据表聚合中的条件因子水平选择(Conditional Factor Level Selection in Aggregation of Data Table)
我试图将每个ID的
data.table
聚合到1行。假设第一列表示ID,最后一列是感兴趣的因素:
mydt <- data.table(matrix(c(1,2,"Level 1", 1,12,"Level 0", 1,12,"Level 0", 2,12,"Level 3", 2,12,"Level 2"), nrow = 5, ncol = 3, byrow = TRUE)) mydt V1 V2 V3 1: 1 2 Level 1 2: 1 12 Level 0 3: 1 12 Level 0 4: 2 12 Level 3 5: 2 12 Level 2
我对如何汇总因素有非直观的规则:
- 如果
Level 1
存在任何ID行,那么聚合行应该具有该Level 1
的ID- 如果没有,那么如果该ID存在
Level 2
,则使用它- 如果不存在,则存在
Level 3
- 如果不是,则为
Level 0
实际的
data.table
非常大,没有实际因子水平的数字分量,它们只是字符串。 这个脚本每天至少运行一次,所以我试图避免使用循环进行缓慢的预处理。所需的结果如下所示:
V1 V2 V3 1: 1 8.67 Level 1 2: 2 12 Level 2
但是我找不到合适的聚合功能...
mydt[,.(V2 = mean(V2, na.rm = T), V3 = if("Level 1") "Level 1" else if("idk help me out?")), by = "V1"]
I'm trying to aggregate a
data.table
to 1 row per ID.Suppose the first column represents ID and the last column is the factor of interest:
mydt <- data.table(matrix(c(1,2,"Level 1", 1,12,"Level 0", 1,12,"Level 0", 2,12,"Level 3", 2,12,"Level 2"), nrow = 5, ncol = 3, byrow = TRUE)) mydt V1 V2 V3 1: 1 2 Level 1 2: 1 12 Level 0 3: 1 12 Level 0 4: 2 12 Level 3 5: 2 12 Level 2
I have non-intuitive rules for how to aggregate the factor:
- if
Level 1
exists for any row of ID then the aggregated row should haveLevel 1
for that ID- if not, then if
Level 2
exists for that ID then use it- if not, then
Level 3
if it exists- if not, then
Level 0
The actual
data.table
is very large and there is no numeric component of the actual factor levels, they are just strings. This script will be run at least once per day, so I'm trying to avoid slow pre-processing with loops.The desired result would look like this:
V1 V2 V3 1: 1 8.67 Level 1 2: 2 12 Level 2
However I can't find an suitable aggregation function...
mydt[,.(V2 = mean(V2, na.rm = T), V3 = if("Level 1") "Level 1" else if("idk help me out?")), by = "V1"]
原文:https://stackoverflow.com/questions/35232201
最满意答案
相关问答
更多-
设置allowDecimals: false ,请参阅API 。 Set allowDecimals: false, see API.
-
一个非常好的问题! 与编程/高图问题相比,它更像是一个数学/几何问题。 但是,嘿,这个杂耍中有一个完整的主题。 电脑图像 !! 如果我的问题得到了解决(我希望我做到了,否则花了一个小时解决了一个不存在的问题,另一个草拟了一个可以理解的答案)。 您总是希望valueX1(90)与valudY1(60)和valueX2(140)对齐以与值Y2(90)对齐。 X和Y绘制在不同的Y轴上。 这是解决方案。 我们所需要做的就是正确对齐两个坐标轴的最小值和最大值,以便所需点也能正确对齐。 这是我们如何做到的。 接受由hi ...
-
不幸的是,Highcharts不提供将yAxis以相同的值对齐的选项(例如0的值)。 我有一段时间准备的例子为两个轴做出相同的位置: http : //jsfiddle.net/5m9JW/349/你可能应该能够升级以获得相同的更多轴 while (chart.yAxis[1].translate(0) != chart.yAxis[0].translate(0) && i > 0) { chart.yAxis[0].setExtremes(chart.yAxis[0].getExtremes(). ...
-
您可能正在寻找threshold选项。 plotOptions: { series: { threshold: 100 } } 演示 You're probably looking for the threshold option. plotOptions: { series: { threshold: 100 } } Demo
-
目前没有这样做的设置。 有一项功能要求可以达到这个目标,这个要求已经有一段时间了,在这里: http://highcharts.uservoice.com/forums/55896-highcharts-javascript-api/suggestions/1848953-extend-the-axis-max-property-to-act-like-css-mi 现在,您必须检查您的数据,如果没有负值,请将min设置为0,如果有负值,则不执行任何操作。 它在缺省情况下对区域图起作用,因为区域类型默认情况 ...
-
您可以通过编程方式注释图表。 在加载时,您可以使用渲染器创建标签或形状。 在重绘时,您可以重绘注释(在缩放时触发重绘): function annotateChart({ text, value }) { const annons = this.annons; const label = this.renderer.label(text, 0, -9e9).attr({ stroke: 'red', 'stroke-width': 2, align: 'center', ...
-
更新 使用以下代码: $(document).on('click', '#updateYScale', function(e) { var yValue = $('#newYValue')[0].value; var chart = $('.actualPlansPlot').highcharts(); chart.yAxis[0].update({ max: yValue }); ...
-
Highcharts Y轴标签(Highcharts Y-axis labels)[2022-01-24]
我能够找到问题的解决方案,我首先将Yaxis更改为xaxis并添加了一个循环,遍历了数组中的每个结果 xAxis: { allowDecimals: false, categories: function(){ var data for(var i=0;i< final.length;i++){ data.push( ... -
您不能为负侧和正侧设置不同的比例。 您可以设置xAxis.min和startOnTick: false 。 要添加一些刻度(无论如何它们将彼此接近!),您可以创建tickPositioner并返回所有刻度(负数和正数)。 You can't set different scale for negative and positive sides. You can set xAxis.min and startOnTick: false. To add some ticks (they will close t ...
-
尝试 m1 <- matrix(df1[,2], ncol=3, dimnames=list(NULL, df1[,1])) barplot(m1, ylim=c(100,150), beside=TRUE, xpd=FALSE) 要么 library(ggplot2) ggplot(df1, aes(x=Name, y=Count))+ geom_bar(stat='identity')+ coord_cartesian(ylim=c(100, ...