Press send too soon? This is not actually a question.
Do read the Posting Guide... for one thing you need to post in plain text
because the automatic text conversion tends to mess up what you send if it is
HTML.
On June 5, 2020 12:02:44 AM PDT, TJUN KIAT TEO wrote:
>Suppose I have a dataframe
s A&M University
> College Station, TX 77840-4352
>
> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Jeff Newmiller
> Sent: Wednesday, December 16, 2015 7:01 PM
> To: Matteo Richiardi; r-help@r-project.org
> Subject: Re: [R]
f Newmiller
Sent: Wednesday, December 16, 2015 7:01 PM
To: Matteo Richiardi; r-help@r-project.org
Subject: Re: [R] Applying a function to a matrix using indexes as arguments
Would
outer( A, B, `*` ) / C
do the trick for you?
--
Sent from my phone. Please excuse my brevity.
On December 16,
Hi David,
that's a great answer, thanks so much. I imagined apply() was involved in
the solution, but I was unable to find how myself. Thanks again.
Matteo
On 17 December 2015 at 01:37, David Winsemius
wrote:
>
> > On Dec 16, 2015, at 5:34 PM, David Winsemius
> wrote:
> >
> >
> >> On Dec 16, 20
> On Dec 16, 2015, at 5:34 PM, David Winsemius wrote:
>
>
>> On Dec 16, 2015, at 4:18 PM, Matteo Richiardi
>> wrote:
>>
>> I have to evolve each element of a matrix W
>>
>> W <- matrix(0,2,3)
>>
>> according to some function which uses the indices of the matrix [i,j] as
>> arguments:
>> w.
> On Dec 16, 2015, at 4:18 PM, Matteo Richiardi
> wrote:
>
> I have to evolve each element of a matrix W
>
> W <- matrix(0,2,3)
>
> according to some function which uses the indices of the matrix [i,j] as
> arguments:
> w.fun = function(i,j) {
> return A[i]*B[j]/(C[i,j])
> }
>
> where
> A<-
Would
outer( A, B, `*` ) / C
do the trick for you?
--
Sent from my phone. Please excuse my brevity.
On December 16, 2015 4:41:13 PM PST, Matteo Richiardi
wrote:
>My problem is of course more complicated, and is obviously not a
>homework.
>I just wanted to provide a minimal working example. Y
My problem is of course more complicated, and is obviously not a homework.
I just wanted to provide a minimal working example. You can replace the
matrix C with a matrix containing any number, for what matters. Btw,
because numbers are extracted from a Gaussian distribution, the likelihood
that you
This calculation divides by values centered around zero. The only context that
I can think of that would require such silliness is a homework problem, and
this list has a no-homework policy. If not, then mentioning the theory you are
applying might help someone point you at an existing function
On Jun 9, 2012, at 1:02 PM, Onur Uncu wrote:
Thank you Michael. One follow up question: In the solution using
apply(), why are we feeding x[2] inside testfun instead of x[[2]]?
Aren't we supposed to feed the vector of values to the function, which
is achieved by x[[2]]?
`apply` sends the colu
Thank you Michael. One follow up question: In the solution using
apply(), why are we feeding x[2] inside testfun instead of x[[2]]?
Aren't we supposed to feed the vector of values to the function, which
is achieved by x[[2]]?
Thnks.
On Sat, Jun 9, 2012 at 3:38 PM, R. Michael Weylandt
wrote:
> N
No worries -- it's an important question and it introduces you to one
of the most important R idioms. [And you get bonus points for having a
well formed question!]
You're probably looking for something like this:
apply(testframe, 1, function(x) testfun(x[2]))
Which goes row-by-row and substitute
Hello Steve,
thank you for your reply. You're right, just before I read your post
I'd found aggregate() and indeed it brought me a long way towards my
goal.
I've been a C programmer for 20+ years, and I'm fairly firm in SQL, so
to understand R I need to lose my scalar and row (record) oriented
th
You can also get the official getting started tutorial by typing
help.start() at the command prompt.
Michael
On Thu, Apr 12, 2012 at 2:52 PM, steven mosher wrote:
> Welcome to R and the list.
>
> Others may suggest books ( Nutshell was my first ) but first there are
> some things that will h
Welcome to R and the list.
Others may suggest books ( Nutshell was my first ) but first there are
some things that will help you
both in programming and getting help on the list.
You should post executable code in your question. So, build a toy example
of the data.frame you have
and show what
Your code for iy doesn't work as providedI'll assume you meant this instead:
iy <- list(c(1, 2),c(1, 2), c(1, 2, 3, 4), c(2, 3, 5), c(4), c(5, 6,
7), c(7, 8, 9))
Then
sapply(iy, function(x) sum(Y1[x]))
Michael
On Sat, Mar 10, 2012 at 5:01 PM, aldi wrote:
> Hi,
>
> I have a vector
> Y1 <-c
Sorry, in the function body, NO 'tab1', use 'x' only:
recovery.rate <- function(x) unlist(lapply(split(x, x[,2]), function(x)
mean(x[[4]]=="y")))
The error is because 'tab1' existed in the environment and the function
would find it.
This time, tested after removing 'tab1'.
Rui Barradas
--
Vi
Joanie Van De Walle wrote
>
> Hi,
>
>I need help writing a function
>
>I capture seal pups mutliple times during the lactation season in order
> to
>monitor their growth rate. When I release them, the recovery
> (mother-pup)
>time is not the same for all individuals. I want to k
First, it may be a good idea to use 1 for âyesâ and 0 for ânoâ in the
motherrecovery column.
Then if you name your table e.g tab1, you may try something like this..
sum(tab1[,2]==1*tab1[,4])/sum(tab1[,2]==1)
2011/12/23 Joanie Van De Walle
>
> Hi,
>
> I need help writing a function
That's it.
thanks very much, I save hours if work qith your reply
-
Mario Garrido Escudero
PhD student
Dpto. de Biología Animal, Ecología, Parasitología, Edafología y Qca. Agrícola
Universidad de Salamanca
--
View this message in context:
http://r.789695.n4.nabble.com/Applying-a-function-to-a
Or just include is.na=TRUE in the definition of kurtosis():
kurtosis<-function(x) {
m4<-sum((x-mean(x,na.rm=TRUE))^4,na.rm=TRUE)/length(x)
s4<-var(x,na.rm=TRUE)^2
m4/s4 - 3 }
HTH
Stephan
Am 29.05.2011 11:34, schrieb Jim Holtman:
kurtosis(fem[!is.na(fem)])
Sent from my iPad
On May 29,
kurtosis(fem[!is.na(fem)])
Sent from my iPad
On May 29, 2011, at 4:54, gaiarrido wrote:
> Here´s my problem,
> i have developed the function kurtosis (using R-book as a guide) with this
> commands:
> kurtosis<-function(x) {
> m4<-sum((x-mean(x))^4)/length(x)
> s4<-var(x)^2
> m4/s4 - 3 }
>
> Th
On Apr 22, 2011, at 2:38 PM, Richard M. Heiberger wrote:
I like David's answer and it can be made much faster.
I show three refinements, each faster than the preceding one.
Rich
> system.time(for (i in 1:1000)
+ mat[seq(1, nrow(mat), by=2), ]+mat[seq(2, nrow(mat), by=2), ]
+ )
user syst
I like David's answer and it can be made much faster.
I show three refinements, each faster than the preceding one.
Rich
> system.time(for (i in 1:1000)
+ mat[seq(1, nrow(mat), by=2), ]+mat[seq(2, nrow(mat), by=2), ]
+ )
user system elapsed
0.180.000.19
>
> system.time(for (i in
On Apr 22, 2011, at 12:13 PM, Christine SINOQUET wrote:
Hello,
mat1 only consists of 0s and 1s:
0 0 1 0 0 0
1 1 0 1 1 0
1 1 1 0 1 0
0 1 1 0 0 1
1 0 0 1 0 0
0 1 0 1 0 1
N = 3
M = 6
I would like to "compress" mat1 every two rows, applying summation
over the two rows (per column), at each ste
A example:
set.seed(123)
m <- matrix(rnorm(12), 3)
myParam <- rowMeans(m)
sweep(m, 1, myParam, FUN = "-")
This subtracts the myParam value in each column.
On Fri, Jan 29, 2010 at 6:59 PM, anna wrote:
>
> I had a quick look at the sweep function but didn't find a solution in it, I
> am going to
I had a quick look at the sweep function but didn't find a solution in it, I
am going to look at it again then.
-
Anna Lippel
--
View this message in context:
http://n4.nabble.com/Applying-a-function-on-each-columns-of-a-matrix-tp1415660p1428193.html
Sent from the R help mailing list archiv
See sweep function
On Fri, Jan 29, 2010 at 2:32 PM, anna wrote:
>
> Hello everyone, I have the following matrix
> [,1] [,2] [,3] [,4]
> [1,] 0.002809706 0.0063856960 0.0063856960 0.011749681
> [2,] 0.004893124 0.0023118418 -0.0005122951 -0.014646465
>
But then I would have to make a loop right?
-
Anna Lippel
--
View this message in context:
http://n4.nabble.com/Applying-a-function-on-each-columns-of-a-matrix-tp1415660p1415743.html
Sent from the R help mailing list archive at Nabble.com.
__
R-h
Hello,
You could do something along the following lines:
sapply( 1:ncol( my.matrix ),
function( i )
my.foo( my.matrix[,i], my.parm[i]
)
Best regards,
Carlos J. Gil Bellosta
http://www.datanalytics.com
anna wrote:
Hello everyone, I have the following matrix
On Fri, 30 Oct 2009 10:28:49 +0100 Karl Ove Hufthammer
wrote:
> $ (pos=which(order(abs(iris$Sepal.Length-x)) %in% 2:6))
This should of course be:
(pos=order(abs(iris$Sepal.Length-x))[2:6])
--
Karl Ove Hufthammer
__
R-help@r-project.org mailing list
Milton,
Thanks, the answer was in fact as simple as you pointed out. I was thinking
more complicated than needed!
Kavitha
On Sun, Jun 28, 2009 at 1:11 AM, milton ruser wrote:
> Hi Kavitha,
>
> I must confess you that I not understood well what you are looking for.
> But..
>
> mylist<-list(x=1:2
Hi Kavitha,
I must confess you that I not understood well what you are looking for.
But..
mylist<-list(x=1:25, y=runif(n=25))
plot(mylist, type="n")
points(mylist, type="p", col=mylist$x)
Hth,
miltinho
On Sun, Jun 28, 2009 at 1:03 AM, Kavitha Venkatesan <
kavitha.venkate...@gmail.com> wrote:
On Dec 16, 2008, at 6:00 PM, glenn roberts wrote:
Another Newbie Question sorry:
I am trying to apply a function a dataframe and could use some help:
Assuming, dim(df) = (10,2) say, I would like to apply a function
that looks
at each row in turn and returns a list (dim =(10,1)) using the
Try this:
func <- function(f, ...) f(...)
# e.g.
func(sin, 0) # same as sin(0)
func(max, 1, 2) # same as max(1, 2)
On Fri, Nov 7, 2008 at 5:21 AM, <[EMAIL PROTECTED]> wrote:
> How can I apply function f, that I get as an argument as in
>
> func <- function(f, ...) {
> .
> .
> .
> }
>
> to a lis
On 11/7/2008 5:40 AM, baptiste auguie wrote:
perhaps something like,
func <- function(f, ...) {
do.call(f, ...)
}
func(rnorm, list(n=3, mean=2, sd=3))
Alternatively, if the caller doesn't want to put the args in a list,
your func can do it:
func2 <- function(f, ...) {
do.call(f, list(..
perhaps something like,
func <- function(f, ...) {
do.call(f, ...)
}
func(rnorm, list(n=3, mean=2, sd=3))
baptiste
On 7 Nov 2008, at 10:21, [EMAIL PROTECTED] wrote:
How can I apply function f, that I get as an argument as in
func <- function(f, ...) {
.
.
.
}
to a list of arguments list
Wrap each element in an environment, flatten that and then
extact the element in each environment. (Be sure not to use
an old version of R since sufficiently far back R had a bug when
environments were stored in lists that was since fixed.)
L <- rapply(test.list, function(el) environment(), how =
Prof Brian Ripley wrote:
>
> See ?rapply
Golly, the things one learns when least expecting it.
Jim
__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guid
Hi,
thanks a lot for your help. Somehow rapply had escaped my notice. I
also have a follow-up question on that. I would like to "flatten" my
output list to a list with only one level. Option "unlist" in rapply
returns a character vector, in my example:
> rapply(test.list, rev, how="unlist")
Georg Otto wrote:
Hi,
I have a question about applying a function recursively through a
list. Suppose I have a list where the different elements have
different levels of recursion:
...
I understand that with a fixed number of recursion levels one can use
lapply() in a nested way, but what i
on 06/11/2008 10:51 AM Georg Otto wrote:
Hi,
I have a question about applying a function recursively through a
list. Suppose I have a list where the different elements have
different levels of recursion:
test.list<-list("I"=list("A"=c("a", "b", "c"), "B"=c("d", "e", "f"), "C"=c("g", "h",
"i"
See
?rapply
for your example
rapply( test.list, rev, how='replace' )
HTH,
Chuck
On Wed, 11 Jun 2008, Georg Otto wrote:
Hi,
I have a question about applying a function recursively through a
list. Suppose I have a list where the different elements have
different levels of
See ?rapply
On Wed, 11 Jun 2008, Georg Otto wrote:
Hi,
I have a question about applying a function recursively through a
list. Suppose I have a list where the different elements have
different levels of recursion:
test.list<-list("I"=list("A"=c("a", "b", "c"), "B"=c("d", "e", "f"), "C"=c("
One thing is needed:
names(v) <- names(u)
PS:Thanks Mark Leeds
On 22/02/2008, Henrique Dallazuanna <[EMAIL PROTECTED]> wrote:
> Try this:
>
> sapply(names(u), function(x)u[x][u[x] >=min(v[x]) & u[x] <= max(v[x])])
>
>
> On 21/02/2008, dxc13 <[EMAIL PROTECTED]> wrote:
> >
> > useR's,
> >
You can do:
lapply2(u, v, function(u,v) u[inRange(u, range(v))])
using two functions 'lapply2' and 'inRange' defined at bottom.
This basically does:
lapply(seq(along=u),
function(i, U, V){
u <- U[[i]]
v <- V[[i]]
u
Try this:
sapply(names(u), function(x)u[x][u[x] >=min(v[x]) & u[x] <= max(v[x])])
On 21/02/2008, dxc13 <[EMAIL PROTECTED]> wrote:
>
> useR's,
>
> I want to apply this function to the columns of a data frame:
>
> u[u >= range(v)[1] & u <= range(v)[2]]
>
> where u is the n column data frame und
47 matches
Mail list logo