Displaying 20 results from an estimated 6000 matches similar to: "lapply version with [ subseting - a suggestion"
2011 Jul 30
2
Applying ifelse() on different functions
Dear all, I am looking for some procedure to apply 'ifelse' condition on function. I have created an alternative to lapply() function with exactly same set of arguments named lapply1(), however with different internal codes.
Therefore I want something like, if (some condition) then call lapply1() otherwise lapply() function. Ofcourse I can create a complete if()... else()... like of
2013 Jan 05
2
lapply (and friends) with data.frames are slow
Hey guys,
I noticed something curious in the lapply call. I'll copy+paste the
function call here because it's short enough:
lapply <- function (X, FUN, ...)
{
FUN <- match.fun(FUN)
if (!is.vector(X) || is.object(X))
X <- as.list(X)
.Internal(lapply(X, FUN))
}
Notice that lapply coerces X to a list if the !is.vector || is.object(X)
check passes.
Curiously,
2010 Mar 31
1
for loop; lm() regressions; list of vectors - lapply - accolades and square brackets??
Hello and thank you both for your answers!
Dennis, I tried to simply run
lm(a ~ b)
after re-importing "a" as a matrix, but I get the following error message:
Error in model.frame.default(formula = a ~ b, drop.unused.levels = TRUE) :
invalid type (list) for variable 'a'
so maybe I have to specify something in the arguments? What do you think?
David,
I tried your syntax as
2017 Aug 14
0
recursive lapply and keeping track of data
You could replace your 'depth' argument with one that shows where in the
original data you are at:
leaf.func <-
function(data, where) {
if(is.null(data)) stop("Null data at ", deparse(where))
return(mean(data))
}
visit.level <-
function(data, where = integer()) {
if (length(where) == 2) {
return(leaf.func(data, where))
} else
2010 Jun 03
2
lapply or data.table to find a unit's previous transaction
I have a dataset of property transactions that includes the
transaction ID (TranID), property ID (UnitID), and transaction date
(TranDt). I need to create a data frame (or data table) that includes
the previous transaction date, if one exists.
This is an easy problem in SQL, where I just run a sub-query, but I'm
trying to make R my one-stop-shopping program. ?The following code
works on a
2008 Jul 11
1
Suggestion: 20% speed up of which() with two-character mod
Hi,
by replacing 'll' with 'wh' in the source code for base::which() one
gets ~20% speed up for *named logical vectors*.
CURRENT CODE:
which <- function(x, arr.ind = FALSE)
{
if(!is.logical(x))
stop("argument to 'which' is not logical")
wh <- seq_along(x)[ll <- x & !is.na(x)]
m <- length(wh)
dl <- dim(x)
if (is.null(dl)
2010 Oct 24
2
"$<-" fails (invalid subscript type 'language')
This might be just beyond of my understanding of how assignment works in R, but
the documentation does not say anything about:
> tv <- c(a="dsf", b="sss")
> tl <- list(232)
> `$<-`(tl, tv[[1]], "sdfdsfdsfsd")
Error: invalid subscript type 'language'
This happens even before the method is dispatched. I can not handle the
"name"
2010 Feb 25
3
behavior of seq_along
I'm trying to understand the behavior of seq_along in the following example:
x <- 1:5; sum(x)
y <- 6:10; sum(y)
data <- c(x,y)
S <- sum( data[seq_along(x)] )
S
T <- sum( data[seq_along(y)] )
T
Why is T != sum(y) ?
2007 Apr 03
1
Behavior of seq_along (was: Create a new var reflecting the order of subjects in existing var)
I am moving this from r-help to r-devel. Based on offline communications
with Jim, suppose dat is defined as follows:
set.seed(123)
dat <- data.frame(ID= c(rep(1,2),rep(2,3), rep(3,3), rep(4,4),
rep(5,5)), var1 =rnorm(17, 35,2), var2=runif(17,0,1))
# Then this ave call works as expected:
ave(dat$ID, dat$ID, FUN = function(x) seq_along(x))
# but this apparently identical calculation
2009 Jul 09
1
bug in seq_along
Using the IRanges package from Bioconductor and somewhat recent R-2.9.1.
ov = IRanges(1:3, 4:6)
length(ov) # 3
seq(along = ov) # 1 2 3 as wanted
seq_along(ov) # 1!
I had expected that the last line would yield 1:3. My guess is that
somehow seq_along don't utilize that ov is an S4 class with a length
method.
The last line of the *Details* section of ?seq has a typeo. Currently
it is
2012 Mar 14
1
[ESS] completion in [] (R internal completion fails)
Hello,
I am forwarding this from ESS mailing list, as it's a failure of
internal R completion system:
This fails:
utils:::.assignLinebuffer('iris[iris$Spec')
utils:::.assignEnd(15)
utils:::.guessTokenFromLine()
utils:::.completeToken()
utils:::.retrieveCompletions() ## -> [1] "iris[iris$Spec"
This works
utils:::.assignLinebuffer('iris[ iris$Spec') # note the
2003 Oct 15
2
Subseting in a 3D array
Hi!
I have a 3d array:
> dim(ib5km15.dbc)
[1] 190 241 19
and a set of positions to extract:
> ib5km.lincol.random[1:3,]
[,1] [,2]
[1,] 78 70
[2,] 29 213
[3,] 180 22
Geting the values of a 2D array
for that set of positions would
be:
> ima <- ib5km15.dbc[,,1]
> ima[ib5km.lincol.random[1:10,]]
but don't find the way for the case
of the 3D array:
>
2012 Nov 30
1
xts indexed with Date class
Hi
I see a changed behaviour in xts indexed on class Date in the latest
versions, versus 2.
It seems to be related to changes to/from daylight savings time,
happens those weekends.
Is it not intended that class Date be used like this, or is this new
behaviour incorrect?
Giles
Example:
> a<-as.Date(15423:15426)
> x<-xts(seq_along(a),a)
> print(x)
[,1]
2012-03-24
2012 Jan 06
1
seq_along and rep_along
Hi all,
A couple of ideas for improving seq_along:
* It would be really useful to have a second argument dim:
seq_along(mtcars, 1)
seq_along(mtcars, 2)
# equivalent to
seq_len(dim(mtcars)[1])
seq_len(dim(mtcars)[2])
I often find myself wanting to iterate over the rows or column of a
data frame, and there isn't a particularly nice idiom if you want to
avoid problems
2019 May 16
3
print.<strorageMode>() not called when autoprinting
In R-3.6.0 autoprinting was changed so that print methods for the storage
modes are not called when there is no explicit class attribute. E.g.,
% R-3.6.0 --vanilla --quiet
> print.function <- function(x, ...) { cat("Function with argument list ");
cat(sep="\n ", head(deparse(args(x)), -1)); invisible(x) }
> f <- function(x, ...) { sum( x * seq_along(x) ) }
2009 Nov 22
2
Help with indexing
Dear R Helpers,
I am missing something very elementary here, and I don't seem to get it from the help pages of the ave, seq and seq_along functions, so I wonder if you could offer a quick help.
To use an example from an earlier post on this list, I have a dataframe of this kind:
dat = data.frame(name = rep(c("Mary", "Sam", "John"), c(3,2,4)))
dat$freq =
2012 Nov 20
2
correct function formation in R
Dear list!
?
I have question of?'correct function formation'. Which function (fun1 or fun2; see below) is written more correctly? Using ''structure'' as output or creating empty ''data.frame'' and then transform it as output? (fun1 and fun1 is just for illustration).
?
Thanks a lot, OV
?
code:
input <- data.frame(x1 = rnorm(20), x2 = rnorm(20), x3 =
2012 Dec 23
1
correction needed in codes
Dear useRs,while trying to plot the yearly curves of 1000 stations and overlapping each set of curves with mean curve and then saving it automatically in a pdf file, i tried the following commands
>Path = "C:\\R\\003.pdf">pdf(file=Path) for (i in seq(1:1000)
>a<-lapply(seq_along(tcp), function(x) tcp[[x]][,-1])
>b<-lapply(seq_along(a), function(a)
2019 May 21
2
print.<strorageMode>() not called when autoprinting
It also is a problem with storage.modes "integer" and "complex":
3.6.0> print.integer <- function(x,...) "integer vector"
3.6.0> 1:10
[1] 1 2 3 4 5 6 7 8 9 10
3.6.0> print(1:10)
[1] "integer vector"
3.6.0>
3.6.0> print.complex <- function(x, ...) "complex vector"
3.6.0> 1+2i
[1] 1+2i
3.6.0>
2008 Jul 01
1
[.data.frame speedup
Below is a version of [.data.frame that is faster
for subscripting rows of large data frames; it avoids calling
duplicated(rows)
if there is no need to check for duplicate row names, when:
i is logical
attr(x, "dup.row.names") is not NULL (S+ compatibility)
i is numeric and negative
i is strictly increasing
"[.data.frame" <-
function (x, i, j,