English 中文(简体)
2. 寻找更有效的途径
原标题:Looking for a more efficient ifelse()

While R s ifelse is incredibly handy, it does have a particular shortcoming: in the call ifelse(test, yes, no) all elements of yes and no are evaluated, even those that will be thrown away.

如果你在复杂的数字操作中重新使用这一术语,那么,如果说其功能将并入<条码>。 例如,人们可能已经这样做了。

ifelse(test, f(x, y, z), g(a, b, c))

www.un.org/Depts/DGACM/index_french.htm 页: 1

是否有任何人为<代码>ifelse填写一份替换文件,仅对将保存的<编码>yes/no<>s/code>要素进行评价? 基本内容大致如下:

out <- test
for(i in seq_along(out))
{
    if(test[i]) out[i] <- f(x[i], y[i], z[i])
    else out[i] <- g(a[i], b[i], c[i])
}

但是,没有明显的 lo/效率低下。 如果没有进入R的那部分人,这是否可行?

问题回答

我不认为问题在于<条码>。 我认为,你的问题是,<代码>fg与病媒相比进展缓慢。

您可更改<代码>f和g的电话,以便这些电话仅能在病媒的子体上得到评价。

out <- numeric(length(test))  #or whatever the output type is
out[test] <- f(x[test], y[test], z[test])
out[!test] <- g(x[!test], y[!test], z[!test])

如果<条码>测试<>/代码>的任何内容为<条码>,则需要加以调整。





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签