Displaying 20 results from an estimated 10000 matches similar to: "get list element names within lapply / sapply call"
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
2007 Feb 21
1
Trying to get an apply to work with a list in applying names to tables
I am trying to use apply and a list to supply names
to a set of tables I want to generate. Below is an
example that I hope mimics the larger original
problem.
EXAMPLE
aa <- c( 2,2,1,1,2)
bb <- c(5,6,6,7,4)
aan <- c("yes", "no")
bbn <- c("a", "b", "c", "d")
mynames <- c("abby", "billy")
mylist <-
2018 Mar 13
0
Possible Improvement to sapply
Wouldn't that change how simplify='array' is handled?
> str(sapply(1:3, function(x)diag(x,5,2), simplify="array"))
int [1:5, 1:2, 1:3] 1 0 0 0 0 0 1 0 0 0 ...
> str(sapply(1:3, function(x)diag(x,5,2), simplify=TRUE))
int [1:10, 1:3] 1 0 0 0 0 0 1 0 0 0 ...
> str(sapply(1:3, function(x)diag(x,5,2), simplify=FALSE))
List of 3
$ : int [1:5, 1:2] 1 0 0 0 0 0 1 0 0
2007 Jun 22
2
extract index during execution of sapply
Hi there
During execution of sapply I want to extract the number of times the
function given to supply has been executed. I came up with:
mylist <- list(a=3,b=6,c=9)
sapply(mylist,function(x)as.numeric(gsub("[^0-9]","",deparse(substitute(x)))))
This works fine, but looks quite ugly. I'm sure that there's a more
elegant way to do this.
Any suggestion?
Christian
2018 Mar 13
1
Possible Improvement to sapply
You?re right, it sure does. My suggestion causes it to fail when simplify = ?array?
From: William Dunlap [mailto:wdunlap at tibco.com]
Sent: Tuesday, March 13, 2018 12:11 PM
To: Doran, Harold <HDoran at air.org>
Cc: r-help at r-project.org
Subject: Re: [R] Possible Improvement to sapply
Wouldn't that change how simplify='array' is handled?
> str(sapply(1:3,
2018 Mar 13
0
Possible Improvement to sapply
On 03/13/2018 09:23 AM, Doran, Harold wrote:
> While working with sapply, the documentation states that the simplify argument will yield a vector, matrix etc "when possible". I was curious how the code actually defined "as possible" and see this within the function
>
> if (!identical(simplify, FALSE) && length(answer))
>
> This seems superfluous to me,
2006 Feb 17
1
extracting a element with a name attribute from a list
Hi R users
I like to extract (or collect) a numeric element with a name from a list.
Is there any way to extract just a numeric element without the name attached
to the element.
For example,
>mylist
Mantel-Haenszel chi-squared test with continuity correction
data: table(mydata[, x])
Mantel-Haenszel X-squared = 8.3832, df = 1, p-value = 0.003787
alternative hypothesis: true
2006 Mar 30
3
access list component names with lapply
I have a question regarding accessing the names of list
components when applying a function with lapply.
Here is an example that demonstrates what I'd like to do.
I have a list like this one:
mylist <- list(a=letters[1:10], b=letters[10:1], c=letters[1:3])
Now I would like to append the names of the list components to their
corresponding vectors with the c() function. I thought this
2005 Nov 29
1
Indexing variables within lapply?
Hello
I am using R 2.2.0 with Windows XP.
I've got a five element list object, each element containing two
dataframes of equivalent size.
> str(mylist)
List of 1
$ data1:List of 2
..$ data1a :`data.frame': 77 obs. of 63 variables:
.. ..$ var1 : num [1:77] 0.41375 0.00056 1.43040 1.43528 0.61730 ...
.. ..$ var2 : num [1:77] 1.154 1.686 0.673 0.800 0.760 ...
..
2010 May 17
3
applying quantile to a list using values of another object as probs
Hi r-users,
I have a matrix B and a list of 3x3 matrices (mylist). I want to
calculate the quantiles in the list using each of the value of B as
probabilities.
The codes I wrote are:
B <- matrix (runif(12, 0, 1), 3, 4)
mylist <- lapply(mylist, function(x) {matrix (rnorm(9), 3, 3)})
for (i in 1:length(B))
{
quant <- lapply (mylist, quantile, probs=B[i])
}
But quant
2018 Mar 13
4
Possible Improvement to sapply
While working with sapply, the documentation states that the simplify argument will yield a vector, matrix etc "when possible". I was curious how the code actually defined "as possible" and see this within the function
if (!identical(simplify, FALSE) && length(answer))
This seems superfluous to me, in particular this part:
!identical(simplify, FALSE)
The preceding
2009 Jul 21
0
Accessing list object from within a function as a list element
Hi -
I have a list (call it 'mylist') with the following elements: (i) a
function (call it 'myfunc' and expressed as 'mylist$myfunc') and (ii)
a variable (call it 'myvar' and expressed as 'mylist$myvar'). Since I
use mylist as a pseudo-class (I assign mylist to multiple different R
objects), I would like to access the mylist R object from within the
2004 Feb 11
6
lapply and dynamically linked functions
Hi all,
I'm trying to use lapply on a list with the following command:
out<-lapply(mylist,myfun,par1=p,par2=d) (1)
where
myfun<-function(x,par1,par1) {.....} (2)
now this function is in fact a wrapper for some Fortran code I have
written so I think this might be the problem. When I call lapply() as in
(1) I get the following message:
Error in get(x,
2010 Mar 08
1
lapply and list indexing basics
I have split my original dataframe to generate a list of dataframes each of
which has 3 columns of factors and a 4th column of numeric data.
I would like to use lapply to apply the fitdistr() function to only the 4th
column (x$isi) of the dataframes in the list.
Is there a way to do this or am I misusing lapply?
As a second solution I tried splitting only the numeric data column to yield
a list
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
2010 Jul 09
2
select columns from vector of column names
Hi
I want to extract columns from a data frame using a vector with the desired
column names.
This short example uses the select argument in the subset function to
accomplish what I am trying to do. Is there a better solution?
#names of desired columns
colnames <- c("col1","col3")
#my data
data <-
2012 Nov 09
5
using lapply with recode
Hello:
Forgive me, this is surely a simple question but I can't figure it out, having consulted the help archives and "Data Manipulation With R" (Spector).
I have a list of 11 data frames with one common variable in each (prov). I'd like to use lapply to go through and recode one particular level of that common variable.
I can get the recode to work, but it only returns the
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,
2004 Feb 26
3
my own function given to lapply
Hi
It seems, I just miss something. I defined
treshold <- function(pred) {
if (pred < 0.5) pred <- 0 else pred <- 1
return(pred)
}
and want to use apply it on a vector
sapply(mylist[,,3],threshold)
but I get:
Error in match.fun(FUN) : Object "threshold" not found
thanks for help
cheers
chris
--
Christoph Lehmann <christoph.lehmann at gmx.ch>
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