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
Suppose I have a dataframe in this from
a b c
g 2 3
h 4 5
i 6 7
I want to apply a function to individual elements of column C where the
function value depends on the value of column A
[[alternative HTML version deleted]]
__
R-help@r-project.o
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
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<-c(100,100)
B<-c(200,200,200)
C <- matrix( rnorm(6,mean=0,sd=1), 2, 3)
How can I do it
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
Apologees the novice question. Currently climbing up the learning curve of R.
Suppose I have the following function and the data.frame:
testfun<-function(x=1,y=2) x+y
testframe=data.frame(col1=c(1,2),col2=c(3,4))
When evaluating testfun, I want to use the default value for y (which
is 2) and fo
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
Hi all,
I'm just getting started in R. My problem is the following:
I have a data frame (v1) with lots of production data measurements.
Each row contains a single measurement ('ARI_MIT') with a timestamp. I
want to "lump" the data by months with their mean and standard
deviation.
I have already
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
Hi,
I have a vector
Y1 <-c(8, 11, 7, 5, 6, 3, 6, 3, 3)
and an index
iy <-c(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))
how can I produce the mean, or the sum of the elements specified in the
index iy from the vector Y1?
expecting something like this for th
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
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 know if individuals that
recover their mot
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
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 }
Then create the object fem, which is the difference between the count of a
trait in the left side of the body mi
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
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 step, to yield:
mat2
1 1 1 1 1 0
1 2 2 0 1 1
1 1 0 2 0 1
The
Dear List,
I am using function distCosine from package geosphere to a list of lat/lon
coordinates, and I want to calculate the great circle distance between a
pair of coordinates in the list and all other pairs --- essentially, the
output should be a matrix. I have been able to achieve this with t
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
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
[3,] 0.003547897 0.0063355297 0.0030410542 0.011403953
[4,] 0.004838
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
I'm having a problem where I have to apply a function to a subset of a
variable, where the subset is defined by the n nearest neighbours of a
second variable.
Here's an example applied to the 'iris' dataset:
$ head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1
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:
Hi,
I have a set of (x,y) coordinate pairs that are stored as a list
> my_list
$x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25
$y
[1] -8.0866819 -7.3876052 -6.6849311 -5.9837693 -5.2967432 -4.6525466
[7] -4.0999453 -3.6556190 -3.3076102 -3.0360780 -2.8220465
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
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 columns as
inputs to the function, but with no INDEX stuf
I am sorry. I am not sure if the mail a send before to this list was rejected
because of header (subject).
I've changed it. The first maybe was not appropriate.
I did a function (sec_conop) whose arguments are syndic,
well and wellconop.
sec_conop(syndic='01syndic.txt',well='well-1.csv',wellcon
Hello,
I did a function (sec_conop) whose arguments are syndic,
well and wellconop.
sec_conop(syndic='01syndic.txt',well='well-1.csv',wellconop='well-1.dat');closeAllConnections()
This function takes “well” and “syndic”, matching between
them and then it does some transformations. The result i
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
How can I apply function f, that I get as an argument as in
func <- function(f, ...) {
.
.
.
}
to a list of arguments list(a, b, c) (eg the ... argument of func above)
in order to obtain
f(a, b, c)
Thanks a lot,
Roberto
[[alternative HTML version deleted]]
___
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("
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")),
+ "II"=list("A"=lis
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
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 under consideration and v is a data frame
of values with the same number of columns as u. For example,
v1 <- c(1,2,3)
v2 <- c(3,4,5)
v3 <- c(2,3,4)
v
65 matches
Mail list logo