Displaying 20 results from an estimated 20000 matches similar to: "Curious behavior of $ and lapply"
2009 Jan 25
3
Defining an iterator
Inspired by Rudolf Biczok's query of Fri, Jan 23, 2009 at 1:25 AM, I
tried to implement iteration in a generic way using S4. (Though I am
admittedly still struggling with learning S4.)
> setClass("foo",representation(bar="list"))
[1] "foo"
> x<-new("foo",bar=list(1,2,3))
Given this, I would not expect for(i in x)... to work, since R has no
way
2008 Sep 09
2
make methods work in lapply - remove lapply's environment
I've defined my own version of summary.default,
that gives a better summary for highly skewed vectors.
If I call
summary(x)
the method is used.
If I call
summary(data.frame(x))
the method is not used.
I've traced this to lapply; this uses the new method:
lapply(list(x), function(x) summary(x))
and this does not:
lapply(list(x), summary)
If I make a copy of lapply, WITHOUT the
2017 Nov 15
0
lapply and runif issue?
Hi Bert,
On Tue, Nov 14, 2017 at 8:11 PM, Bert Gunter <bgunter.4567 at gmail.com> wrote:
> Could someone please explain the following? I did check bug reports, but
> did not recognize the issue there. I am reluctant to call it a bug, as it
> is much more likely my misunderstanding. Ergo my request for clarification:
>
> ## As expected:
>
>> lapply(1:3, rnorm, n = 3)
2005 Oct 10
2
problem with lapply(x, subset, ...) and variable select argument
I need to extract identically named columns from several data frames in
a list. the column name is a variable (i.e. not known in advance). the
whole thing occurs within a function body. I'd like to use lapply with a
variable 'select' argument.
example:
tt <- function (n) {
x <- list(data.frame(a=1,b=2), data.frame(a=3,b=4))
for (xx in x) print(subset(xx, select = n))
2013 Mar 13
2
holding argument(s) fixed within lapply
|Hello,
Given a function with several arguments, I would like to perform an
lapply (or equivalent) while holding one or more arguments fixed to some
common value, and I would like to do it in as elegant a fashion as
possible, without resorting to wrapping a separate wrapper for the
function if possible. Moreover I would also like it to work in cases
where one or more arguments to the original
2009 Aug 11
1
Passing a list object to lapply
Hello,
I'm having difficulty passing an object name to a lapply function. Can
somebody tell me the trick to make this work?
#Works
T13702 <- TRACKDATA[["13702.xls"]][["data"]]
min(unlist(lapply(list(T13702), function(x) mdy.date(x[1, 2], x[1, 1],
x[1, 3]))))
16553
#Works
d<-2
assign(paste("T",substr(names(TRACKDATA)[d],1,(nchar(names(TRACKDATA)[d]
2012 Dec 11
2
debug on lapply
Dear R experts,
recently I tried to debug a R function with an internal lapply call.
When debugging I seem not to be able to use the "n" command to debug the
inner function called by lapply.
How could I achieve this?
*For example:*
test <- function( ) {
lapply( 1:3, function( x ) x + 1 )
}
debug( test )
*Start debug:*
> test()
debugging in: test()
debug bei #1:{
2008 Jan 31
2
Pb with lapply()
Hi,
If needed, lapply() tries to convert its first argument into a list
before it starts doing something with it:
> lapply
function (X, FUN, ...)
{
FUN <- match.fun(FUN)
if (!is.vector(X) || is.object(X))
X <- as.list(X)
.Internal(lapply(X, FUN))
}
But in practice, things don't always seem to "work" as suggested by
this code (at least to the
2009 Aug 17
1
how to pass more than one argument to the function called by lapply?
Dear R helpers:
I wonder how to pass more than one argument to the function called by
lapply.
For example,
#R code below ---------------------------
indf <- data.frame(id=I(c('a','b')),y=c(1,10))
#I want to add an addition argument cutoff into the function called by
lapply.
outside.fun <- function(indf, cutoff)
{
unlist(lapply(split(indf, indf[,'id']),
2012 Sep 11
1
lapply with different size lists?
Hello,
I have 2 functions (a and b)
a = function(n) { matrix (runif(n*2,0.0,1), n) }
>
>
> b = function (m, matrix) {
> n=nrow (matrix)
> p=ceiling (n/m)
> lapply (1:p, function (l,n,m) {
> inf = ((l-1)*m)+1
> if (l<p) sup=((l-1)*m)+m
> else sup=n
>
2010 Sep 21
2
lapply version with [ subseting - a suggestion
Dear R developers,
Reviewing my code, I have realized that about 80% of the time in the lapply I
need to access the names of the objects inside the loop.
In such cases I iterate over indexes or names:
lapply(names(x), ... [i]),
lapply(seq_along(x), ... x[[i]] ... names(x)[i] ), or
for(i in seq_along(x)) ...
which is rather inconvenient.
How about an argument to lapply which would specify the
2009 Dec 08
2
could not find function lapply<-
R-help,
I have a list whose elements are data frames.
I want to change the colnames attribute in each element of this list but an error message
comes up:
> lapply(LD_strataNew,function(x) dimnames(x)[[2]][-1]) <- as.roman(1:9)[-6]
Error in lapply(LD_strataNew, function(x) dimnames(x)[[2]][-1]) <- as.roman(1:9)[-6] :
could not find function "lapply<-"
>
2010 Oct 15
1
Downloading file with lapply
I'm still getting familiar with lapply
I have this date sequence
x <- seq(as.Date("01-Jan-2010",format="%d-%b-%Y"), Sys.Date(), by=1) #to
generate series of dates
I want to apply the function for all values of x . so I use lapply (Still a
newbie!)
I wrote this test function
pFun <- function (x) {
print(paste("This is: ",x,sep=""))
}
When I
2004 Nov 19
2
accessing the attributes of a list inside lapply()
Hello R-users,
I have the following problem, that I want to solve efficiently:
I have a named list, for example:
> l <- list(a = 1, b = 3, c = 'asd')
> l
$a
[1] 1
$b
[1] 3
$c
[1] "asd"
I know that I can iterate through it using lapply() function, but I
would also like to able to get the list names or some attributes of l
in the lapply(). For example if I use
2007 Aug 15
1
getting lapply() to work for a new class
Hi,
I would like to get lapply() to work in the natural way on a class I've
defined. As far as I can tell, lapply() needs the class to be coercible
to a list. Even after I define as.list() and as.vector(x, mode="list")
methods, though, I still get an "Error in as.vector(x, "list") : cannot
coerce to vector". What am I doing wrong?
# dummy class
2013 Jan 27
1
lapply and SpatialGridDataFrame error
Hi all, I have a set of 54 files that I need to convert from ASCII grid
format to .shp files to .bnd files for BayesX.
I have the following R code to operate on those files:
library(maptools)
library(Grid2Polygons)
library(BayesX)
library(BayesXsrc)
library(R2BayesX)
readfunct <- function(x)
{
u <- readAsciiGrid(x)
}
modfilesmore <- paste0("MaxFloodDepth_", 1:54,
2013 Apr 17
2
use of names() within lapply()
Dear all,
List g has 2 elements
> names(g)
[1] "2009-10-07" "2012-02-29"
and the list plot
lapply(g, plot, main=names(g))
results in equal plot titles with both list names, whereas distinct titles names(g[1]) and names(g[2]) are sought. Clearly, lapply is passing 'g' in stead of consecutively passing g[1] and then g[2] to process the additional 'main'
2012 Mar 28
2
lapply and paste
I have a list of suffixes I want to turn into file names with extensions.
suff<- c("C1", "C2", "C3")
paste("filename_", suff[[1]], ".ext", sep="")
[1] "filename_C1.ext"
How do I use lapply() on that call to paste()?
What's the right way to do this:
filenames <- lapply(suff, paste, ...)
?
Can I have lapply()
2013 Feb 12
1
recovering from errors with lapply()
Hi all,
I am searching for a way to recover results from lapply() when one or more
values returns an error. I have a written a function that uses tryCatch()
to recover from errors. Here is a very simple example of such a function.
divideBy2<-function(X){
result<-tryCatch(X/2, error = function(e) "An Error Occurred")
return(result)
}
This function appears to work as I expect
2020 Jul 10
2
lapply and vapply Primitive Documentation
The documentation of ?lapply includes:
> lapply and vapply are primitive functions.
However, both evaluate to FALSE in `is.primitive()`:
is.primitive(vapply)
#FALSE
is.primitive(lapply)
#FALSE
It appears that they are not primitives and that the documentation
might be outdated. Thank you for your time and work.
Cole Miller
P.S. During research, my favorite `help()` is