Displaying 20 results from an estimated 30000 matches similar to: "when to use which apply function?"
2012 Apr 19
2
Trouble with [sv]apply
Friends
I clearly donot understand how sapply and vapply work.
What I have is a function that returns a matrix with an indeterminate
number of rows (some times zero) but a constant number of columns. I
cannot reliably use an apply function to assemble the matrices into a
matrix. I am not sure it is possible.
I can demonstrate the core of my confusion with this simple code.
A.f <-
2012 Mar 01
3
Converting a string vector with names to a numeric vector with names
Not paying close attention to detail, I entered the equivalent of
pstr<-c("b1=200", "b2=50", "b3=0.3")
when what I wanted was
pnum<-c(b1=200, b2=50, b3=0.3)
There was a list thread in 2010 that shows how to deal with un-named vectors, but the same
lapply solution doesn't seem to work here i.e.,
pnum<-lapply(pstr, as.numeric)
or similar vapply
2016 Feb 08
2
inconsistency in treatment of USE.NAMES argument
Hi,
Both vapply() and sapply() support the 'USE.NAMES' argument. According
to the man page:
USE.NAMES: logical; if ?TRUE? and if ?X? is character, use ?X? as
?names? for the result unless it had names already.
But if 'X' has names already and 'USE.NAMES' is FALSE, it's not clear
what will happen to the names. Are they going to propagate to the
result
2016 Feb 11
2
inconsistency in treatment of USE.NAMES argument
Changing the vapply() behavior makes sense in principle. I analyzed
the CRAN code base using the R parser and found 143 instances of
calling vapply with USE.NAMES=FALSE. These would need to be inspected
to understand the consequences of the change.
For reference:
/AzureML/R/datasets.R:226
/BBmisc/R/toRangeStr.R:33
/DBI/R/DBDriver.R:205
/Kmisc/R/str_rev.R:37
/Matrix/R/diagMatrix.R:98
2005 Aug 03
2
using weighted.mean with tapply()
I am trying to calculate the weighted mean for a of 10 deciles and I
get an error:
> decile <- tapply(X=mat$trt1m, INDEX=mat$Rank, FUN=weighted.mean, w=mat$mcap)
Error in FUN(X[[1]], ...) : 'x' and 'w' must have the same length
All three of my inputs have the same length, as shown below, and the
weighted.mean calculation works by itself, just not in tapply()
>
2011 Aug 22
1
Data Frame Indexing
Hello,
I've been dealing with a set of values that contain time stamps and part
of my summary needs to look at just weekend data. In trying to limit the
data I've found a large difference in performance in the way I index a
data frame. I've constructed a minimal example here to try to explain my
observation.
is.weekend <- function(x) {
tm <-
2015 Jan 21
2
reducing redundant work in methods package
Hi all,
The function call series genericForPrimitive -> .findBasicFuns -> .findAll
happens 4400 times while the GenomicRanges package is loading. Each time
.findAll follows a chain of environments to determine that the methods
namespace is the only one that holds a variable called .BasicFunsList. This
accounts for ~10% of package loading time. I'm sure there is some history
to that
2012 Aug 21
1
About matrix manipulation
Dear list,
I'm trying to create a matrix by combining the sites that species occur in
a new matrix with species as rows and sites as columns.
The main matrix is "mat":
mat <- as.data.frame(cbind(sp1=c(rep(0, 5), rep(1, 5)),sp2=sample(c(rep(0,
6),rep(1, 4))),
fac=c(rep("a", 3), rep("b", 3),rep("c", 4))))
The first two columns are species and the
2015 Jan 21
2
reducing redundant work in methods package
Doing it like this:
genericForPrimitive <- function(f, where = topenv(parent.frame()), mustFind
= TRUE) {
ans = .BasicFunsList[[f]]
## this element may not exist (yet, during loading), dom't test null
if(mustFind && identical(ans, FALSE))
stop(gettextf("methods may not be defined for primitive function %s
in this version of R",
2018 Mar 13
1
Possible Improvement to sapply
Could your code use vapply instead of sapply? vapply forces you to declare
the type and dimensions
of FUN's output and stops if any call to FUN does not match the
declaration. It can use much less
memory and time than sapply because it fills in the output array as it goes
instead of calling lapply()
and seeing how it could be simplified.
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Tue,
2003 Jul 18
2
create a vector looping over a frame
Hello,
I have a data.frame
> names(popA)
[1] "Year" "Series" "Age" "WM" "WF" "HM" "HF" "BM"
[9] "BF" "IM" "IF" "AM" "AF" "Yr"
how do i loop over a subset of variables in this frame to create a vector of
2018 Mar 13
0
Possible Improvement to sapply
Quite possibly, and I?ll look into that. Aside from the work I was doing, however, I wonder if there is a way such that sapply could avoid the overhead of having to call the identical function to determine the conditional path.
From: William Dunlap [mailto:wdunlap at tibco.com]
Sent: Tuesday, March 13, 2018 12:14 PM
To: Doran, Harold <HDoran at air.org>
Cc: Martin Morgan <martin.morgan
2011 Aug 08
1
Overwriting imported function in another package
I am running into a limitation of the grid::grid.newpage function, for
which I would like to overwrite this function with a slightly modified
one. Hopefully this is a temporary working solution until the package
gets updated. I found a way to overwrite the function in the
package:grid namespace. However, lattice imports grid rather than
depending on it. So I need a way to overwrite this imported
2018 Mar 13
2
Possible Improvement to sapply
FYI, in R devel (to become 3.5.0), there's isFALSE() which will cut
some corners compared to identical():
> microbenchmark::microbenchmark(identical(FALSE, FALSE), isFALSE(FALSE))
Unit: nanoseconds
expr min lq mean median uq max neval
identical(FALSE, FALSE) 984 1138 1694.13 1218.0 1337.5 13584 100
isFALSE(FALSE) 713 761 1133.53 809.5 871.5
2014 Dec 17
2
vapply definition question
vapply <- function(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE)
{
FUN <- match.fun(FUN)
if(!is.vector(X) || is.object(X)) X <- as.list(X)
.Internal(vapply(X, FUN, FUN.VALUE, USE.NAMES))
}
This is an implementor question. Basically, what happened to the '...'
args in the call to the .Internal? cf lapply:, where the ... is passed.
lapply <- function (X, FUN, ...)
2014 Apr 12
1
vapply confusion
The following code seems to contain an inconsistency in
the behavior of vapply(). Am I missing something here?
## This function assumes v is a 3d vector, beta a scalar.
f3d <- function(v,beta) { v+beta }
## This expression applies f3d to a vector of scalars, and
## specifies the template 'array(10,3)' for the return value.
dat <- vapply(seq(0,1,length=10), function(beta) {
2005 Jun 15
2
need help on computing double summation
Dear helpers in this forum,
This is a clarified version of my previous
questions in this forum. I really need your generous
help on this issue.
> Suppose I have the following data set:
>
> id x y
> 023 1 2
> 023 2 5
> 023 4 6
> 023 5 7
> 412 2 5
> 412 3 4
> 412 4 6
> 412 7 9
> 220 5 7
> 220 4 8
> 220 9 8
> ......
>
Now I want to compute the
2018 Mar 13
2
Possible Improvement to sapply
Martin
In terms of context of the actual problem, sapply is called millions of times because the work involves scoring individual students who took a test. A score for student A is generated and then student B and such and there are millions of students. The psychometric process of scoring students is complex and our code makes use of sapply many times for each student.
The toy example used
2015 Aug 13
2
[lld] Alias in COFF short import library.
>
> If you want to define an alias symbol "bar" to "foo" (which is an
> extension you want to provide), one way is to create an object file that
> defines "bar" and "__imp_bar" as aliases to "foo" and "__imp_foo",
> respectively, and add that object file to the import library. As a result,
> the import library file
2013 Mar 27
1
Passing arguments between apply and l(s)apply functions vs. nested for loop
Hi R community,
I have a question concerning passing arguments between apply and lapply? Or maybe, once my problem is explained, the question is really about how to best transform my nested for loops into list/matrix operations; I am just beginning this transformation away from nested for loops, so I beg of you to have some lenience regarding my ignorance.
Part I:
I used a set of nested for