我有三个看起来像这样的向量:
x = c(NaN, 15, 16, 14, 14, NaN)
y = c(NaN, NaN, NaN, NaN, NaN, 11)
z = c(17, NaN, NaN, NaN, NaN, 12)
我需要将它们合并到一个向量中。我需要这三个平滑地网格化,并且在存在重叠值的地方(如y和z所示),应优先考虑z的值。最终结果应如下所示:
xyz = c(17, 15, 16, 14, 14, 12)
我已经研究过使用rowSums来处理na,但是在同一个索引上有多个值的情况下这不起作用。我试图尽可能避免循环。
非常确定我可以通过遍历向量来完成此操作,但我正在使用相当大的数据集。
答案 0 :(得分:2)
这似乎有效。 NB 它依赖于rev
作为数据框的最后一列,然后翻转它以便它首先(即df <- data.frame(x = c(NaN, 15, 16, 14, 14, NaN),
y = c(NaN, NaN, NaN, NaN, NaN, 11),
z = c(17, NaN, NaN, NaN, NaN, 12))
do.call(dplyr::coalesce, rev(df))
)。
function demoFromHTML() {
var pdf = new jsPDF('p', 'pt', 'letter');
// source can be HTML-formatted string, or a reference
// to an actual DOM element from which the text will be scraped.
source = $('.JsPDF tbody')[0];
// we support special element handlers. Register them with jQuery-style
// ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
// There is no support for any other type of selectors
// (class, of compound) at this time.
specialElementHandlers = {
// element with id of "bypass" - jQuery style selector
'#bypassme': function (element, renderer) {
// true = "handled elsewhere, bypass text extraction"
return true
}
};
margins = {
top: 80,
bottom: 60,
left: 10,
width: 700
};
// all coords and widths are in jsPDF instance's declared units
// 'inches' in this case
pdf.fromHTML(
source, // HTML string or DOM elem ref.
margins.left, // x coord
margins.top, { // y coord
'width': margins.width, // max width of content on PDF
'elementHandlers': specialElementHandlers
},
function (dispose) {
// dispose: object with X, Y of the last line add to the PDF
// this allow the insertion of new lines after html
pdf.save('Test.pdf');
}, margins);
答案 1 :(得分:0)
或者您可以使用基本R函数na.omit
x = c(NaN, 15, 16, 14, 14, NaN)
y = c(NaN, NaN, NaN, NaN, NaN, 11)
z = c(17, NaN, NaN, NaN, NaN, 12)
dt=data.frame(z=z,x=x,y=y)
unlist(lapply(apply(dt,1,na.omit), `[[`, 1))
[1] 17 15 16 14 14 12
答案 2 :(得分:0)
您还可以使用zoo
包:
df <- rbind(x, y, z)
#it replaces last row with latest non-NA value therefore z always has priority:
xyz <- zoo::na.locf(df)['z',]
#[1] 17 15 16 14 14 12
<强> 数据 强>:
x <- c(NaN, 15, 16, 14, 14, NaN)
y <- c(NaN, NaN, NaN, NaN, NaN, 11)
z <- c(17, NaN, NaN, NaN, NaN, 12)