Re: [R] apply

2024-10-04 Thread Richard O'Keefe
> x <- runif(10)
> y <- runif(10)
> cov(cbind(x,y))
  x  y
x 0.1205034 0.02642830
y 0.0264283 0.09945432

I understand wanting to calculate covariance matrices.
What I DON'T understand is wanting to do it using apply().
(And that's what looked like a homework problem, it's so artificial.)


On Sat, 5 Oct 2024 at 01:41, Ivan Krylov via R-help
 wrote:
>
> В Fri, 4 Oct 2024 20:28:01 +0800
> Steven Yen  пишет:
>
> > Suppose I have two vectors, x and y. Is there a way
> > to do the covariance matrix with “apply”.
>
> There is no covariance matrix for just two samples (vectors) 'x' and
> 'y'. You can only get one covariance value for these.
>
> If you had a pair of vectors of _random variates_, the situation would
> be different, but those are more abstract mathematical concepts. You
> would need to sample every random variate, producing two matrices 'x'
> and 'y' in order to calculate a covariance matrix for them.
>
> --
> Best regards,
> Ivan
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply

2024-10-04 Thread Rolf Turner
On Fri, 04 Oct 2024 11:16:45 -0700
Jeff Newmiller via R-help  wrote:

> Even if this is not a homework question, it smells like one. If you
> read the Posting Guide it warns you that homework is off-topic, so
> when you impose an arbitrary constraint like "must use specific
> unrelated function" we feel like you are either cheating or wasting
> our time, and it is up to you to explain why we should follow you
> down this rabbit hole, keeping in mind that statistics theory per-se
> is also off-topic here. You have yet to explain why you want to do
> this the hard way.

Well put, Jeff.

cheers,

Rolf

> On October 4, 2024 2:13:26 AM PDT, Steven Yen 
> wrote:
> >Pardon me!!!
> >
> >What makes you think this is a homework question? You are not
> >obligated to respond if the question is not intelligent enough for
> >you.
> >
> >I did the following: two ways to calculate a covariance matrix but 
> >wonder how I might replicate the results with "apply". I am not too 
> >comfortable with the online do of apply.
> >
> >> set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449
> >> 0.80940115 
> >0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2]
> >[,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 
> >0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 
> >0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1]
> >[,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,]
> >0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805
> >-0.00273267965 0.0151896601 >
> >
> >On 10/4/2024 4:57 PM, Uwe Ligges wrote:
> >> Homework questions are not answered on this list.
> >>
> >> Best,
> >> Uwe Ligges
> >>
> >>
> >>
> >> On 04.10.2024 10:32, Steven Yen wrote:
> >>> The following line calculates standard deviations of a column
> >>> vector:
> >>>
> >>> se<-apply(dd,1,sd)
> >>>
> >>> How can I calculate the covariance matrix using apply? Thanks.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply

2024-10-04 Thread Jeff Newmiller via R-help
Even if this is not a homework question, it smells like one. If you read the 
Posting Guide it warns you that homework is off-topic, so when you impose an 
arbitrary constraint like "must use specific unrelated function" we feel like 
you are either cheating or wasting our time, and it is up to you to explain why 
we should follow you down this rabbit hole, keeping in mind that statistics 
theory per-se is also off-topic here. You have yet to explain why you want to 
do this the hard way.

On October 4, 2024 2:13:26 AM PDT, Steven Yen  wrote:
>Pardon me!!!
>
>What makes you think this is a homework question? You are not obligated 
>to respond if the question is not intelligent enough for you.
>
>I did the following: two ways to calculate a covariance matrix but 
>wonder how I might replicate the results with "apply". I am not too 
>comfortable with the online do of apply.
>
>> set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115 
>0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2] [,3] 
>[1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 
>0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 
>0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1] [,2] 
>[,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 
>0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 
>0.0151896601 >
>
>On 10/4/2024 4:57 PM, Uwe Ligges wrote:
>> Homework questions are not answered on this list.
>>
>> Best,
>> Uwe Ligges
>>
>>
>>
>> On 04.10.2024 10:32, Steven Yen wrote:
>>> The following line calculates standard deviations of a column vector:
>>>
>>> se<-apply(dd,1,sd)
>>>
>>> How can I calculate the covariance matrix using apply? Thanks.
>>>
>>> __
>>> [email protected] mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide 
>>> https://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>   [[alternative HTML version deleted]]
>
>__
>[email protected] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

-- 
Sent from my phone. Please excuse my brevity.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply

2024-10-04 Thread Steven Yen
Pardon me!!!

What makes you think this is a homework question? You are not obligated 
to respond if the question is not intelligent enough for you.

I did the following: two ways to calculate a covariance matrix but 
wonder how I might replicate the results with "apply". I am not too 
comfortable with the online do of apply.

> set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115 
0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2] [,3] 
[1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 
0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 
0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1] [,2] 
[,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 
0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 
0.0151896601 >

On 10/4/2024 4:57 PM, Uwe Ligges wrote:
> Homework questions are not answered on this list.
>
> Best,
> Uwe Ligges
>
>
>
> On 04.10.2024 10:32, Steven Yen wrote:
>> The following line calculates standard deviations of a column vector:
>>
>> se<-apply(dd,1,sd)
>>
>> How can I calculate the covariance matrix using apply? Thanks.
>>
>> __
>> [email protected] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide 
>> https://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply

2024-10-04 Thread Rui Barradas via R-help

Hello,

You don't need apply, covariance calculations are so frequent that R or 
any other statistics package already has pre-programmed functions.

This time with two vectors x and y.



set.seed(123)
n <- 3
x <- rnorm(n)
y <- rnorm(n)

# the two main diagonal values
var(x)
#> [1] 1.300025
var(y)
#> [1] 0.8704518
# the secondary diagonal values
cov(x, y)
#> [1] 1.056885
# cov(x, y) == cov(y, x)
cov(y, x)
#> [1] 1.056885

# and the result you are after
# (the covariance matrix is symmetric)
cov(cbind(x, y))
#>  x y
#> x 1.300025 1.0568845
#> y 1.056885 0.8704518



Hope this helps,

Rui Barradas


Às 13:28 de 04/10/2024, Steven Yen escreveu:
OK. Thanks to all. Suppose I have two vectors, x and y. Is there a way 
to do the covariance matrix with “apply”. The matrix I need really 
contains the deviation products divided by the degrees of freedom (n-1). 
That is, the elements


(1,1), (1,2),...,(1,n)

(2,1), (2,2),, (2,n)



(n,1),(n,2),...,(n,n).


Hello,

This doesn't make sense, if you have only one vector you can estimate 
its variance with


var(x)


but there is no covariance, the joint variance of two rv's. "co" or 
joint with what if you have only x?
Note that the variance of x[1] or any other vector element is zero, 
it's only one value therefore it does not vary. A similar reasonong 
can be applied to cov(x[1], x[2]), etc.


Hope this helps,

Rui Barradas

Às 12:14 de 04/10/2024, Steven Yen escreveu:

Hello

I have a vector:

set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749 
1.55870831 I like to create a matrix with elements containing 
variances and covariances of x. That is var(x[1]) cov(x[1],x[2]) 
cov(x[1],x[3]) cov(x[2],x[1]) var(x[2]) cov(x[2],x[3]) cov(x[3],x[1]) 
cov(x[3],x[2]) var(x[3]) And I like to do it with "apply". Thanks.


On 10/4/2024 6:35 PM, Rui Barradas wrote:

Hello,

If you have a numeric matrix or data.frame, try something like

cov(mtcars)

Hope this helps,

Rui Barradas


Às 10:15 de 04/10/2024, Steven Yen escreveu:

On 10/4/2024 5:13 PM, Steven Yen wrote:


Pardon me!!!

What makes you think this is a homework question? You are not
obligated to respond if the question is not intelligent enough for 
you.


I did the following: two ways to calculate a covariance matrix but
wonder how I might replicate the results with "apply". I am not too
comfortable with the online documentation of "apply".

set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 
0.80940115

0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2]
[,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598
0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965
0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1]
[,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,]
0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805
-0.00273267965 0.0151896601 >
On 10/4/2024 4:57 PM, Uwe Ligges wrote:

Homework questions are not answered on this list.

Best,
Uwe Ligges



On 04.10.2024 10:32, Steven Yen wrote:
The following line calculates standard deviations of a column 
vector:


se<-apply(dd,1,sd)

How can I calculate the covariance matrix using apply? Thanks.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting- 
guide.html

and provide commented, minimal, self-contained, reproducible code.









--
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença 
de vírus.
www.avg.com

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply

2024-10-04 Thread Ebert,Timothy Aaron
Why must the answer use apply? It feels like there are elements of the problem 
that are not explained.

-Original Message-
From: R-help  On Behalf Of Ben Bolker
Sent: Friday, October 4, 2024 8:45 AM
To: [email protected]
Subject: Re: [R] apply

[External Email]

   It's still hard to figure out what you want.  If you have two vectors you 
can compute their (2x2) covariance matrix using cov(cbind(x,y)).

If you want to compute all pairwise squared differences between elements of x 
and y you could use outer(x, y, "-")^2.

  Can you explain a little bit more about (1) the context for your question and 
(2) why you want/need to use apply() ?

On 2024-10-04 8:28 a.m., Steven Yen wrote:
> OK. Thanks to all. Suppose I have two vectors, x and y. Is there a way
> to do the covariance matrix with "apply". The matrix I need really
> contains the deviation products divided by the degrees of freedom (n-1).
> That is, the elements
>
> (1,1), (1,2),...,(1,n)
>
> (2,1), (2,2),, (2,n)
>
> 
>
> (n,1),(n,2),...,(n,n).
>
>> Hello,
>>
>> This doesn't make sense, if you have only one vector you can estimate
>> its variance with
>>
>> var(x)
>>
>>
>> but there is no covariance, the joint variance of two rv's. "co" or
>> joint with what if you have only x?
>> Note that the variance of x[1] or any other vector element is zero,
>> it's only one value therefore it does not vary. A similar reasonong
>> can be applied to cov(x[1], x[2]), etc.
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>> Às 12:14 de 04/10/2024, Steven Yen escreveu:
>>> Hello
>>>
>>> I have a vector:
>>>
>>> set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749
>>> 1.55870831 I like to create a matrix with elements containing
>>> variances and covariances of x. That is var(x[1]) cov(x[1],x[2])
>>> cov(x[1],x[3]) cov(x[2],x[1]) var(x[2]) cov(x[2],x[3])
>>> cov(x[3],x[1])
>>> cov(x[3],x[2]) var(x[3]) And I like to do it with "apply". Thanks.
>>>
>>> On 10/4/2024 6:35 PM, Rui Barradas wrote:
>>>> Hello,
>>>>
>>>> If you have a numeric matrix or data.frame, try something like
>>>>
>>>> cov(mtcars)
>>>>
>>>> Hope this helps,
>>>>
>>>> Rui Barradas
>>>>
>>>>
>>>> Às 10:15 de 04/10/2024, Steven Yen escreveu:
>>>>> On 10/4/2024 5:13 PM, Steven Yen wrote:
>>>>>
>>>>>> Pardon me!!!
>>>>>>
>>>>>> What makes you think this is a homework question? You are not
>>>>>> obligated to respond if the question is not intelligent enough
>>>>>> for you.
>>>>>>
>>>>>> I did the following: two ways to calculate a covariance matrix
>>>>>> but wonder how I might replicate the results with "apply". I am
>>>>>> not too comfortable with the online documentation of "apply".
>>>>>>
>>>>>>> set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449
>>>>>>> 0.80940115
>>>>>> 0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1]
>>>>>> [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,]
>>>>>> 0.0022410598
>>>>>> 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965
>>>>>> 0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2
>>>>>> [,1] [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,]
>>>>>> 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805
>>>>>> -0.00273267965 0.0151896601 >
>>>>>> On 10/4/2024 4:57 PM, Uwe Ligges wrote:
>>>>>>> Homework questions are not answered on this list.
>>>>>>>
>>>>>>> Best,
>>>>>>> Uwe Ligges
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On 04.10.2024 10:32, Steven Yen wrote:
>>>>>>>> The following line calculates standard deviations of a column
>>>>>>>> vector:
>>>>>>>>
>>>>>>>> se<-apply(dd,1,sd)
>>>>>>>>
>>>>>>>> How can I calculate the covariance matrix using apply? Thanks.
>>>>>>>>
>>>>>>>> __

Re: [R] apply

2024-10-04 Thread Ben Bolker
  It's still hard to figure out what you want.  If you have two vectors 
you can compute their (2x2) covariance matrix using cov(cbind(x,y)).


If you want to compute all pairwise squared differences between elements 
of x and y you could use outer(x, y, "-")^2.


 Can you explain a little bit more about (1) the context for your 
question and (2) why you want/need to use apply() ?


On 2024-10-04 8:28 a.m., Steven Yen wrote:
OK. Thanks to all. Suppose I have two vectors, x and y. Is there a way 
to do the covariance matrix with “apply”. The matrix I need really 
contains the deviation products divided by the degrees of freedom (n-1). 
That is, the elements


(1,1), (1,2),...,(1,n)

(2,1), (2,2),, (2,n)



(n,1),(n,2),...,(n,n).


Hello,

This doesn't make sense, if you have only one vector you can estimate 
its variance with


var(x)


but there is no covariance, the joint variance of two rv's. "co" or 
joint with what if you have only x?
Note that the variance of x[1] or any other vector element is zero, 
it's only one value therefore it does not vary. A similar reasonong 
can be applied to cov(x[1], x[2]), etc.


Hope this helps,

Rui Barradas

Às 12:14 de 04/10/2024, Steven Yen escreveu:

Hello

I have a vector:

set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749 
1.55870831 I like to create a matrix with elements containing 
variances and covariances of x. That is var(x[1]) cov(x[1],x[2]) 
cov(x[1],x[3]) cov(x[2],x[1]) var(x[2]) cov(x[2],x[3]) cov(x[3],x[1]) 
cov(x[3],x[2]) var(x[3]) And I like to do it with "apply". Thanks.


On 10/4/2024 6:35 PM, Rui Barradas wrote:

Hello,

If you have a numeric matrix or data.frame, try something like

cov(mtcars)

Hope this helps,

Rui Barradas


Às 10:15 de 04/10/2024, Steven Yen escreveu:

On 10/4/2024 5:13 PM, Steven Yen wrote:


Pardon me!!!

What makes you think this is a homework question? You are not
obligated to respond if the question is not intelligent enough for 
you.


I did the following: two ways to calculate a covariance matrix but
wonder how I might replicate the results with "apply". I am not too
comfortable with the online documentation of "apply".

set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 
0.80940115

0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2]
[,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598
0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965
0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1]
[,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,]
0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805
-0.00273267965 0.0151896601 >
On 10/4/2024 4:57 PM, Uwe Ligges wrote:

Homework questions are not answered on this list.

Best,
Uwe Ligges



On 04.10.2024 10:32, Steven Yen wrote:
The following line calculates standard deviations of a column 
vector:


se<-apply(dd,1,sd)

How can I calculate the covariance matrix using apply? Thanks.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting- 
guide.html

and provide commented, minimal, self-contained, reproducible code.








__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting- 
guide.html

and provide commented, minimal, self-contained, reproducible code.


--
Dr. Benjamin Bolker
Professor, Mathematics & Statistics and Biology, McMaster University
Director, School of Computational Science and Engineering
> E-mail is sent at my convenience; I don't expect replies outside of 
working hours.


__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply

2024-10-04 Thread Ivan Krylov via R-help
В Fri, 4 Oct 2024 20:28:01 +0800
Steven Yen  пишет:

> Suppose I have two vectors, x and y. Is there a way 
> to do the covariance matrix with “apply”.

There is no covariance matrix for just two samples (vectors) 'x' and
'y'. You can only get one covariance value for these.

If you had a pair of vectors of _random variates_, the situation would
be different, but those are more abstract mathematical concepts. You
would need to sample every random variate, producing two matrices 'x'
and 'y' in order to calculate a covariance matrix for them.

-- 
Best regards,
Ivan

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply

2024-10-04 Thread Steven Yen
OK. Thanks to all. Suppose I have two vectors, x and y. Is there a way 
to do the covariance matrix with “apply”. The matrix I need really 
contains the deviation products divided by the degrees of freedom (n-1). 
That is, the elements


(1,1), (1,2),...,(1,n)

(2,1), (2,2),, (2,n)



(n,1),(n,2),...,(n,n).


Hello,

This doesn't make sense, if you have only one vector you can estimate 
its variance with


var(x)


but there is no covariance, the joint variance of two rv's. "co" or 
joint with what if you have only x?
Note that the variance of x[1] or any other vector element is zero, 
it's only one value therefore it does not vary. A similar reasonong 
can be applied to cov(x[1], x[2]), etc.


Hope this helps,

Rui Barradas

Às 12:14 de 04/10/2024, Steven Yen escreveu:

Hello

I have a vector:

set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749 
1.55870831 I like to create a matrix with elements containing 
variances and covariances of x. That is var(x[1]) cov(x[1],x[2]) 
cov(x[1],x[3]) cov(x[2],x[1]) var(x[2]) cov(x[2],x[3]) cov(x[3],x[1]) 
cov(x[3],x[2]) var(x[3]) And I like to do it with "apply". Thanks.


On 10/4/2024 6:35 PM, Rui Barradas wrote:

Hello,

If you have a numeric matrix or data.frame, try something like

cov(mtcars)

Hope this helps,

Rui Barradas


Às 10:15 de 04/10/2024, Steven Yen escreveu:

On 10/4/2024 5:13 PM, Steven Yen wrote:


Pardon me!!!

What makes you think this is a homework question? You are not
obligated to respond if the question is not intelligent enough for 
you.


I did the following: two ways to calculate a covariance matrix but
wonder how I might replicate the results with "apply". I am not too
comfortable with the online documentation of "apply".

set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 
0.80940115

0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2]
[,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598
0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965
0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1]
[,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,]
0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805
-0.00273267965 0.0151896601 >
On 10/4/2024 4:57 PM, Uwe Ligges wrote:

Homework questions are not answered on this list.

Best,
Uwe Ligges



On 04.10.2024 10:32, Steven Yen wrote:
The following line calculates standard deviations of a column 
vector:


se<-apply(dd,1,sd)

How can I calculate the covariance matrix using apply? Thanks.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting- 
guide.html

and provide commented, minimal, self-contained, reproducible code.








__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply

2024-10-04 Thread Rui Barradas

Hello,

This doesn't make sense, if you have only one vector you can estimate 
its variance with


var(x)


but there is no covariance, the joint variance of two rv's. "co" or 
joint with what if you have only x?
Note that the variance of x[1] or any other vector element is zero, it's 
only one value therefore it does not vary. A similar reasonong can be 
applied to cov(x[1], x[2]), etc.


Hope this helps,

Rui Barradas

Às 12:14 de 04/10/2024, Steven Yen escreveu:

Hello

I have a vector:

set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749 
1.55870831 I like to create a matrix with elements containing variances 
and covariances of x. That is var(x[1]) cov(x[1],x[2]) cov(x[1],x[3]) 
cov(x[2],x[1]) var(x[2]) cov(x[2],x[3]) cov(x[3],x[1]) cov(x[3],x[2]) 
var(x[3]) And I like to do it with "apply". Thanks.


On 10/4/2024 6:35 PM, Rui Barradas wrote:

Hello,

If you have a numeric matrix or data.frame, try something like

cov(mtcars)

Hope this helps,

Rui Barradas


Às 10:15 de 04/10/2024, Steven Yen escreveu:

On 10/4/2024 5:13 PM, Steven Yen wrote:


Pardon me!!!

What makes you think this is a homework question? You are not
obligated to respond if the question is not intelligent enough for you.

I did the following: two ways to calculate a covariance matrix but
wonder how I might replicate the results with "apply". I am not too
comfortable with the online documentation of "apply".


set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115

0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2]
[,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598
0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965
0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1]
[,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,]
0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805
-0.00273267965 0.0151896601 >
On 10/4/2024 4:57 PM, Uwe Ligges wrote:

Homework questions are not answered on this list.

Best,
Uwe Ligges



On 04.10.2024 10:32, Steven Yen wrote:

The following line calculates standard deviations of a column vector:

se<-apply(dd,1,sd)

How can I calculate the covariance matrix using apply? Thanks.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting- 
guide.html

and provide commented, minimal, self-contained, reproducible code.






--
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença 
de vírus.
www.avg.com

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply

2024-10-04 Thread Ivan Krylov via R-help
В Fri, 4 Oct 2024 19:14:30 +0800
Steven Yen  пишет:

> I have a vector:

> set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749
> 1.55870831

> var(x[1]) cov(x[1],x[2])

Are you sure you don't have a matrix? If you type var(x[1]) or
cov(x[1],x[2]) into R, you can see that all these are NA: an unbiased
estimate of variance or covariance requires dividing by (sample size -
1), which would be 0 for individual numbers.

Even if you did divide by (sample size), the answers would all be 0,
because a single number is always equal to the mean of the same one
number.

-- 
Best regards,
Ivan

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply

2024-10-04 Thread Steven Yen
Hello

I have a vector:

set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749 
1.55870831 I like to create a matrix with elements containing variances 
and covariances of x. That is var(x[1]) cov(x[1],x[2]) cov(x[1],x[3]) 
cov(x[2],x[1]) var(x[2]) cov(x[2],x[3]) cov(x[3],x[1]) cov(x[3],x[2]) 
var(x[3]) And I like to do it with "apply". Thanks.

On 10/4/2024 6:35 PM, Rui Barradas wrote:
> Hello,
>
> If you have a numeric matrix or data.frame, try something like
>
> cov(mtcars)
>
> Hope this helps,
>
> Rui Barradas
>
>
> Às 10:15 de 04/10/2024, Steven Yen escreveu:
>> On 10/4/2024 5:13 PM, Steven Yen wrote:
>>
>>> Pardon me!!!
>>>
>>> What makes you think this is a homework question? You are not
>>> obligated to respond if the question is not intelligent enough for you.
>>>
>>> I did the following: two ways to calculate a covariance matrix but
>>> wonder how I might replicate the results with "apply". I am not too
>>> comfortable with the online documentation of "apply".
>>>
 set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115
>>> 0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2]
>>> [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598
>>> 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965
>>> 0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1]
>>> [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,]
>>> 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805
>>> -0.00273267965 0.0151896601 >
>>> On 10/4/2024 4:57 PM, Uwe Ligges wrote:
 Homework questions are not answered on this list.

 Best,
 Uwe Ligges



 On 04.10.2024 10:32, Steven Yen wrote:
> The following line calculates standard deviations of a column vector:
>
> se<-apply(dd,1,sd)
>
> How can I calculate the covariance matrix using apply? Thanks.
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>> [[alternative HTML version deleted]]
>>
>> __
>> [email protected] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide 
>> https://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply

2024-10-04 Thread Rui Barradas

Hello,

If you have a numeric matrix or data.frame, try something like

cov(mtcars)

Hope this helps,

Rui Barradas


Às 10:15 de 04/10/2024, Steven Yen escreveu:

On 10/4/2024 5:13 PM, Steven Yen wrote:


Pardon me!!!

What makes you think this is a homework question? You are not
obligated to respond if the question is not intelligent enough for you.

I did the following: two ways to calculate a covariance matrix but
wonder how I might replicate the results with "apply". I am not too
comfortable with the online documentation of "apply".


set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115

0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2]
[,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598
0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965
0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1]
[,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,]
0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805
-0.00273267965 0.0151896601 >
On 10/4/2024 4:57 PM, Uwe Ligges wrote:

Homework questions are not answered on this list.

Best,
Uwe Ligges



On 04.10.2024 10:32, Steven Yen wrote:

The following line calculates standard deviations of a column vector:

se<-apply(dd,1,sd)

How can I calculate the covariance matrix using apply? Thanks.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



--
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença 
de vírus.
www.avg.com

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply

2024-10-04 Thread Uwe Ligges



On 04.10.2024 11:13, Steven Yen wrote:

Pardon me!!!

What makes you think this is a homework question? You are not obligated 


Otherwise you called cov()

Best,
Uwe Ligges




to respond if the question is not intelligent enough for you.

I did the following: two ways to calculate a covariance matrix but 
wonder how I might replicate the results with "apply". I am not too 
comfortable with the online do of apply.


set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115 
0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2] [,3] 
[1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 
0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 
0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1] [,2] 
[,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 
0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 
0.0151896601 >


On 10/4/2024 4:57 PM, Uwe Ligges wrote:

Homework questions are not answered on this list.

Best,
Uwe Ligges



On 04.10.2024 10:32, Steven Yen wrote:

The following line calculates standard deviations of a column vector:

se<-apply(dd,1,sd)

How can I calculate the covariance matrix using apply? Thanks.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide 
https://www.R-project.org/posting-guide.html

and provide commented, minimal, self-contained, reproducible code.
__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply

2024-10-04 Thread Steven Yen
On 10/4/2024 5:13 PM, Steven Yen wrote:

> Pardon me!!!
>
> What makes you think this is a homework question? You are not 
> obligated to respond if the question is not intelligent enough for you.
>
> I did the following: two ways to calculate a covariance matrix but 
> wonder how I might replicate the results with "apply". I am not too 
> comfortable with the online documentation of "apply".
>
> > set.seed(122345671) > n<-3 > x<-rnorm(n); x [1] 0.92098449 0.80940115 
> 0.60374785 > cov1<-outer(x-mean(x),x-mean(x))/(n-1); cov1 [,1] [,2] 
> [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 0.0022410598 
> 0.00049161983 -0.0027326797 [3,] -0.0124569805 -0.00273267965 
> 0.0151896601 > cov2<-(x-mean(x))%*%t((x-mean(x)))/(n-1); cov2 [,1] 
> [,2] [,3] [1,] 0.0102159207 0.00224105983 -0.0124569805 [2,] 
> 0.0022410598 0.00049161983 -0.0027326797 [3,] -0.0124569805 
> -0.00273267965 0.0151896601 >
> On 10/4/2024 4:57 PM, Uwe Ligges wrote:
>> Homework questions are not answered on this list.
>>
>> Best,
>> Uwe Ligges
>>
>>
>>
>> On 04.10.2024 10:32, Steven Yen wrote:
>>> The following line calculates standard deviations of a column vector:
>>>
>>> se<-apply(dd,1,sd)
>>>
>>> How can I calculate the covariance matrix using apply? Thanks.
>>>
>>> __
>>> [email protected] mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide 
>>> https://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply

2024-10-04 Thread Uwe Ligges

Homework questions are not answered on this list.

Best,
Uwe Ligges



On 04.10.2024 10:32, Steven Yen wrote:

The following line calculates standard deviations of a column vector:

se<-apply(dd,1,sd)

How can I calculate the covariance matrix using apply? Thanks.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide 
https://www.R-project.org/posting-guide.html

and provide commented, minimal, self-contained, reproducible code.
__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply a function to a list of lists

2023-09-20 Thread Ivan Krylov
On Tue, 19 Sep 2023 17:14:58 +0200
arnaud gaboury  wrote:

> non_empty_df <- function(l) {
> lapply(l, function(df) df[sapply(df, function(df) nrow(df) !=0)])
> }

> If I test this way: non_empty_df(my.list[1]) it does the job. It will
> return the data.frame from the first list of my_list with rows.
> Now I can't find a way to iterate the above function to all the 9
> nested lists.

I think it should already work on the whole my.list. Note that
my.list[1] is a list containing one list of data.frames, not just a
list of data.frames (the latter would be my.list[[1]]).

The outer `l` argument is the list of lists of data.frames. Inside the
first anonymous function, `df` is a list of data.frames. Inside the
second anonymous function, `df` is finally a data frame which you test
for being non-empty. The lapply() call iterates over the lists of lists
and the sapply() iterates over the data.frames inside a list.

-- 
Best regards,
Ivan

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] 'apply' for 0 or 1 element member.

2023-08-10 Thread Jeff Newmiller
Define

mean0na <- function(x) {
  x[ 0 == x ] <- NA
  mean( x, na.rm = TRUE )
}

and then use that instead of mean.

On August 10, 2023 7:25:08 PM PDT, ani jaya  wrote:
>Hello,
>
>I try to calculate the mean of an array with a condition. My array is
>B=c(181,101,420) in dimensions. And I want to find a specific member
>in 3rd dimension (time monthly) based on a condition of another data
>frame, A =c(420). My final array would be C=c(181,101,12) which 12 is
>a monthly mean based on the condition of A.
>
>what I tried is below but doesn't work.
>
>#find the index with a condition larger than 0.5
>index<-which(A>=0.5)
>#convert the index to monthly
>index1<-index%%12
>#change 0 to 12 (Jan=1, Feb=2, and so on..)
>index1[index1==0]<-12
>
>for (i in 1:12){
>dummy<-index[which(index1==i)]
>C[,,i]<-apply(B[,,dummy],c(1,2), mean,na.rm=T)
>}
>
>The problem is when there is a month that doesnt meet the condition,
>it will return 0 element.
>
>apply(B[,,1],c(1,2), mean,na.rm=T) also produce an error.
>
>Any solution and turnaround would be appreciated.
>
>Thank you
>
>Ani Jaya
>
>__
>[email protected] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

-- 
Sent from my phone. Please excuse my brevity.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply to row and column of matrix

2022-09-26 Thread Eric Berger
Bert provided an excellent answer to your question.
FYI here is a different approach to do the calculation.
It uses data.frame rather than matrix. A data frame is a list of its columns.
Here the function supplied to sapply operates on each column of the data.frame.

> m <- as.data.frame(t(matrix(1:10,ncol=2)))
> m
   V1  V2  V3  V4  V5
1   1 23 45
2678 9   10

> sapply(m, cumsum)
  V1  V2  V3  V4  V5
[1,]1 23 45
[2,]7 9   11   13  15





On Mon, Sep 26, 2022 at 5:57 PM Bert Gunter  wrote:
>
> from ?apply:
> "If each call to FUN returns a vector of length n, and simplify is
> TRUE, then apply returns an array of dimension c(n, dim(X)[MARGIN]) ."
>
> For margin = 1 (cumsum over rows), each call to cumsum return a vector
> of length 2. Hence the array returned will be of dimension c(2,
> c(5,2)[1]) = c(2,5).
>
> Cheers,
> Bert
>
>
> On Mon, Sep 26, 2022 at 7:43 AM Jinsong Zhao  wrote:
> >
> > Hi there,
> >
> > I try to calculate the cumsum of row and column of a matrix as follows.
> >
> >  > m <- matrix(1:10, ncol = 2)
> >  > m
> >   [,1] [,2]
> > [1,]16
> > [2,]27
> > [3,]38
> > [4,]49
> > [5,]5   10
> >  > apply(m, 1, cumsum)
> >   [,1] [,2] [,3] [,4] [,5]
> > [1,]12345
> > [2,]79   11   13   15
> >  > apply(m, 2, cumsum)
> >   [,1] [,2]
> > [1,]16
> > [2,]3   13
> > [3,]6   21
> > [4,]   10   30
> > [5,]   15   40
> >
> > My question is why the dim of the return value of apply(m, 1, cumsum) is
> > not 5x2, but 2x5.
> >
> > Best,
> >
> > Jinsong
> >
> > __
> > [email protected] mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply to row and column of matrix

2022-09-26 Thread Bert Gunter
from ?apply:
"If each call to FUN returns a vector of length n, and simplify is
TRUE, then apply returns an array of dimension c(n, dim(X)[MARGIN]) ."

For margin = 1 (cumsum over rows), each call to cumsum return a vector
of length 2. Hence the array returned will be of dimension c(2,
c(5,2)[1]) = c(2,5).

Cheers,
Bert


On Mon, Sep 26, 2022 at 7:43 AM Jinsong Zhao  wrote:
>
> Hi there,
>
> I try to calculate the cumsum of row and column of a matrix as follows.
>
>  > m <- matrix(1:10, ncol = 2)
>  > m
>   [,1] [,2]
> [1,]16
> [2,]27
> [3,]38
> [4,]49
> [5,]5   10
>  > apply(m, 1, cumsum)
>   [,1] [,2] [,3] [,4] [,5]
> [1,]12345
> [2,]79   11   13   15
>  > apply(m, 2, cumsum)
>   [,1] [,2]
> [1,]16
> [2,]3   13
> [3,]6   21
> [4,]   10   30
> [5,]   15   40
>
> My question is why the dim of the return value of apply(m, 1, cumsum) is
> not 5x2, but 2x5.
>
> Best,
>
> Jinsong
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] "apply" a function that takes two or more vectors as arguments, such as cor(x, y), over a "category" or "grouping variable" or "index"?

2022-04-09 Thread Kelly Thompson
Thanks. I have a clarification and a follow-up question. I should have
asked this in the original post, and I should have provided a better
example for the FUN argument, I apologize.

For use in an example, here is a "silly" example of a function that
requires arguments such as x and y to be "separately assigned" :

udf_x_plus_y <- function (x, y) { return ( x + y) }

Q. Is there a way to use by() when the argument of FUN is a function
that requires arguments such as "x" and "y" to be separately assigned
(ex. udf_x_plus_y (x = my_x , y = my_y ), rather than assigned as a
range of columns using brackets (ex. cor(x)[1,2]) ?

Something like this perhaps? (This produces an error message.)
by( data = my_df[-1], INDICES = my_df$my_category,  FUN = function(x,
y) { udf_x_plus_y (x = data$my_x, y = data$my_y) } )

Thanks again.

On Sat, Apr 9, 2022 at 5:32 AM Rui Barradas  wrote:
>
> Hello,
>
> Another option is ?by.
>
>
> by(my_df[-1], my_df$my_category, cor)
> by(my_df[-1], my_df$my_category, \(x) cor(x)[1,2])
>
>
> Hope this helps,
>
> Rui Barradas
>
> Às 02:26 de 09/04/2022, Kelly Thompson escreveu:
> > #Q. How can I "apply" a function that takes two or more vectors as
> > arguments, such as cor(x, y), over a "category" or "grouping variable"
> > or "index"?
> > #I'm using cor() as an example, I'd like to find a way to do this for
> > any function that takes 2 or more vectors as arguments.
> >
> >
> > #create example data
> >
> > my_category <- rep ( c("a","b","c"),  4)
> >
> > set.seed(12345)
> > my_x <- rnorm(12)
> >
> > set.seed(54321)
> > my_y <- rnorm(12)
> >
> > my_df <- data.frame(my_category, my_x, my_y)
> >
> > #review data
> > my_df
> >
> > #If i wanted to get the correlation of x and y grouped by category, I
> > could use this code and loop:
> >
> > my_category_unique <- unique(my_category)
> >
> > my_results <- vector("list", length(my_category_unique) )
> > names(my_results) <- my_category_unique
> >
> > #start i loop
> >for (i in 1:length(my_category_unique) ) {
> >  my_criteria_i <- my_category == my_category_unique[i]
> >  my_x_i <- my_x[which(my_criteria_i)]
> >  my_y_i <- my_y[which(my_criteria_i)]
> >  my_correl_i <- cor(x = my_x_i, y = my_y_i)
> >  my_results[i] <- list(my_correl_i)
> > } # end i loop
> >
> > #review results
> > my_results
> >
> > #Q. Is there a better or more "elegant" way to do this, using by(),
> > aggregate(), apply(), or some other function?
> >
> > #This does not work and results in this error message: "Error in
> > FUN(dd[x, ], ...) : incompatible dimensions"
> > by (data = my_x, INDICES = my_category, FUN = cor, y = my_y)
> >
> > #This does not work and results in this error message: "Error in
> > cor(my_df$x, my_df$y) : ... supply both 'x' and 'y' or a matrix-like
> > 'x' "
> > by (data = my_df, INDICES = my_category, FUN = function(x, y) { cor
> > (my_df$x, my_df$y) } )
> >
> >
> > #if I wanted the mean of x by category, I could use by() or aggregate():
> > by (data = my_x, INDICES = my_category, FUN = mean)
> >
> > aggregate(x = my_x, by = list(my_category), FUN = mean)
> >
> > #Thanks!
> >
> > __
> > [email protected] mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply gsub to dataframe to modify row values

2021-08-09 Thread Luigi Marongiu
Thank you, it works!

On Mon, Aug 9, 2021 at 3:26 PM Andrew Simmons  wrote:
>
> Hello,
>
>
> There are two convenient ways to access a column in a data.frame using `$` 
> and `[[`. Using `df` from your first email, we would do something like
>
> df <- data.frame(VAR = 1:3, VAL = c("value is blue", "Value is red", "empty"))
> df$VAL
> df[["VAL"]]
>
> The two convenient ways to update / / replace a column with something new are 
> also very similar, something like
>
> df$VAL <- ...
> df[["VAL"]] <- ...
>
> As for the regex part, I would suggest using `sub` instead of `gsub` since 
> you're looking to remove only the first instance of "value is". Also, I would 
> recommend using "^" to mark the beginning of your string, something like
>
> df$VAL <- sub("^Value is ", "", df$VAL, ignore.case = TRUE)
>
> I might be misunderstanding, but it sounds like you also want to remove all 
> leading whitespace. If so, you could do something like
>
> df$VAL <- sub("^[[:blank:]]*Value is ", "", df$VAL, ignore.case = TRUE)
>
> where "*" signifies that there will be zero or more blank characters at the 
> beginning of the string. You can try `?regex` to read more about this.
>
> I hope this helps!
>
> On Mon, Aug 9, 2021 at 6:50 AM Luigi Marongiu  
> wrote:
>>
>> Sorry, silly question, gsub works already with regex. But still, if I
>> add `[[:blank:]]` still I don't get rid of all instances. And I am
>> keeping obtaining extra columns
>> ```
>> > df[df$VAL] = gsub("[[:blank:]Value is]", "", df$VAL, ignore.case=TRUE)
>> > df[df$VAL] = gsub("[[:blank:]Value is]", "", df$VAL, ignore.case=TRUE);df
>>   VAR   VAL value is blue Value is red empty
>> 1   1 value is blue bb b
>> 2   2  Value is redrd   rdrd
>> 3   3 empty  mpty mpty  mpty
>> ```
>>
>> On Mon, Aug 9, 2021 at 12:40 PM Luigi Marongiu  
>> wrote:
>> >
>> > Thank you, that is much appreciated. But on the real data, the
>> > substitution works only on few instances. Is there a way to introduce
>> > regex into this?
>> > Cheers
>> > Luigi
>> >
>> > On Mon, Aug 9, 2021 at 11:01 AM Jim Lemon  wrote:
>> > >
>> > > Hi Luigi,
>> > > Ah, now I see:
>> > >
>> > >  df$VAL<-gsub("Value is","",df$VAL,ignore.case=TRUE)
>> > > df
>> > >  VAR   VAL
>> > > 1   1  blue
>> > > 2   2   red
>> > > 3   3 empty
>> > >
>> > > Jim
>> > >
>> > > On Mon, Aug 9, 2021 at 6:43 PM Luigi Marongiu  
>> > > wrote:
>> > > >
>> > > > Hello,
>> > > > I have a dataframe where I would like to change the string of certain
>> > > > rows, essentially I am looking to remove some useless text from the
>> > > > variables.
>> > > > I tried with:
>> > > > ```
>> > > > > df = data.frame(VAR = 1:3, VAL = c("value is blue", "Value is red", 
>> > > > > "empty"))
>> > > > > df[df$VAL] = gsub("value is ", "", df$VAL, ignore.case = TRUE, perl 
>> > > > > = FALSE)
>> > > > > df
>> > > >   VAR   VAL value is blue Value is red empty
>> > > > 1   1 value is blue  blue blue  blue
>> > > > 2   2  Value is red   red  red   red
>> > > > 3   3 empty emptyempty empty
>> > > > ```
>> > > > which is of course wrong because I was expecting
>> > > > ```
>> > > >   VAR   VAL
>> > > > 1   1 blue
>> > > > 2   2 red
>> > > > 3   3empty
>> > > > ```
>> > > > What is the correct syntax in these cases?
>> > > > Thank you
>> > > >
>> > > > __
>> > > > [email protected] mailing list -- To UNSUBSCRIBE and more, see
>> > > > https://stat.ethz.ch/mailman/listinfo/r-help
>> > > > PLEASE do read the posting guide 
>> > > > http://www.R-project.org/posting-guide.html
>> > > > and provide commented, minimal, self-contained, reproducible code.
>> >
>> >
>> >
>> > --
>> > Best regards,
>> > Luigi
>>
>>
>>
>> --
>> Best regards,
>> Luigi
>>
>> __
>> [email protected] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.



-- 
Best regards,
Luigi

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply gsub to dataframe to modify row values

2021-08-09 Thread Andrew Simmons
Hello,


There are two convenient ways to access a column in a data.frame using `$`
and `[[`. Using `df` from your first email, we would do something like

df <- data.frame(VAR = 1:3, VAL = c("value is blue", "Value is red",
"empty"))
df$VAL
df[["VAL"]]

The two convenient ways to update / / replace a column with something new
are also very similar, something like

df$VAL <- ...
df[["VAL"]] <- ...

As for the regex part, I would suggest using `sub` instead of `gsub` since
you're looking to remove only the first instance of "value is". Also, I
would recommend using "^" to mark the beginning of your string, something
like

df$VAL <- sub("^Value is ", "", df$VAL, ignore.case = TRUE)

I might be misunderstanding, but it sounds like you also want to remove all
leading whitespace. If so, you could do something like

df$VAL <- sub("^[[:blank:]]*Value is ", "", df$VAL, ignore.case = TRUE)

where "*" signifies that there will be zero or more blank characters at the
beginning of the string. You can try `?regex` to read more about this.

I hope this helps!

On Mon, Aug 9, 2021 at 6:50 AM Luigi Marongiu 
wrote:

> Sorry, silly question, gsub works already with regex. But still, if I
> add `[[:blank:]]` still I don't get rid of all instances. And I am
> keeping obtaining extra columns
> ```
> > df[df$VAL] = gsub("[[:blank:]Value is]", "", df$VAL, ignore.case=TRUE)
> > df[df$VAL] = gsub("[[:blank:]Value is]", "", df$VAL, ignore.case=TRUE);df
>   VAR   VAL value is blue Value is red empty
> 1   1 value is blue bb b
> 2   2  Value is redrd   rdrd
> 3   3 empty  mpty mpty  mpty
> ```
>
> On Mon, Aug 9, 2021 at 12:40 PM Luigi Marongiu 
> wrote:
> >
> > Thank you, that is much appreciated. But on the real data, the
> > substitution works only on few instances. Is there a way to introduce
> > regex into this?
> > Cheers
> > Luigi
> >
> > On Mon, Aug 9, 2021 at 11:01 AM Jim Lemon  wrote:
> > >
> > > Hi Luigi,
> > > Ah, now I see:
> > >
> > >  df$VAL<-gsub("Value is","",df$VAL,ignore.case=TRUE)
> > > df
> > >  VAR   VAL
> > > 1   1  blue
> > > 2   2   red
> > > 3   3 empty
> > >
> > > Jim
> > >
> > > On Mon, Aug 9, 2021 at 6:43 PM Luigi Marongiu <
> [email protected]> wrote:
> > > >
> > > > Hello,
> > > > I have a dataframe where I would like to change the string of certain
> > > > rows, essentially I am looking to remove some useless text from the
> > > > variables.
> > > > I tried with:
> > > > ```
> > > > > df = data.frame(VAR = 1:3, VAL = c("value is blue", "Value is
> red", "empty"))
> > > > > df[df$VAL] = gsub("value is ", "", df$VAL, ignore.case = TRUE,
> perl = FALSE)
> > > > > df
> > > >   VAR   VAL value is blue Value is red empty
> > > > 1   1 value is blue  blue blue  blue
> > > > 2   2  Value is red   red  red   red
> > > > 3   3 empty emptyempty empty
> > > > ```
> > > > which is of course wrong because I was expecting
> > > > ```
> > > >   VAR   VAL
> > > > 1   1 blue
> > > > 2   2 red
> > > > 3   3empty
> > > > ```
> > > > What is the correct syntax in these cases?
> > > > Thank you
> > > >
> > > > __
> > > > [email protected] mailing list -- To UNSUBSCRIBE and more, see
> > > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > > > and provide commented, minimal, self-contained, reproducible code.
> >
> >
> >
> > --
> > Best regards,
> > Luigi
>
>
>
> --
> Best regards,
> Luigi
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply gsub to dataframe to modify row values

2021-08-09 Thread Luigi Marongiu
I wanted to remove possible white spaces before or after the string.
Actually, it worked, I used `gsub("[:blank:]*val[:blank:]*", "",
df$VAL, ignore.case=TRUE)`. I don't know why in the example there were
extra columns -- they did not came out in the real case.
Thank you, I think the case is closed.
Cheers
Luigi

On Mon, Aug 9, 2021 at 1:33 PM Jim Lemon  wrote:
>
> Hi Luigi,
> You want to get rid of certain strings in the "VAL" column. You are
> assigning to:
>
> df[df$VAL]
> Error in `[.data.frame`(df, df$VAL) : undefined columns selected
>
> when I think you should be assigning to:
>
> df$VAL
>
> What do you want to remove other than "[V|v]alue is" ?
>
> JIim
>
> On Mon, Aug 9, 2021 at 8:50 PM Luigi Marongiu  
> wrote:
> >
> > Sorry, silly question, gsub works already with regex. But still, if I
> > add `[[:blank:]]` still I don't get rid of all instances. And I am
> > keeping obtaining extra columns
> > ```
> > > df[df$VAL] = gsub("[[:blank:]Value is]", "", df$VAL, ignore.case=TRUE)
> > > df[df$VAL] = gsub("[[:blank:]Value is]", "", df$VAL, ignore.case=TRUE);df
> >   VAR   VAL value is blue Value is red empty
> > 1   1 value is blue bb b
> > 2   2  Value is redrd   rdrd
> > 3   3 empty  mpty mpty  mpty
> > ```
> >
> > On Mon, Aug 9, 2021 at 12:40 PM Luigi Marongiu  
> > wrote:
> > >
> > > Thank you, that is much appreciated. But on the real data, the
> > > substitution works only on few instances. Is there a way to introduce
> > > regex into this?
> > > Cheers
> > > Luigi
> > >
> > > On Mon, Aug 9, 2021 at 11:01 AM Jim Lemon  wrote:
> > > >
> > > > Hi Luigi,
> > > > Ah, now I see:
> > > >
> > > >  df$VAL<-gsub("Value is","",df$VAL,ignore.case=TRUE)
> > > > df
> > > >  VAR   VAL
> > > > 1   1  blue
> > > > 2   2   red
> > > > 3   3 empty
> > > >
> > > > Jim
> > > >
> > > > On Mon, Aug 9, 2021 at 6:43 PM Luigi Marongiu 
> > > >  wrote:
> > > > >
> > > > > Hello,
> > > > > I have a dataframe where I would like to change the string of certain
> > > > > rows, essentially I am looking to remove some useless text from the
> > > > > variables.
> > > > > I tried with:
> > > > > ```
> > > > > > df = data.frame(VAR = 1:3, VAL = c("value is blue", "Value is red", 
> > > > > > "empty"))
> > > > > > df[df$VAL] = gsub("value is ", "", df$VAL, ignore.case = TRUE, perl 
> > > > > > = FALSE)
> > > > > > df
> > > > >   VAR   VAL value is blue Value is red empty
> > > > > 1   1 value is blue  blue blue  blue
> > > > > 2   2  Value is red   red  red   red
> > > > > 3   3 empty emptyempty empty
> > > > > ```
> > > > > which is of course wrong because I was expecting
> > > > > ```
> > > > >   VAR   VAL
> > > > > 1   1 blue
> > > > > 2   2 red
> > > > > 3   3empty
> > > > > ```
> > > > > What is the correct syntax in these cases?
> > > > > Thank you
> > > > >
> > > > > __
> > > > > [email protected] mailing list -- To UNSUBSCRIBE and more, see
> > > > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > > > PLEASE do read the posting guide 
> > > > > http://www.R-project.org/posting-guide.html
> > > > > and provide commented, minimal, self-contained, reproducible code.
> > >
> > >
> > >
> > > --
> > > Best regards,
> > > Luigi
> >
> >
> >
> > --
> > Best regards,
> > Luigi



-- 
Best regards,
Luigi

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply gsub to dataframe to modify row values

2021-08-09 Thread Jim Lemon
Hi Luigi,
You want to get rid of certain strings in the "VAL" column. You are
assigning to:

df[df$VAL]
Error in `[.data.frame`(df, df$VAL) : undefined columns selected

when I think you should be assigning to:

df$VAL

What do you want to remove other than "[V|v]alue is" ?

JIim

On Mon, Aug 9, 2021 at 8:50 PM Luigi Marongiu  wrote:
>
> Sorry, silly question, gsub works already with regex. But still, if I
> add `[[:blank:]]` still I don't get rid of all instances. And I am
> keeping obtaining extra columns
> ```
> > df[df$VAL] = gsub("[[:blank:]Value is]", "", df$VAL, ignore.case=TRUE)
> > df[df$VAL] = gsub("[[:blank:]Value is]", "", df$VAL, ignore.case=TRUE);df
>   VAR   VAL value is blue Value is red empty
> 1   1 value is blue bb b
> 2   2  Value is redrd   rdrd
> 3   3 empty  mpty mpty  mpty
> ```
>
> On Mon, Aug 9, 2021 at 12:40 PM Luigi Marongiu  
> wrote:
> >
> > Thank you, that is much appreciated. But on the real data, the
> > substitution works only on few instances. Is there a way to introduce
> > regex into this?
> > Cheers
> > Luigi
> >
> > On Mon, Aug 9, 2021 at 11:01 AM Jim Lemon  wrote:
> > >
> > > Hi Luigi,
> > > Ah, now I see:
> > >
> > >  df$VAL<-gsub("Value is","",df$VAL,ignore.case=TRUE)
> > > df
> > >  VAR   VAL
> > > 1   1  blue
> > > 2   2   red
> > > 3   3 empty
> > >
> > > Jim
> > >
> > > On Mon, Aug 9, 2021 at 6:43 PM Luigi Marongiu  
> > > wrote:
> > > >
> > > > Hello,
> > > > I have a dataframe where I would like to change the string of certain
> > > > rows, essentially I am looking to remove some useless text from the
> > > > variables.
> > > > I tried with:
> > > > ```
> > > > > df = data.frame(VAR = 1:3, VAL = c("value is blue", "Value is red", 
> > > > > "empty"))
> > > > > df[df$VAL] = gsub("value is ", "", df$VAL, ignore.case = TRUE, perl = 
> > > > > FALSE)
> > > > > df
> > > >   VAR   VAL value is blue Value is red empty
> > > > 1   1 value is blue  blue blue  blue
> > > > 2   2  Value is red   red  red   red
> > > > 3   3 empty emptyempty empty
> > > > ```
> > > > which is of course wrong because I was expecting
> > > > ```
> > > >   VAR   VAL
> > > > 1   1 blue
> > > > 2   2 red
> > > > 3   3empty
> > > > ```
> > > > What is the correct syntax in these cases?
> > > > Thank you
> > > >
> > > > __
> > > > [email protected] mailing list -- To UNSUBSCRIBE and more, see
> > > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > > PLEASE do read the posting guide 
> > > > http://www.R-project.org/posting-guide.html
> > > > and provide commented, minimal, self-contained, reproducible code.
> >
> >
> >
> > --
> > Best regards,
> > Luigi
>
>
>
> --
> Best regards,
> Luigi

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply gsub to dataframe to modify row values

2021-08-09 Thread Luigi Marongiu
Sorry, silly question, gsub works already with regex. But still, if I
add `[[:blank:]]` still I don't get rid of all instances. And I am
keeping obtaining extra columns
```
> df[df$VAL] = gsub("[[:blank:]Value is]", "", df$VAL, ignore.case=TRUE)
> df[df$VAL] = gsub("[[:blank:]Value is]", "", df$VAL, ignore.case=TRUE);df
  VAR   VAL value is blue Value is red empty
1   1 value is blue bb b
2   2  Value is redrd   rdrd
3   3 empty  mpty mpty  mpty
```

On Mon, Aug 9, 2021 at 12:40 PM Luigi Marongiu  wrote:
>
> Thank you, that is much appreciated. But on the real data, the
> substitution works only on few instances. Is there a way to introduce
> regex into this?
> Cheers
> Luigi
>
> On Mon, Aug 9, 2021 at 11:01 AM Jim Lemon  wrote:
> >
> > Hi Luigi,
> > Ah, now I see:
> >
> >  df$VAL<-gsub("Value is","",df$VAL,ignore.case=TRUE)
> > df
> >  VAR   VAL
> > 1   1  blue
> > 2   2   red
> > 3   3 empty
> >
> > Jim
> >
> > On Mon, Aug 9, 2021 at 6:43 PM Luigi Marongiu  
> > wrote:
> > >
> > > Hello,
> > > I have a dataframe where I would like to change the string of certain
> > > rows, essentially I am looking to remove some useless text from the
> > > variables.
> > > I tried with:
> > > ```
> > > > df = data.frame(VAR = 1:3, VAL = c("value is blue", "Value is red", 
> > > > "empty"))
> > > > df[df$VAL] = gsub("value is ", "", df$VAL, ignore.case = TRUE, perl = 
> > > > FALSE)
> > > > df
> > >   VAR   VAL value is blue Value is red empty
> > > 1   1 value is blue  blue blue  blue
> > > 2   2  Value is red   red  red   red
> > > 3   3 empty emptyempty empty
> > > ```
> > > which is of course wrong because I was expecting
> > > ```
> > >   VAR   VAL
> > > 1   1 blue
> > > 2   2 red
> > > 3   3empty
> > > ```
> > > What is the correct syntax in these cases?
> > > Thank you
> > >
> > > __
> > > [email protected] mailing list -- To UNSUBSCRIBE and more, see
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide 
> > > http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
>
>
>
> --
> Best regards,
> Luigi



-- 
Best regards,
Luigi

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply gsub to dataframe to modify row values

2021-08-09 Thread Luigi Marongiu
Thank you, that is much appreciated. But on the real data, the
substitution works only on few instances. Is there a way to introduce
regex into this?
Cheers
Luigi

On Mon, Aug 9, 2021 at 11:01 AM Jim Lemon  wrote:
>
> Hi Luigi,
> Ah, now I see:
>
>  df$VAL<-gsub("Value is","",df$VAL,ignore.case=TRUE)
> df
>  VAR   VAL
> 1   1  blue
> 2   2   red
> 3   3 empty
>
> Jim
>
> On Mon, Aug 9, 2021 at 6:43 PM Luigi Marongiu  
> wrote:
> >
> > Hello,
> > I have a dataframe where I would like to change the string of certain
> > rows, essentially I am looking to remove some useless text from the
> > variables.
> > I tried with:
> > ```
> > > df = data.frame(VAR = 1:3, VAL = c("value is blue", "Value is red", 
> > > "empty"))
> > > df[df$VAL] = gsub("value is ", "", df$VAL, ignore.case = TRUE, perl = 
> > > FALSE)
> > > df
> >   VAR   VAL value is blue Value is red empty
> > 1   1 value is blue  blue blue  blue
> > 2   2  Value is red   red  red   red
> > 3   3 empty emptyempty empty
> > ```
> > which is of course wrong because I was expecting
> > ```
> >   VAR   VAL
> > 1   1 blue
> > 2   2 red
> > 3   3empty
> > ```
> > What is the correct syntax in these cases?
> > Thank you
> >
> > __
> > [email protected] mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.



-- 
Best regards,
Luigi

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply gsub to dataframe to modify row values

2021-08-09 Thread Jim Lemon
Hi Luigi,
Ah, now I see:

 df$VAL<-gsub("Value is","",df$VAL,ignore.case=TRUE)
df
 VAR   VAL
1   1  blue
2   2   red
3   3 empty

Jim

On Mon, Aug 9, 2021 at 6:43 PM Luigi Marongiu  wrote:
>
> Hello,
> I have a dataframe where I would like to change the string of certain
> rows, essentially I am looking to remove some useless text from the
> variables.
> I tried with:
> ```
> > df = data.frame(VAR = 1:3, VAL = c("value is blue", "Value is red", 
> > "empty"))
> > df[df$VAL] = gsub("value is ", "", df$VAL, ignore.case = TRUE, perl = FALSE)
> > df
>   VAR   VAL value is blue Value is red empty
> 1   1 value is blue  blue blue  blue
> 2   2  Value is red   red  red   red
> 3   3 empty emptyempty empty
> ```
> which is of course wrong because I was expecting
> ```
>   VAR   VAL
> 1   1 blue
> 2   2 red
> 3   3empty
> ```
> What is the correct syntax in these cases?
> Thank you
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply a color range (kind of like a heat map) to the values in each cell of a data frame

2021-06-18 Thread Bert Gunter
I haven't followed this closely, but you might wish to check the
stats::image() function to see if it might give you what you want (perhaps
with a little finagling). Feel free to ignore if you are happy with what
you have. As Jim said, there are lots of functions in various packages that
do this sort of thing.

Cheers,
Bert

On Fri, Jun 18, 2021 at 11:47 AM Yuan Chun Ding  wrote:

> Hi Jim,
>
> Thank you so much for your help!
>
> I looked into the plotrix package, the addtable2plot function can do
> somewhat I want by creating an empty plot first.
>
> Ding
>
> -Original Message-
> From: Jim Lemon [mailto:[email protected]]
> Sent: Thursday, June 17, 2021 8:29 PM
> To: Yuan Chun Ding 
> Cc: [email protected]
> Subject: Re: [R] apply a color range (kind of like a heat map) to the
> values in each cell of a data frame
>
> Hi Ding,
> There are a number of "value to color" functions in various packages.
> One is "color.scale" in the plotrix package:
>
> library(plotrix)
> s1
> <-c(0.085,0.086,0.139,0.129,0.235,0.177,0.000,0.126,0.271,0.000,0.083,0.163)
> s2
> <-c(0.000,0.093,0.000,0.080,0.072,0.388,0.138,0.107,0.000,0.000,0.474,0.000)
> s13
> <-c(0.000,0.077,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.166,0.308,0.000)
> s3
> <-c(0.000,0.478,0.000,0.332,0.163,0.000,0.000,0.000,0.145,0.469,0.000,0.000)
> test1 <-data.frame(s1,s2,s13,s3)
> # generate a matrix of colors
> test1.col<-color.scale(as.matrix(test1),extremes=c("white","blue"))
>
> color.scale calculates colors using linear interpolation of the values in
> three color spaces (RGB, HSV or HCL). You may be interested in some of the
> plotting functions in plotrix that employ color.scale, particularly
> "size_n_color".
>
> Jim
>
> On Fri, Jun 18, 2021 at 10:04 AM Yuan Chun Ding  wrote:
> >
> > Dear R users,
> >
> > I have a numeric table with 140 rows and 30 columns, here I only made
> partial table,  test1,  as an example.  I want to apply a blue color range
> to the value in each cell of  the data frame test1.
> > I found some R code using DT library. However, I only can see the
> colored table at my R studio viewer window, also only show the first 10
> rows.  I hope to save the colored table into a PNG file and want to modify
> the size of each cell, so I can apply the code to a much bigger table with
> 140 rows and 30 column.
> >
> > Can you help me?
> >
> > Thank you very much!!
> >
> > Ding
> >
> > s1
> > <-c(0.085,0.086,0.139,0.129,0.235,0.177,0.000,0.126,0.271,0.000,0.083,
> > 0.163)
> > s2
> > <-c(0.000,0.093,0.000,0.080,0.072,0.388,0.138,0.107,0.000,0.000,0.474,
> > 0.000)
> > s13
> > <-c(0.000,0.077,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.166,0.308,
> > 0.000)
> > s3
> > <-c(0.000,0.478,0.000,0.332,0.163,0.000,0.000,0.000,0.145,0.469,0.000,
> > 0.000)
> > test1 <-data.frame(s1,s2,s13,s3)
> > row.names(test1) > COH_006","COH_007","COH_008","COH_009","COH_010","COH_011","COH_012")
> >
> > library(DT)
> >
> > dtable1 <- datatable(test1, rownames=TRUE, options = list(lengthChange
> > = FALSE, dom='t'))
> >
> > colRamp <- colorRamp(c("white","blue")) for(column in names(test1)){
> >   x <- na.omit(test1[[column]])
> >   brks <- quantile(x, probs = seq(.05, .95, .01))
> >   RGB <- colRamp(c(0, (brks-min(x))/(max(x)-min(x
> >   clrs <- apply(RGB, 1, function(rgb){
> > sprintf("rgb(%s)", toString(round(rgb,0)))
> >   })
> >   dtable1 <- dtable1 %>%
> > formatStyle(column, backgroundColor = styleInterval(brks, clrs)) }
> >
> > dtable1
> >
> > --
> > 
> > -SECURITY/CONFIDENTIALITY WARNING-
> >
> > This message and any attachments are intended solely for the
> > individual or entity to which they are addressed. This communication
> > may contain information that is privileged, confidential, or exempt
> > from disclosure under applicable law (e.g., personal health
> > information, research data, financial information). Because this
> > e-mail has been sent without encryption, individuals other than the
> > intended recipient may be able to view the information, forward it to
> > others or tamper with the information without the knowledge or consent
> > of the sender. If you are not the intended recipient

Re: [R] apply a color range (kind of like a heat map) to the values in each cell of a data frame

2021-06-18 Thread Yuan Chun Ding
Hi Jim,

Thank you so much for your help!

I looked into the plotrix package, the addtable2plot function can do somewhat I 
want by creating an empty plot first.  

Ding

-Original Message-
From: Jim Lemon [mailto:[email protected]] 
Sent: Thursday, June 17, 2021 8:29 PM
To: Yuan Chun Ding 
Cc: [email protected]
Subject: Re: [R] apply a color range (kind of like a heat map) to the values in 
each cell of a data frame

Hi Ding,
There are a number of "value to color" functions in various packages.
One is "color.scale" in the plotrix package:

library(plotrix)
s1 <-c(0.085,0.086,0.139,0.129,0.235,0.177,0.000,0.126,0.271,0.000,0.083,0.163)
s2 <-c(0.000,0.093,0.000,0.080,0.072,0.388,0.138,0.107,0.000,0.000,0.474,0.000)
s13 <-c(0.000,0.077,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.166,0.308,0.000)
s3 <-c(0.000,0.478,0.000,0.332,0.163,0.000,0.000,0.000,0.145,0.469,0.000,0.000)
test1 <-data.frame(s1,s2,s13,s3)
# generate a matrix of colors
test1.col<-color.scale(as.matrix(test1),extremes=c("white","blue"))

color.scale calculates colors using linear interpolation of the values in three 
color spaces (RGB, HSV or HCL). You may be interested in some of the plotting 
functions in plotrix that employ color.scale, particularly "size_n_color".

Jim

On Fri, Jun 18, 2021 at 10:04 AM Yuan Chun Ding  wrote:
>
> Dear R users,
>
> I have a numeric table with 140 rows and 30 columns, here I only made partial 
> table,  test1,  as an example.  I want to apply a blue color range to the 
> value in each cell of  the data frame test1.
> I found some R code using DT library. However, I only can see the colored 
> table at my R studio viewer window, also only show the first 10 rows.  I hope 
> to save the colored table into a PNG file and want to modify the size of each 
> cell, so I can apply the code to a much bigger table with 140 rows and 30 
> column.
>
> Can you help me?
>
> Thank you very much!!
>
> Ding
>
> s1 
> <-c(0.085,0.086,0.139,0.129,0.235,0.177,0.000,0.126,0.271,0.000,0.083,
> 0.163)
> s2 
> <-c(0.000,0.093,0.000,0.080,0.072,0.388,0.138,0.107,0.000,0.000,0.474,
> 0.000)
> s13 
> <-c(0.000,0.077,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.166,0.308,
> 0.000)
> s3 
> <-c(0.000,0.478,0.000,0.332,0.163,0.000,0.000,0.000,0.145,0.469,0.000,
> 0.000)
> test1 <-data.frame(s1,s2,s13,s3)
> row.names(test1) COH_006","COH_007","COH_008","COH_009","COH_010","COH_011","COH_012")
>
> library(DT)
>
> dtable1 <- datatable(test1, rownames=TRUE, options = list(lengthChange 
> = FALSE, dom='t'))
>
> colRamp <- colorRamp(c("white","blue")) for(column in names(test1)){
>   x <- na.omit(test1[[column]])
>   brks <- quantile(x, probs = seq(.05, .95, .01))
>   RGB <- colRamp(c(0, (brks-min(x))/(max(x)-min(x
>   clrs <- apply(RGB, 1, function(rgb){
> sprintf("rgb(%s)", toString(round(rgb,0)))
>   })
>   dtable1 <- dtable1 %>%
> formatStyle(column, backgroundColor = styleInterval(brks, clrs)) }
>
> dtable1
>
> --
> 
> -SECURITY/CONFIDENTIALITY WARNING-
>
> This message and any attachments are intended solely for the 
> individual or entity to which they are addressed. This communication 
> may contain information that is privileged, confidential, or exempt 
> from disclosure under applicable law (e.g., personal health 
> information, research data, financial information). Because this 
> e-mail has been sent without encryption, individuals other than the 
> intended recipient may be able to view the information, forward it to 
> others or tamper with the information without the knowledge or consent 
> of the sender. If you are not the intended recipient, or the employee 
> or person responsible for delivering the message to the intended 
> recipient, any dissemination, distribution or copying of the 
> communication is strictly prohibited. If you received the 
> communication in error, please notify the sender immediately by 
> replying to this message and deleting the message and any accompanying 
> files from your system. If, due to the security risks, you do not wish 
> to receive further communications via e-mail, please reply to this 
> message and inform the sender that you do not wish to receive further 
> e-mail from the sender. (LCP301)
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see 
> https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-he
> lp__;!!Fou38LsQ

Re: [R] apply a color range (kind of like a heat map) to the values in each cell of a data frame

2021-06-17 Thread Jim Lemon
Hi Ding,
There are a number of "value to color" functions in various packages.
One is "color.scale" in the plotrix package:

library(plotrix)
s1 <-c(0.085,0.086,0.139,0.129,0.235,0.177,0.000,0.126,0.271,0.000,0.083,0.163)
s2 <-c(0.000,0.093,0.000,0.080,0.072,0.388,0.138,0.107,0.000,0.000,0.474,0.000)
s13 <-c(0.000,0.077,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.166,0.308,0.000)
s3 <-c(0.000,0.478,0.000,0.332,0.163,0.000,0.000,0.000,0.145,0.469,0.000,0.000)
test1 <-data.frame(s1,s2,s13,s3)
# generate a matrix of colors
test1.col<-color.scale(as.matrix(test1),extremes=c("white","blue"))

color.scale calculates colors using linear interpolation of the values
in three color spaces (RGB, HSV or HCL). You may be interested in some
of the plotting functions in plotrix that employ color.scale,
particularly "size_n_color".

Jim

On Fri, Jun 18, 2021 at 10:04 AM Yuan Chun Ding  wrote:
>
> Dear R users,
>
> I have a numeric table with 140 rows and 30 columns, here I only made partial 
> table,  test1,  as an example.  I want to apply a blue color range to the 
> value in each cell of  the data frame test1.
> I found some R code using DT library. However, I only can see the colored 
> table at my R studio viewer window, also only show the first 10 rows.  I hope 
> to save the colored table into a PNG file and want to modify the size of each 
> cell, so I can apply the code to a much bigger table with 140 rows and 30 
> column.
>
> Can you help me?
>
> Thank you very much!!
>
> Ding
>
> s1 
> <-c(0.085,0.086,0.139,0.129,0.235,0.177,0.000,0.126,0.271,0.000,0.083,0.163)
> s2 
> <-c(0.000,0.093,0.000,0.080,0.072,0.388,0.138,0.107,0.000,0.000,0.474,0.000)
> s13 
> <-c(0.000,0.077,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.166,0.308,0.000)
> s3 
> <-c(0.000,0.478,0.000,0.332,0.163,0.000,0.000,0.000,0.145,0.469,0.000,0.000)
> test1 <-data.frame(s1,s2,s13,s3)
> row.names(test1)
> library(DT)
>
> dtable1 <- datatable(test1, rownames=TRUE, options = list(lengthChange = 
> FALSE, dom='t'))
>
> colRamp <- colorRamp(c("white","blue"))
> for(column in names(test1)){
>   x <- na.omit(test1[[column]])
>   brks <- quantile(x, probs = seq(.05, .95, .01))
>   RGB <- colRamp(c(0, (brks-min(x))/(max(x)-min(x
>   clrs <- apply(RGB, 1, function(rgb){
> sprintf("rgb(%s)", toString(round(rgb,0)))
>   })
>   dtable1 <- dtable1 %>%
> formatStyle(column, backgroundColor = styleInterval(brks, clrs))
> }
>
> dtable1
>
> --
> 
> -SECURITY/CONFIDENTIALITY WARNING-
>
> This message and any attachments are intended solely for the individual or 
> entity to which they are addressed. This communication may contain 
> information that is privileged, confidential, or exempt from disclosure under 
> applicable law (e.g., personal health information, research data, financial 
> information). Because this e-mail has been sent without encryption, 
> individuals other than the intended recipient may be able to view the 
> information, forward it to others or tamper with the information without the 
> knowledge or consent of the sender. If you are not the intended recipient, or 
> the employee or person responsible for delivering the message to the intended 
> recipient, any dissemination, distribution or copying of the communication is 
> strictly prohibited. If you received the communication in error, please 
> notify the sender immediately by replying to this message and deleting the 
> message and any accompanying files from your system. If, due to the security 
> risks, you do not wish to r
 eceive further communications via e-mail, please reply to this message and 
inform the sender that you do not wish to receive further e-mail from the 
sender. (LCP301)
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply and cor()

2017-05-12 Thread Micha Silver



On 05/12/2017 06:53 PM, David L Carlson wrote:

Actually, not using apply() would be faster and simpler

cor(t(compare_data), t(test_data))

Worked just as I wanted, thanks!
I first reshaped the data frames, then I didn't even need the t()


David C

-Original Message-
From: R-help [mailto:[email protected]] On Behalf Of David L Carlson
Sent: Friday, May 12, 2017 10:48 AM
To: Ismail SEZEN ; Micha Silver 
Cc: [email protected]
Subject: Re: [R] apply and cor()

Actually, r is a vector, not an index value. You need

apply(compare_data, 1, function(r) cor(r, t(test_data)))

-
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352



-Original Message-
From: R-help [mailto:[email protected]] On Behalf Of Ismail SEZEN
Sent: Friday, May 12, 2017 10:11 AM
To: Micha Silver 
Cc: [email protected]
Subject: Re: [R] apply and cor()



On 12 May 2017, at 17:57, Micha Silver  wrote:

I have two data.frames, one with a single row of 31 columns, and the second 
with 269 rows and the same 31 columns.

dim(compare_data)

[1] 269  31

dim(test_data)

[1]  1 31

I want to apply cor() between the one row of 'test_data', and each row of the 
'compare_data' .
I tried 'apply' but I get this error:

apply(compare_data, 1, function(r) {cor(compare_data[r,], test_data)})

Error in cor(compare_data[r, ], test_data) : incompatible dimensions


apply(compare_data, 1, function(r) {cor(compare_data[r,], 
as.numeric(test_data))})

See ?cor. Explanation of y is "NULL (default) or a vector, matrix or data frame 
with compatible dimensions to x”.



In order to try to understand I did:

dims <- apply(compare_data, 1, function(r) {dim(compare_data[r,])})
head(dims)

 20 73 103 118 130 142 151 154 191 205 217 222 227 232 240 275 282 301 320 
359 360 551 589 653 789 801 808 812
[1,]  9  8   8   9   5   9   8  11   6  15  12  13  10   7   9  14 8  11   9  
11  11  12   9  14   5   8   9  10
[2,] 31 31  31  31  31  31  31  31  31  31  31  31  31  31  31  31 31  31  31  
31  31  31  31  31  31  31  31  31
 840 856 857 867 885 970 983 985 1103 1107 1197 1207 1237 1262 1279 1282 
1332 1357 1358 1392 1411 1435 1458 1473
[1,]  14  11  12   8  10   2   7   9   108   10   13   11 79   12   11  
 11   16   10   10   12   10   10
[2,]  31  31  31  31  31  31  31  31   31   31   31   31   31   31 31   31   31 
  31   31   31   31   31   31   31

and indeed I am getting different row dimensions. I expected "1 31" for each. 
What are the values 9,8,8,9,5... in the [1,] dimension?

If I test the compare_data data.frame one row at a time:

dim(compare_data['20',])

[1]  1 31

dim(compare_data['1473',])

[1]  1 31

It looks as I expected.
What am I missing??

Thanks

--
Micha Silver
cell: +972-523-665918

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


--
Micha Silver
cell: +972-523-665918

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: [R] apply and cor()

2017-05-12 Thread David L Carlson
Actually, not using apply() would be faster and simpler

cor(t(compare_data), t(test_data))

David C

-Original Message-
From: R-help [mailto:[email protected]] On Behalf Of David L Carlson
Sent: Friday, May 12, 2017 10:48 AM
To: Ismail SEZEN ; Micha Silver 
Cc: [email protected]
Subject: Re: [R] apply and cor()

Actually, r is a vector, not an index value. You need

apply(compare_data, 1, function(r) cor(r, t(test_data)))

-
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352



-Original Message-
From: R-help [mailto:[email protected]] On Behalf Of Ismail SEZEN
Sent: Friday, May 12, 2017 10:11 AM
To: Micha Silver 
Cc: [email protected]
Subject: Re: [R] apply and cor()


> On 12 May 2017, at 17:57, Micha Silver  wrote:
> 
> I have two data.frames, one with a single row of 31 columns, and the second 
> with 269 rows and the same 31 columns.
> > dim(compare_data)
> [1] 269  31
> > dim(test_data)
> [1]  1 31
> 
> I want to apply cor() between the one row of 'test_data', and each row of the 
> 'compare_data' .
> I tried 'apply' but I get this error:
> > apply(compare_data, 1, function(r) {cor(compare_data[r,], test_data)})
> Error in cor(compare_data[r, ], test_data) : incompatible dimensions


apply(compare_data, 1, function(r) {cor(compare_data[r,], 
as.numeric(test_data))})

See ?cor. Explanation of y is "NULL (default) or a vector, matrix or data frame 
with compatible dimensions to x”.

> 
> 
> In order to try to understand I did:
> > dims <- apply(compare_data, 1, function(r) {dim(compare_data[r,])})
> > head(dims)
> 20 73 103 118 130 142 151 154 191 205 217 222 227 232 240 275 282 301 320 
> 359 360 551 589 653 789 801 808 812
> [1,]  9  8   8   9   5   9   8  11   6  15  12  13  10   7   9  14 8  11   9  
> 11  11  12   9  14   5   8   9  10
> [2,] 31 31  31  31  31  31  31  31  31  31  31  31  31  31  31  31 31  31  31 
>  31  31  31  31  31  31  31  31  31
> 840 856 857 867 885 970 983 985 1103 1107 1197 1207 1237 1262 1279 1282 
> 1332 1357 1358 1392 1411 1435 1458 1473
> [1,]  14  11  12   8  10   2   7   9   108   10   13   11 79   12   
> 11   11   16   10   10   12   10   10
> [2,]  31  31  31  31  31  31  31  31   31   31   31   31   31   31 31   31   
> 31   31   31   31   31   31   31   31
> 
> and indeed I am getting different row dimensions. I expected "1 31" for each. 
> What are the values 9,8,8,9,5... in the [1,] dimension?
> 
> If I test the compare_data data.frame one row at a time:
> > dim(compare_data['20',])
> [1]  1 31
> > dim(compare_data['1473',])
> [1]  1 31
> 
> It looks as I expected.
> What am I missing??
> 
> Thanks
> 
> -- 
> Micha Silver
> cell: +972-523-665918
> 
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: [R] apply and cor()

2017-05-12 Thread David L Carlson
Actually, r is a vector, not an index value. You need

apply(compare_data, 1, function(r) cor(r, t(test_data)))

-
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352



-Original Message-
From: R-help [mailto:[email protected]] On Behalf Of Ismail SEZEN
Sent: Friday, May 12, 2017 10:11 AM
To: Micha Silver 
Cc: [email protected]
Subject: Re: [R] apply and cor()


> On 12 May 2017, at 17:57, Micha Silver  wrote:
> 
> I have two data.frames, one with a single row of 31 columns, and the second 
> with 269 rows and the same 31 columns.
> > dim(compare_data)
> [1] 269  31
> > dim(test_data)
> [1]  1 31
> 
> I want to apply cor() between the one row of 'test_data', and each row of the 
> 'compare_data' .
> I tried 'apply' but I get this error:
> > apply(compare_data, 1, function(r) {cor(compare_data[r,], test_data)})
> Error in cor(compare_data[r, ], test_data) : incompatible dimensions


apply(compare_data, 1, function(r) {cor(compare_data[r,], 
as.numeric(test_data))})

See ?cor. Explanation of y is "NULL (default) or a vector, matrix or data frame 
with compatible dimensions to x”.

> 
> 
> In order to try to understand I did:
> > dims <- apply(compare_data, 1, function(r) {dim(compare_data[r,])})
> > head(dims)
> 20 73 103 118 130 142 151 154 191 205 217 222 227 232 240 275 282 301 320 
> 359 360 551 589 653 789 801 808 812
> [1,]  9  8   8   9   5   9   8  11   6  15  12  13  10   7   9  14 8  11   9  
> 11  11  12   9  14   5   8   9  10
> [2,] 31 31  31  31  31  31  31  31  31  31  31  31  31  31  31  31 31  31  31 
>  31  31  31  31  31  31  31  31  31
> 840 856 857 867 885 970 983 985 1103 1107 1197 1207 1237 1262 1279 1282 
> 1332 1357 1358 1392 1411 1435 1458 1473
> [1,]  14  11  12   8  10   2   7   9   108   10   13   11 79   12   
> 11   11   16   10   10   12   10   10
> [2,]  31  31  31  31  31  31  31  31   31   31   31   31   31   31 31   31   
> 31   31   31   31   31   31   31   31
> 
> and indeed I am getting different row dimensions. I expected "1 31" for each. 
> What are the values 9,8,8,9,5... in the [1,] dimension?
> 
> If I test the compare_data data.frame one row at a time:
> > dim(compare_data['20',])
> [1]  1 31
> > dim(compare_data['1473',])
> [1]  1 31
> 
> It looks as I expected.
> What am I missing??
> 
> Thanks
> 
> -- 
> Micha Silver
> cell: +972-523-665918
> 
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: [R] apply and cor()

2017-05-12 Thread Ismail SEZEN

> On 12 May 2017, at 17:57, Micha Silver  wrote:
> 
> I have two data.frames, one with a single row of 31 columns, and the second 
> with 269 rows and the same 31 columns.
> > dim(compare_data)
> [1] 269  31
> > dim(test_data)
> [1]  1 31
> 
> I want to apply cor() between the one row of 'test_data', and each row of the 
> 'compare_data' .
> I tried 'apply' but I get this error:
> > apply(compare_data, 1, function(r) {cor(compare_data[r,], test_data)})
> Error in cor(compare_data[r, ], test_data) : incompatible dimensions


apply(compare_data, 1, function(r) {cor(compare_data[r,], 
as.numeric(test_data))})

See ?cor. Explanation of y is "NULL (default) or a vector, matrix or data frame 
with compatible dimensions to x”.

> 
> 
> In order to try to understand I did:
> > dims <- apply(compare_data, 1, function(r) {dim(compare_data[r,])})
> > head(dims)
> 20 73 103 118 130 142 151 154 191 205 217 222 227 232 240 275 282 301 320 
> 359 360 551 589 653 789 801 808 812
> [1,]  9  8   8   9   5   9   8  11   6  15  12  13  10   7   9  14 8  11   9  
> 11  11  12   9  14   5   8   9  10
> [2,] 31 31  31  31  31  31  31  31  31  31  31  31  31  31  31  31 31  31  31 
>  31  31  31  31  31  31  31  31  31
> 840 856 857 867 885 970 983 985 1103 1107 1197 1207 1237 1262 1279 1282 
> 1332 1357 1358 1392 1411 1435 1458 1473
> [1,]  14  11  12   8  10   2   7   9   108   10   13   11 79   12   
> 11   11   16   10   10   12   10   10
> [2,]  31  31  31  31  31  31  31  31   31   31   31   31   31   31 31   31   
> 31   31   31   31   31   31   31   31
> 
> and indeed I am getting different row dimensions. I expected "1 31" for each. 
> What are the values 9,8,8,9,5... in the [1,] dimension?
> 
> If I test the compare_data data.frame one row at a time:
> > dim(compare_data['20',])
> [1]  1 31
> > dim(compare_data['1473',])
> [1]  1 31
> 
> It looks as I expected.
> What am I missing??
> 
> Thanks
> 
> -- 
> Micha Silver
> cell: +972-523-665918
> 
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: [R] apply weight to a data frame

2016-09-16 Thread PIKAL Petr
Hi

Pls, keep conversation on list, you can get answer from others, more capable 
than myself in survey analysis. As I said I am not an expert in this task but 
you seem to seek some statistical help. For this maybe stackexchange can be 
more appropriate.

If you want some help here, you should present some data (not necessarily 
real), some R code and what did you expect and did not get.

I went through help for survey and plm but did not find any mention that those 
two packages are somehow related. In some survey functions you can use weight 
argument but I did not find any note that these functions could result in 
object suitable for plm

Cheers
Petr

From: laura roncaglia [mailto:[email protected]]
Sent: Friday, September 16, 2016 9:36 AM
To: PIKAL Petr 
Subject: Re: [R] apply weight to a data frame

Thank you for your answer.

The data frame contains social and economic variables (like sex, education 
level, age, income, wealth and so on); since the survey is conduct on a number 
of family smaller than the national one, I should use survey weights to make 
the survey's data more representative (the survey data could be influenced by 
the family choosen, using the survey weights the data should be adjusted).

Now I am trying  to get a weighted data frame using the survey package (to 
replicate weights) ; in this way I hope I will be able to use the plm package 
without problems.

Do you think it could be right?

2016-09-16 9:21 GMT+02:00 PIKAL Petr 
mailto:[email protected]>>:
Hi

I do not know much about plm but your request seems to me pretty cryptic, 
probably even for knowledgeable person.

There is no weight argument in plm call so I wonder how do you want to "weight 
data frame".

The only weighting scheme for data frame I can imagine is to repeat rows 
somehow according to weight value, but I do not consider it proper way.

You should probably explain what do you mean by data frame weighing to get 
relevant answer.
But I may be completely out of understanding your problem.

Cheers
Petr

> -Original Message-
> From: R-help 
> [mailto:[email protected]<mailto:[email protected]>] On 
> Behalf Of laura
> roncaglia
> Sent: Thursday, September 15, 2016 6:40 PM
> To: [email protected]<mailto:[email protected]>
> Subject: [R] apply weight to a data frame
>
> I am a beginner user of R.
>
> I am writing the master thesis using a data frame from a national survey.
> The data frame contains several variables, one of which contains the survey
> weights.
>
> I need to apply the survey weights to the data frame, in order to use the data
> frame with the plm package (I need to run a fixed effect analysis).
>
> I know that I could use packages different from plm, but I am more
> interested in weighting the data frame.
>
> Thank you in advance.
>
>   [[alternative HTML version deleted]]
>
> __
> [email protected]<mailto:[email protected]> mailing list -- To 
> UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.





Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with

Re: [R] apply weight to a data frame

2016-09-16 Thread PIKAL Petr
Hi

I do not know much about plm but your request seems to me pretty cryptic, 
probably even for knowledgeable person.

There is no weight argument in plm call so I wonder how do you want to "weight 
data frame".

The only weighting scheme for data frame I can imagine is to repeat rows 
somehow according to weight value, but I do not consider it proper way.

You should probably explain what do you mean by data frame weighing to get 
relevant answer.
But I may be completely out of understanding your problem.

Cheers
Petr

> -Original Message-
> From: R-help [mailto:[email protected]] On Behalf Of laura
> roncaglia
> Sent: Thursday, September 15, 2016 6:40 PM
> To: [email protected]
> Subject: [R] apply weight to a data frame
>
> I am a beginner user of R.
>
> I am writing the master thesis using a data frame from a national survey.
> The data frame contains several variables, one of which contains the survey
> weights.
>
> I need to apply the survey weights to the data frame, in order to use the data
> frame with the plm package (I need to run a fixed effect analysis).
>
> I know that I could use packages different from plm, but I am more
> interested in weighting the data frame.
>
> Thank you in advance.
>
>   [[alternative HTML version deleted]]
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.


Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately 
accept such offer; The sender of this e-mail (offer) excludes any acceptance of 
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an 
express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into 
any contracts on behalf of the company except for cases in which he/she is 
expressly authorized to do so in writing, and such authorization or power of 
attorney is submitted to the recipient or the person represented by the 
recipient, or the existence of such authorization is known to the recipient of 
the person represented by the recipient.
__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: [R] Apply a multi-variable function to a vector

2016-09-13 Thread S Ellison
> I would like to define an arbitrary function of an arbitrary number of 
> variables,
> for example, for 2 variables:
> 
> func2 <- function(time, temp) time + temp
> 
> I'd like to keep variable names that have a meaning in the problem (time and
> temperature above).

Not quite enough information here.

If we called 
func2(30, 298, 23)

which has an arbitrary third argument, what would you like to happen to the 
third argument? And a fourth, fifth and so on? Something consistent for all 
arguments, or something different for each depending on its (arbitrary) name?


S Ellison





***
This email and any attachments are confidential. Any use...{{dropped:8}}

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply a multi-variable function to a vector

2016-09-12 Thread Stephen Kennedy
Hello Jeff,

I kept fooling with this, and also looking around the web and I actually
found something on stackoverflow, which does what I had in mind.  You
mentioned that you would rarely use someting like this, but the link is:

http://stackoverflow.com/questions/6192848/how-to-generalize-outer-to-n-dimensions

This may not be efficient, but what I like is that you can name the vectors
of variable values anything, as well as the names of the variables in the
arbitrary function.  It is not sensitive to the 'names' of anything.

Here is the code.  I used the function you defined for me, but for example
you can just pass in a vector of values (1:3, etc. below) and this produces
the output array.  I would name the vector something that means something
to me (temperature_vals = c(), etc.), and then pass that to multi.outer,
and the function will be applied (independent of what you name the
variables in the function).

Thanks again for your help.  The responses are like a tutorial for me ...

Best,

Steve



list_args <- Vectorize( function(a,b) c( as.list(a), as.list(b) ),
+ SIMPLIFY = FALSE)

 make_args_mtx <- function( alist ) {
+ Reduce(function(x, y) outer(x, y, list_args), alist)
+ }

 multi.outer <- function(f, ... ) {
+ args <- make_args_mtx(list(...))
+ apply(args, 1:length(dim(args)), function(a) do.call(f, a[[1]] ) )
+ }


multi.outer(myfunc, 1:3, 2:6, 3:4)
, , 1

 [,1] [,2] [,3] [,4] [,5]
[1,]56789
[2,]79   11   13   15
[3,]9   12   15   18   21

, , 2

 [,1] [,2] [,3] [,4] [,5]
[1,]6789   10
[2,]8   10   12   14   16
[3,]   10   13   16   19   22

On Sat, Sep 10, 2016 at 1:29 AM, Jeff Newmiller 
wrote:

> Not sure I understand what you really want, if you have found ways to
> accomplish what you want but are not satisfied with them. That is one
> reason why keeping the mailing list involved (by reply-all) is good for
> you. From my end, I don't do one-on-one support online, and may not be able
> to carry on a thread to the end if I get busy.
>
> Your concept of a generalized outer function sounds to me like:
>
> myfunc <- function( A, B, C ) {
>  A * B + C
> }
>
> gouter <- function( FUN, ... ) {
>  args <- list( ... )
>  DF <- do.call( expand.grid, args )
>  array( data = do.call( FUN, DF )
>   , dim = sapply( args, FUN=length )
>   , dimnames = args
>   )
> }
>
> gouter( myfunc, A = 1:3, B=2:6, C=3:4 )
> # , , C = 3
> #
> #B
> # A   2  3  4  5  6
> #   1 5  6  7  8  9
> #   2 7  9 11 13 15
> #   3 9 12 15 18 21
> #
> # , , C = 4
> #
> #B
> # A2  3  4  5  6
> #   1  6  7  8  9 10
> #   2  8 10 12 14 16
> #   3 10 13 16 19 22
>
> I generally just tack on columns to the expand.grid result... I almost
> never have a need for multidimensional arrays.
>
> On Fri, 9 Sep 2016, Steve Kennedy wrote:
>
> Hello,
>>
>> Abstraction is what I want.  I'm actually looking to do something more
>> complicated.  The functions do.call, and as.list get me most of the way
>> there, but there is something I'm missing ...
>>
>> My eventual goal is to produce a multi-dimensional version of 'outer'.
>> Like my.outer(func, a_vec, b_vec, c_vec, ..), where the function of the
>> variables 'a', 'b', 'c', etc. would be applied to the vectors from the
>> outer product of the vectors of values for each variable.
>>
>> I wanted to use expand.grid (does require reshaping the output).  Using
>> temps = c(40,50,60) and times = c(1:5), this doesn't quite seem to work:
>>
>>   apply(expand.grid(temps,times), 1, function(a) do.call(func2,
>> as.list(a)))
>>
>> although this does work:
>>
>>   do.call(func2, as.list(c(10, 121)))
>>
>> And, this also works:
>>
>>  apply(expand.grid(temps,times), 1, function(a) do.call("+", as.list(a)))
>>
>> There is some subtlety here I don't understand.
>>
>> Thanks,
>>
>> Steve
>>
>> -Original Message-
>> From: Jeff Newmiller [mailto:[email protected]]
>> Sent: Friday, September 09, 2016 5:39 PM
>> To: Steve Kennedy; [email protected]
>> Subject: Re: [R] Apply a multi-variable function to a vector
>>
>> Your architecture has a bad smell to me. For one thing you are mixing
>> different units in the same vector but should be putting multiple instances
>> of the same variable into one vector. Lists of vectors (data frames) are
>> typically used when multiple variables need to be grouped.
>>
>> Another problem is that you are constraining the names of the variables
>> you pass to 

Re: [R] Apply a multi-variable function to a vector

2016-09-10 Thread Stephen Kennedy
Thanks.  I have gotten some replies.  One problem was that I was not
passing the names of the vectors to expand.grid.  I didn't think I had to
do that and that caused problems with do.call.

I wanted to just define the vectors of variables values, the function,
func, and then pass that to my.outer.

I was using A <- c( ... )

Then, expand.grid(A, etc.) with do.call and as.list and without the names
use in 'fund', there was an error.  I didn't think it would matter what
names I used in defining the function.

Thanks very much.  I think I have some good alternatives that all work.

Steve


On Sat, Sep 10, 2016 at 1:29 AM, Jeff Newmiller 
wrote:

> Not sure I understand what you really want, if you have found ways to
> accomplish what you want but are not satisfied with them. That is one
> reason why keeping the mailing list involved (by reply-all) is good for
> you. From my end, I don't do one-on-one support online, and may not be able
> to carry on a thread to the end if I get busy.
>
> Your concept of a generalized outer function sounds to me like:
>
> myfunc <- function( A, B, C ) {
>  A * B + C
> }
>
> gouter <- function( FUN, ... ) {
>  args <- list( ... )
>  DF <- do.call( expand.grid, args )
>  array( data = do.call( FUN, DF )
>   , dim = sapply( args, FUN=length )
>   , dimnames = args
>   )
> }
>
> gouter( myfunc, A = 1:3, B=2:6, C=3:4 )
> # , , C = 3
> #
> #B
> # A   2  3  4  5  6
> #   1 5  6  7  8  9
> #   2 7  9 11 13 15
> #   3 9 12 15 18 21
> #
> # , , C = 4
> #
> #B
> # A2  3  4  5  6
> #   1  6  7  8  9 10
> #   2  8 10 12 14 16
> #   3 10 13 16 19 22
>
> I generally just tack on columns to the expand.grid result... I almost
> never have a need for multidimensional arrays.
>
> On Fri, 9 Sep 2016, Steve Kennedy wrote:
>
> Hello,
>>
>> Abstraction is what I want.  I'm actually looking to do something more
>> complicated.  The functions do.call, and as.list get me most of the way
>> there, but there is something I'm missing ...
>>
>> My eventual goal is to produce a multi-dimensional version of 'outer'.
>> Like my.outer(func, a_vec, b_vec, c_vec, ..), where the function of the
>> variables 'a', 'b', 'c', etc. would be applied to the vectors from the
>> outer product of the vectors of values for each variable.
>>
>> I wanted to use expand.grid (does require reshaping the output).  Using
>> temps = c(40,50,60) and times = c(1:5), this doesn't quite seem to work:
>>
>>   apply(expand.grid(temps,times), 1, function(a) do.call(func2,
>> as.list(a)))
>>
>> although this does work:
>>
>>   do.call(func2, as.list(c(10, 121)))
>>
>> And, this also works:
>>
>>  apply(expand.grid(temps,times), 1, function(a) do.call("+", as.list(a)))
>>
>> There is some subtlety here I don't understand.
>>
>> Thanks,
>>
>> Steve
>>
>> -Original Message-
>> From: Jeff Newmiller [mailto:[email protected]]
>> Sent: Friday, September 09, 2016 5:39 PM
>> To: Steve Kennedy; [email protected]
>> Subject: Re: [R] Apply a multi-variable function to a vector
>>
>> Your architecture has a bad smell to me. For one thing you are mixing
>> different units in the same vector but should be putting multiple instances
>> of the same variable into one vector. Lists of vectors (data frames) are
>> typically used when multiple variables need to be grouped.
>>
>> Another problem is that you are constraining the names of the variables
>> you pass to the function to be named the same as they are inside the
>> function. This really limits your use of those functions.
>>
>> There really is too much abstraction going on here.
>> --
>> Sent from my phone. Please excuse my brevity.
>>
>> On September 9, 2016 12:44:52 PM PDT, Steve Kennedy
>>  wrote:
>>
>>> Hello,
>>>
>>> I would like to define an arbitrary function of an arbitrary number of
>>> variables, for example, for 2 variables:
>>>
>>> func2 <- function(time, temp) time + temp
>>>
>>> I'd like to keep variable names that have a meaning in the problem
>>> (time and temperature above).
>>>
>>> If I have a vector of values for these variables, for example in the
>>> 2-d case, c(10, 121), I'd like to apply my function (in this case
>>> func2) and obtain the result. Conceptually, something like,
>>>
>>> func2(c(10,121))
>>&g

Re: [R] Apply a multi-variable function to a vector

2016-09-09 Thread Jeff Newmiller
Not sure I understand what you really want, if you have found ways to 
accomplish what you want but are not satisfied with them. That is one 
reason why keeping the mailing list involved (by reply-all) is good for 
you. From my end, I don't do one-on-one support online, and may not be 
able to carry on a thread to the end if I get busy.


Your concept of a generalized outer function sounds to me like:

myfunc <- function( A, B, C ) {
 A * B + C
}

gouter <- function( FUN, ... ) {
 args <- list( ... )
 DF <- do.call( expand.grid, args )
 array( data = do.call( FUN, DF )
  , dim = sapply( args, FUN=length )
  , dimnames = args
  )
}

gouter( myfunc, A = 1:3, B=2:6, C=3:4 )
# , , C = 3
#
#B
# A   2  3  4  5  6
#   1 5  6  7  8  9
#   2 7  9 11 13 15
#   3 9 12 15 18 21
#
# , , C = 4
#
#B
# A2  3  4  5  6
#   1  6  7  8  9 10
#   2  8 10 12 14 16
#   3 10 13 16 19 22

I generally just tack on columns to the expand.grid result... I almost 
never have a need for multidimensional arrays.


On Fri, 9 Sep 2016, Steve Kennedy wrote:


Hello,

Abstraction is what I want.  I'm actually looking to do something more 
complicated.  The functions do.call, and as.list get me most of the way 
there, but there is something I'm missing ...


My eventual goal is to produce a multi-dimensional version of 'outer'. 
Like my.outer(func, a_vec, b_vec, c_vec, ..), where the function of the 
variables 'a', 'b', 'c', etc. would be applied to the vectors from the 
outer product of the vectors of values for each variable.


I wanted to use expand.grid (does require reshaping the output).  Using 
temps = c(40,50,60) and times = c(1:5), this doesn't quite seem to work:


  apply(expand.grid(temps,times), 1, function(a) do.call(func2, as.list(a)))

although this does work:

  do.call(func2, as.list(c(10, 121)))

And, this also works:

 apply(expand.grid(temps,times), 1, function(a) do.call("+", as.list(a)))

There is some subtlety here I don't understand.

Thanks,

Steve

-Original Message-
From: Jeff Newmiller [mailto:[email protected]]
Sent: Friday, September 09, 2016 5:39 PM
To: Steve Kennedy; [email protected]
Subject: Re: [R] Apply a multi-variable function to a vector

Your architecture has a bad smell to me. For one thing you are mixing 
different units in the same vector but should be putting multiple 
instances of the same variable into one vector. Lists of vectors (data 
frames) are typically used when multiple variables need to be grouped.


Another problem is that you are constraining the names of the variables 
you pass to the function to be named the same as they are inside the 
function. This really limits your use of those functions.


There really is too much abstraction going on here.
--
Sent from my phone. Please excuse my brevity.

On September 9, 2016 12:44:52 PM PDT, Steve Kennedy 
 wrote:

Hello,

I would like to define an arbitrary function of an arbitrary number of
variables, for example, for 2 variables:

func2 <- function(time, temp) time + temp

I'd like to keep variable names that have a meaning in the problem
(time and temperature above).

If I have a vector of values for these variables, for example in the
2-d case, c(10, 121), I'd like to apply my function (in this case
func2) and obtain the result. Conceptually, something like,

func2(c(10,121))

becomes

func2(10,121)

Is there a simple way to accomplish this, for an arbitrary number of
variables?  I'd like something that would simply work from the
definition of the function.  If that is possible.

Thanks,

Steve Kennedy

CONFIDENTIALITY NOTICE: This e-mail message, including
a...{{dropped:11}}

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
http://cp.mcafee.com/d/k-Kr6x0g6hASyMepov78FI6XCQXLK3AnCrFCQXLK3AnCnAPq
tTT1ObPdSjqaby8VMsUyYrEl-4fgb0HoiaXcDYtmZKsHkVsTI95tCj-eHuTelGsKrpYwCOw
evW_ccnpuKNRXBQQT1TfcFzCnTeEyCJtdmXP_axVZicHs3jq9JcTvANOoVcsCej76XCOsVH
kiPajSvvcCatoDwCHIcfBisEeRO9sDVWNIhgxVxmhUagJ3AdcOFRJVKxJBxdcS2_id41Fr1
pFtd40wIIumd46Cy1lI-syVDoOQwvVEwtrxqsGMd44WCy3jh0p-QWNdLECZzL1
PLEASE do read the posting guide
http://cp.mcafee.com/d/5fHCMUp418SyMepov78FI6XCQXLK3AnCrFCQXLK3AnCnAPqt
TT1ObPdSjqaby8VMsUyYrEl-4fgb0HoiaXcDYtmZKsHkVsTI95tCj-eHuTelGsKrpYwCOwe
vW_ccnpuKNRXBQQT1TfcFzCnTeEyCJtdmXP_axVZicHs3jqpJcTvANOoVcsCej76XCM0gbb
HhG8_qv00smHisE4iV5Ki7Y3zoyx3P2IzMkxq78qpBjHrPt3rb2qpI5-Aq83iS2PiWq811p
oYIq8dd42HpYV5PeNBF0_Ph0WT2QVlwq89Rd46Cy0PZFRyrvhd_2KV
and provide commented, minimal, self-contained, reproducible code.


CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, 
contains information belonging to Anika Therapeutics, Inc. and is for the sole 
use of the intended recipient(s) and may contain confidential, proprietary, 
copyrighted and privileged information. Any unauthorized review, use, 
disclosure, distribution or c

Re: [R] Apply a multi-variable function to a vector

2016-09-09 Thread Jeff Newmiller
Your architecture has a bad smell to me. For one thing you are mixing different 
units in the same vector but should be putting multiple instances of the same 
variable into one vector. Lists of vectors (data frames) are typically used 
when multiple variables need to be grouped.

Another problem is that you are constraining the names of the variables you 
pass to the function to be named the same as they are inside the function. This 
really limits your use of those functions.

There really is too much abstraction going on here.
-- 
Sent from my phone. Please excuse my brevity.

On September 9, 2016 12:44:52 PM PDT, Steve Kennedy 
 wrote:
>Hello,
>
>I would like to define an arbitrary function of an arbitrary number of
>variables, for example, for 2 variables:
>
>func2 <- function(time, temp) time + temp
>
>I'd like to keep variable names that have a meaning in the problem
>(time and temperature above).
>
>If I have a vector of values for these variables, for example in the
>2-d case, c(10, 121), I'd like to apply my function (in this case
>func2) and obtain the result. Conceptually, something like,
>
>func2(c(10,121))
>
>becomes
>
>func2(10,121)
>
>Is there a simple way to accomplish this, for an arbitrary number of
>variables?  I'd like something that would simply work from the
>definition of the function.  If that is possible.
>
>Thanks,
>
>Steve Kennedy
>
>CONFIDENTIALITY NOTICE: This e-mail message, including
>a...{{dropped:11}}
>
>__
>[email protected] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply a multi-variable function to a vector

2016-09-09 Thread William Dunlap via R-help
Try do.call(), as in

> func2 <- function(time, temp) paste(time, temp)
> func2(121, 10)
[1] "121 10"
> do.call(func2, as.list(c(121,10)))
[1] "121 10"
> do.call(func2, list(121,10))
[1] "121 10"
>
> func2(121, time=10:12)
[1] "10 121" "11 121" "12 121"
> do.call(func2, list(121,time=10:12))
[1] "10 121" "11 121" "12 121"


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, Sep 9, 2016 at 12:50 PM, Stephen Kennedy 
wrote:

> Hello,
>
>
>
> I would like to define an arbitrary function of an arbitrary number of
> variables, for example, for 2 variables:
>
>
>
> func2 <- function(time, temp) time + temp
>
>
>
> I'd like to keep variable names that have a meaning in the problem (time
> and temperature above).
>
>
>
> If I have a vector of values for these variables, for example in the 2-d
> case, c(10, 121), I'd like to apply my function (in this case func2) and
> obtain the result. Conceptually, something like,
>
>
>
> func2(c(10,121))
>
>
>
> becomes
>
>
>
> func2(10,121)
>
>
>
> Is there a simple way to accomplish this, for an arbitrary number of
> variables?  I’d like something that would simply work from the definition
> of the function.  If that is possible.
>
>
>
> Thanks,
>
>
>
> Steve Kennedy
>
> [[alternative HTML version deleted]]
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: [R] apply and cousins

2016-06-09 Thread John Logsdon
Thanks Jim and others (and sorry Jim - an early version of this slipped
into your inbox :))

Apologies for not giving some concrete code - I was trying to explain in
words.

What I need to do is to fit a simple linear model to successive sections
of a long matrix.

So far, the best solution I have come up with uses apply twice:

Generate some data in a 10*3 matrix:

N = 10
Z = cbind(1:N,cumsum(rnorm(N,1,0.01)),rnorm(N,1.2,0.1)) #

where the first column is an index, the second a monotonic increasing
value representing time and the third just the measurements I want to
process.

Then write a function dVals1:

dVals1 = function(Y,DD,dT){which.min((Y[2] - dT) > DD[,2])))

which will identify the first row where the time is greater than current
time - dT.

So to identify the start of the data (say) 10 units before for each row,
we use apply and prepended this as a column to the array for later use:

ZZ = cbind(apply(Z,1,dVals1,Z,10),Z)

There may be some cases, particularly at the start, where later values are
extracted because the minimum returned by which.min is 1.

I now have start and finish pointers for each position so can proceed to
fit a simple linear model with the following function:

dVals2=function(D2,DD){
  if((D2[2]-D2[1])<10){return(rep(0,2))} # reject short examples
  DX=DD[D2[1]:D2[2],]
  Res=as.vector(lm(DX[,3]~DX[,2])$coefficients)
  return(Res)
}

which returns 2 0's either if there are fewer than 10 values, otherwise it
returns the intercept and slope calculated over the specified range.

Applying this to the whole data by:

t(apply(ZZ,1,dVals2,DD=ZZ))

does the job I think returning the results as an N * 2 matrix.

> Hi John,
> With due respect to the other respondents, here is something that might
help:
>
> # get a vector of values
> foo<-rnorm(100)
> # get a vector of increasing indices (aka your "recent" values)
> bar<-sort(sample(1:100,40))
> # write a function to "clump" the adjacent index values
> clump_adj_int<-function(x) {
>  index_list<-list(x[1])
>  list_index<-1
>  for(i in 2:length(x)) {
>   if(x[i]==x[i-1]+1)
>index_list[[list_index]]<-c(index_list[[list_index]],x[i])
>   else {
>list_index<-list_index+1
>index_list[[list_index]]<-x[i]
>   }
>  }
>  return(index_list)
> }
> index_clumps<-clump_adj_int(bar)
> # write another function to sum the values
> sum_subsets<-function(indices,vector)
> return(sum(vector[indices],na.rm=TRUE))
> # now "apply" the function to the list of indices
> lapply(index_clumps,sum_subsets,foo)
>
> Jim
>
>
> On Thu, Jun 9, 2016 at 2:41 AM, John Logsdon
>  wrote:
>> Folks
>>
>> Is there any way to get the row index into apply as a variable?
>>
>> I want a function to do some sums on a small subset of some very long
vectors, rolling through the whole vectors.
>>
>> apply(X,1,function {do something}, other arguments)
>>
>> seems to be the way to do it.
>>
>> The subset I want is the most recent set of measurements only - perhaps a
>> couple of hundred out of millions - but I can't see how to index each
value.  The ultimate output should be a matrix of results the length of
the input vector.  But to do the sum I need to access the current row
number.
>>
>> It is easy in a loop but that will take ages. Is there any vectorised
apply-like solution to this?
>>
>> Or does apply etc only operate on each row at a time, independently of
other rows?
>>
>>
>> Best wishes
>>
>> John
>>
>> John Logsdon
>> Quantex Research Ltd
>> +44 161 445 4951/+44 7717758675
>>
>> __
>> [email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>


Best wishes

John

John Logsdon
Quantex Research Ltd
+44 161 445 4951/+44 7717758675



Best wishes

John

John Logsdon
Quantex Research Ltd
+44 161 445 4951/+44 7717758675

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply and cousins

2016-06-08 Thread Jim Lemon
Hi John,
With due respect to the other respondents, here is something that might help:

# get a vector of values
foo<-rnorm(100)
# get a vector of increasing indices (aka your "recent" values)
bar<-sort(sample(1:100,40))
# write a function to "clump" the adjacent index values
clump_adj_int<-function(x) {
 index_list<-list(x[1])
 list_index<-1
 for(i in 2:length(x)) {
  if(x[i]==x[i-1]+1)
   index_list[[list_index]]<-c(index_list[[list_index]],x[i])
  else {
   list_index<-list_index+1
   index_list[[list_index]]<-x[i]
  }
 }
 return(index_list)
}
index_clumps<-clump_adj_int(bar)
# write another function to sum the values
sum_subsets<-function(indices,vector) return(sum(vector[indices],na.rm=TRUE))
# now "apply" the function to the list of indices
lapply(index_clumps,sum_subsets,foo)

Jim


On Thu, Jun 9, 2016 at 2:41 AM, John Logsdon
 wrote:
> Folks
>
> Is there any way to get the row index into apply as a variable?
>
> I want a function to do some sums on a small subset of some very long
> vectors, rolling through the whole vectors.
>
> apply(X,1,function {do something}, other arguments)
>
> seems to be the way to do it.
>
> The subset I want is the most recent set of measurements only - perhaps a
> couple of hundred out of millions - but I can't see how to index each
> value.  The ultimate output should be a matrix of results the length of
> the input vector.  But to do the sum I need to access the current row
> number.
>
> It is easy in a loop but that will take ages. Is there any vectorised
> apply-like solution to this?
>
> Or does apply etc only operate on each row at a time, independently of
> other rows?
>
>
> Best wishes
>
> John
>
> John Logsdon
> Quantex Research Ltd
> +44 161 445 4951/+44 7717758675
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply and cousins

2016-06-08 Thread MacQueen, Don
Hopefully Bert and William won't be offended if I more or less summarize:

Are you assuming a loop will take ages, or have you actually tested it? I
wouldn't assume a loop will take ages, or that it will take much longer
than apply().

What's wrong with

  apply( X[ {logical expression } , ] , 1, function {do something} )

?

Where the logical expression identifies (by row index or any other method)
which rows you need to work on. I would expect it to be faster to subset
the rows first, rather than test for inclusion at every iteration within a
loop.

Also, if the data is acquired in such a way that you can know that the
most recent set of measurements is the last n rows, then tail(X,n) might
be good. For example,

> foo <- matrix(1:20, ncol=2)
> foo
  [,1] [,2]
 [1,]1   11
 [2,]2   12
 [3,]3   13
 [4,]4   14
 [5,]5   15
 [6,]6   16
 [7,]7   17
 [8,]8   18
 [9,]9   19
[10,]   10   20
> tail(foo,4)
  [,1] [,2]
 [7,]7   17
 [8,]8   18
 [9,]9   19
[10,]   10   20


-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 6/8/16, 9:41 AM, "R-help on behalf of John Logsdon"

wrote:

>Folks
>
>Is there any way to get the row index into apply as a variable?
>
>I want a function to do some sums on a small subset of some very long
>vectors, rolling through the whole vectors.
>
>apply(X,1,function {do something}, other arguments)
>
>seems to be the way to do it.
>
>The subset I want is the most recent set of measurements only - perhaps a
>couple of hundred out of millions - but I can't see how to index each
>value.  The ultimate output should be a matrix of results the length of
>the input vector.  But to do the sum I need to access the current row
>number.
>
>It is easy in a loop but that will take ages. Is there any vectorised
>apply-like solution to this?
>
>Or does apply etc only operate on each row at a time, independently of
>other rows?
>
>
>Best wishes
>
>John
>
>John Logsdon
>Quantex Research Ltd
>+44 161 445 4951/+44 7717758675
>
>__
>[email protected] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply and cousins

2016-06-08 Thread Bert Gunter
John:

1. Please read and follow the posting guide. In particular, provide a
small reproducible example so that we know what your data and looping
code look like.

2. apply-type commands are *not* vectorized; they are disguised loops
that may or may not offer any speedup over explicit loops.

3. A guess at a possible strategy is to convert character date-time
data to POSIXct dates using as.POSITct and then just choose those rows
with the maximum value . e.g.

x[x==max(x)]

These operations *are* vectorized.

However, this guess might be completely useless with your unspecified
data, so beware.

Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, Jun 8, 2016 at 9:41 AM, John Logsdon
 wrote:
> Folks
>
> Is there any way to get the row index into apply as a variable?
>
> I want a function to do some sums on a small subset of some very long
> vectors, rolling through the whole vectors.
>
> apply(X,1,function {do something}, other arguments)
>
> seems to be the way to do it.
>
> The subset I want is the most recent set of measurements only - perhaps a
> couple of hundred out of millions - but I can't see how to index each
> value.  The ultimate output should be a matrix of results the length of
> the input vector.  But to do the sum I need to access the current row
> number.
>
> It is easy in a loop but that will take ages. Is there any vectorised
> apply-like solution to this?
>
> Or does apply etc only operate on each row at a time, independently of
> other rows?
>
>
> Best wishes
>
> John
>
> John Logsdon
> Quantex Research Ltd
> +44 161 445 4951/+44 7717758675
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply and cousins

2016-06-08 Thread William Dunlap via R-help
>It is easy in a loop but that will take ages. Is there any vectorised
>apply-like solution to this?

If you showed the loop that takes ages, along with small inputs for
it (and an indication of how to expand those small inputs to big ones),
someone might be able to show you some code that does the
same thing in less time.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, Jun 8, 2016 at 9:41 AM, John Logsdon  wrote:

> Folks
>
> Is there any way to get the row index into apply as a variable?
>
> I want a function to do some sums on a small subset of some very long
> vectors, rolling through the whole vectors.
>
> apply(X,1,function {do something}, other arguments)
>
> seems to be the way to do it.
>
> The subset I want is the most recent set of measurements only - perhaps a
> couple of hundred out of millions - but I can't see how to index each
> value.  The ultimate output should be a matrix of results the length of
> the input vector.  But to do the sum I need to access the current row
> number.
>
> It is easy in a loop but that will take ages. Is there any vectorised
> apply-like solution to this?
>
> Or does apply etc only operate on each row at a time, independently of
> other rows?
>
>
> Best wishes
>
> John
>
> John Logsdon
> Quantex Research Ltd
> +44 161 445 4951/+44 7717758675
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply formula over columns by subset of rows in a dataframe (to get a new dataframe)

2016-05-14 Thread Massimo Bressan
thank you, what a nice compact solution with ave() 

I learned something new about the subtleties of R 

let me here summarize the alternative solutions, just in case someonelse might 
be interested... 

thanks, bye 

# 

# my user function (an example) 
mynorm <- function(x) {(x - min(x, na.rm=TRUE))/(max(x, na.rm=TRUE) - min(x, 
na.rm=TRUE))} 

# my dataframe to apply the formula by blocks 
mydf<-data.frame(blocks=rep(c("a","b","c"),each=5), 
v1=round(runif(15,10,25),0), v2=round(rnorm(15,30,5),0)) 

# blocks (factors) to be used for splitting 
b <- mydf$blocks 

# 1 - split-lapply-unsplit with anonimous function to return a new df 
s <- split(mydf, b) 
l<- lapply(s, function(x) data.frame(x, v1mod=mynorm(x$v1))) 
mydf_new <- unsplit(l, mydf$blocks) 

# 2 - split-lapply-unsplit with function trasnform to return a new df 
l <- split(mydf, b) 
l <- lapply(l, transform, v1.mod = mynorm(v1)) 
mydf_new <- unsplit(l, b) 

# 3 - ave() encapsulating split-lapply-unsplit approach 
mydf_new<-transform(mydf, v1.mod = ave(v1, blocks, FUN=mynorm)) 

# 





Da: "William Dunlap"  
A: "Massimo Bressan"  
Cc: "David L Carlson" , "r-help"  
Inviato: Venerdì, 13 maggio 2016 19:22:21 
Oggetto: Re: [R] apply formula over columns by subset of rows in a dataframe 
(to get a new dataframe) 

ave() encapsulates the split/lapply/unsplit stuff so 
transform(mydf, v1.mod = ave(v1, blocks, FUN=mynorm)) 
also gives what you got above. 

Bill Dunlap 
TIBCO Software 
wdunlap tibco.com 

On Fri, May 13, 2016 at 7:44 AM, Massimo Bressan < 
[email protected] > wrote: 


yes, thanks 

you pointed me in the right direction: split/unplist was the trick 

I completely left behind that possibility! 

here the final version 

 

mynorm <- function(x) {(x - min(x, na.rm=TRUE))/(max(x, na.rm=TRUE) - min(x, 
na.rm=TRUE))} 

mydf<-data.frame(blocks=rep(c("a","b","c"),each=5), 
v1=round(runif(15,10,25),0), v2=round(rnorm(15,30,5),0)) 

g <- mydf$blocks 
l <- split(mydf, g) 
l <- lapply(l, transform, v1.mod = mynorm(v1)) 
mydf_new <- unsplit(l, g) 

 

thanks again 

massimo 

__ 
[email protected] mailing list -- To UNSUBSCRIBE and more, see 
https://stat.ethz.ch/mailman/listinfo/r-help 
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html 
and provide commented, minimal, self-contained, reproducible code. 





-- 

 
Massimo Bressan 

ARPAV 
Agenzia Regionale per la Prevenzione e 
Protezione Ambientale del Veneto 

Dipartimento Provinciale di Treviso 
Via Santa Barbara, 5/a 
31100 Treviso, Italy 

tel: +39 0422 558545 
fax: +39 0422 558516 
e-mail: [email protected] 
 

[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: [R] apply formula over columns by subset of rows in a dataframe (to get a new dataframe)

2016-05-13 Thread William Dunlap via R-help
ave() encapsulates the split/lapply/unsplit stuff so
   transform(mydf, v1.mod = ave(v1, blocks, FUN=mynorm))
also gives what you got above.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, May 13, 2016 at 7:44 AM, Massimo Bressan <
[email protected]> wrote:

> yes, thanks
>
> you pointed me in the right direction: split/unplist was the trick
>
> I completely left behind that possibility!
>
> here the final version
>
> 
>
> mynorm <- function(x) {(x - min(x, na.rm=TRUE))/(max(x, na.rm=TRUE) -
> min(x, na.rm=TRUE))}
>
> mydf<-data.frame(blocks=rep(c("a","b","c"),each=5),
> v1=round(runif(15,10,25),0), v2=round(rnorm(15,30,5),0))
>
> g <- mydf$blocks
> l <- split(mydf, g)
> l <- lapply(l, transform, v1.mod = mynorm(v1))
> mydf_new <- unsplit(l, g)
>
> 
>
> thanks again
>
> massimo
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply formula over columns by subset of rows in a dataframe (to get a new dataframe)

2016-05-13 Thread Massimo Bressan
yes, thanks

you pointed me in the right direction: split/unplist was the trick 

I completely left behind that possibility!

here the final version



mynorm <- function(x) {(x - min(x, na.rm=TRUE))/(max(x, na.rm=TRUE) - min(x, 
na.rm=TRUE))} 

mydf<-data.frame(blocks=rep(c("a","b","c"),each=5), 
v1=round(runif(15,10,25),0), v2=round(rnorm(15,30,5),0)) 

g <- mydf$blocks
l <- split(mydf, g)
l <- lapply(l, transform, v1.mod = mynorm(v1))
mydf_new <- unsplit(l, g)



thanks again

massimo

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply formula over columns by subset of rows in a dataframe (to get a new dataframe)

2016-05-13 Thread David L Carlson
You can do this with split/unsplit:

> mydf.split <- split(mydf, mydf$blocks)
> str(mydf.split)
List of 3
 $ a:'data.frame':  5 obs. of  3 variables:
  ..$ blocks: Factor w/ 3 levels "a","b","c": 1 1 1 1 1
  ..$ v1: num [1:5] 19 15 17 22 16
  ..$ v2: num [1:5] 35 31 35 31 39
 $ b:'data.frame':  5 obs. of  3 variables:
  ..$ blocks: Factor w/ 3 levels "a","b","c": 2 2 2 2 2
  ..$ v1: num [1:5] 12 24 25 22 18
  ..$ v2: num [1:5] 31 19 35 32 38
 $ c:'data.frame':  5 obs. of  3 variables:
  ..$ blocks: Factor w/ 3 levels "a","b","c": 3 3 3 3 3
  ..$ v1: num [1:5] 17 14 21 21 22
  ..$ v2: num [1:5] 27 25 23 23 27
> mydf.split2 <- lapply(mydf.split, function(x) data.frame(x, 
+  v1mod=mynorm(x$v1)))
> str(mydf.split2)
List of 3
 $ a:'data.frame':  5 obs. of  4 variables:
  ..$ blocks: Factor w/ 3 levels "a","b","c": 1 1 1 1 1
  ..$ v1: num [1:5] 19 15 17 22 16
  ..$ v2: num [1:5] 35 31 35 31 39
  ..$ v1mod : num [1:5] 0.571 0 0.286 1 0.143
 $ b:'data.frame':  5 obs. of  4 variables:
  ..$ blocks: Factor w/ 3 levels "a","b","c": 2 2 2 2 2
  ..$ v1: num [1:5] 12 24 25 22 18
  ..$ v2: num [1:5] 31 19 35 32 38
  ..$ v1mod : num [1:5] 0 0.923 1 0.769 0.462
 $ c:'data.frame':  5 obs. of  4 variables:
  ..$ blocks: Factor w/ 3 levels "a","b","c": 3 3 3 3 3
  ..$ v1: num [1:5] 17 14 21 21 22
  ..$ v2: num [1:5] 27 25 23 23 27
  ..$ v1mod : num [1:5] 0.375 0 0.875 0.875 1
> mydf2 <- unsplit(mydf.split2, mydf$blocks)
> str(mydf2)
'data.frame':   15 obs. of  4 variables:
 $ blocks: Factor w/ 3 levels "a","b","c": 1 1 1 1 1 2 2 2 2 2 ...
 $ v1: num  19 15 17 22 16 12 24 25 22 18 ...
 $ v2: num  35 31 35 31 39 31 19 35 32 38 ...
 $ v1mod : num  0.571 0 0.286 1 0.143 ...

-
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-Original Message-
From: R-help [mailto:[email protected]] On Behalf Of Massimo Bressan
Sent: Friday, May 13, 2016 6:56 AM
To: [email protected]
Subject: [R] apply formula over columns by subset of rows in a dataframe (to 
get a new dataframe)

hi 

I need to apply a user defined formula over some selected columns of a 
dataframe by subsetting group of rows (blocks) and get back a new dataframe 

I’ve been managed to get the the calculations right but I’m not satisfied at 
all by the form of the results 

please refer to my reproducible example 

## 
# my user function (an example) 
mynorm <- function(x) {(x - min(x, na.rm=TRUE))/(max(x, na.rm=TRUE) - min(x, 
na.rm=TRUE))} 

# my dataframe to apply the formula by blocks 
mydf<-data.frame(blocks=rep(c("a","b","c"),each=5), 
v1=round(runif(15,10,25),0), v2=round(rnorm(15,30,5),0)) 


#my attempts (not satisfied by final output) 

tapply(mydf$v1, mydf$blocks, mynorm) 

byf<-factor(mydf$blocks) 
aggregate(mydf[2:3], list(byf), mynorm) 
aggregate(mydf[2:3], list(mydf$blocks), mynorm, simplify = FALSE) 

### 

please can anyone give me some hints on how to properly proceed? 

I need a dataframe with all variables as final result 
sorry but I’m sort of definitely stuck with this… 

thanks 


[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: [R] apply mean function to a subset of data

2016-04-04 Thread Pedro Mardones
Thanks David
It works perfectly
Pedro

Sent from my iPhone

> On Apr 3, 2016, at 17:44, David L Carlson  wrote:
> 
> Here are several ways to get there, but your original loop is fine once it is 
> corrected:
> 
>> for (i in 1:2)  smean[i] <- mean(toy$diam[toy$group==i][1:nsel[i]])
>> smean
> [1] 0.271489 1.117015
> 
> Using sapply() to hide the loop:
>> smean <- sapply(1:2, function(x) mean((toy$diam[toy$group==x])[1:nsel[x]]))
>> smean
> [1] 0.271489 1.117015
> 
> Or use head()
>> smean <- sapply(1:2, function(x) mean(head(toy$diam[toy$group==x], nsel[x])))
>> smean
> [1] 0.271489 1.117015
> 
> Or mapply() instead of sapply
>> smean <- mapply(function(x, y) mean(head(x, y)) , x=split(toy$diam, 
>> toy$group), y=nsel)
>> smean
>   12 
> 0.271489 1.117015
> 
> --
> David L. Carlson
> Department of Anthropology
> Texas A&M University
> 
> -Original Message-
> From: R-help [mailto:[email protected]] On Behalf Of Jim Lemon
> Sent: Saturday, April 2, 2016 6:14 PM
> To: Pedro Mardones 
> Cc: r-help mailing list 
> Subject: Re: [R] apply mean function to a subset of data
> 
> Hi Pedro,
> This may not be much of an improvement, but it was a challenge.
> 
> selvec<-as.vector(matrix(c(nsel,unlist(by(toy$diam,toy$group,length))-nsel),
> ncol=2,byrow=TRUE))
> TFvec<-rep(c(TRUE,FALSE),length.out=length(selvec))
> toynsel<-rep(TFvec,selvec)
> by(toy[toynsel,]$diam,toy[toynsel,]$group,mean)
> 
> Jim
> 
>> On 4/3/16, Pedro Mardones  wrote:
>> Dear all;
>> 
>> This must have a rather simple answer but haven't been able to figure it
>> out: I have a data frame with say 2 groups (group 1 & 2). I want to select
>> from group 1 say "n" rows and calculate the mean; then select "m" rows from
>> group 2 and calculate the mean as well. So far I've been using a for loop
>> for doing it but when it comes to a large data set is rather inefficient.
>> Any hint to vectorize this would be appreciated.
>> 
>> toy = data.frame(group = c(rep(1,10),rep(2,8)), diam =
>> c(rnorm(10),rnorm(8)))
>> nsel = c(6,4)
>> smean <- c(0,0)
>> for (i in 1:2)  smean[i] <- mean(toy$diam[1:nsel[i]])
>> 
>> Thanks
>> 
>> Pedro
>> 
>>[[alternative HTML version deleted]]
>> 
>> __
>> [email protected] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply mean function to a subset of data

2016-04-03 Thread David L Carlson
Here are several ways to get there, but your original loop is fine once it is 
corrected:

> for (i in 1:2)  smean[i] <- mean(toy$diam[toy$group==i][1:nsel[i]])
> smean
[1] 0.271489 1.117015

Using sapply() to hide the loop:
> smean <- sapply(1:2, function(x) mean((toy$diam[toy$group==x])[1:nsel[x]]))
> smean
[1] 0.271489 1.117015

Or use head()
> smean <- sapply(1:2, function(x) mean(head(toy$diam[toy$group==x], nsel[x])))
> smean
[1] 0.271489 1.117015

Or mapply() instead of sapply
> smean <- mapply(function(x, y) mean(head(x, y)) , x=split(toy$diam, 
> toy$group), y=nsel)
> smean
   12 
0.271489 1.117015

--
David L. Carlson
Department of Anthropology
Texas A&M University

-Original Message-
From: R-help [mailto:[email protected]] On Behalf Of Jim Lemon
Sent: Saturday, April 2, 2016 6:14 PM
To: Pedro Mardones 
Cc: r-help mailing list 
Subject: Re: [R] apply mean function to a subset of data

Hi Pedro,
This may not be much of an improvement, but it was a challenge.

selvec<-as.vector(matrix(c(nsel,unlist(by(toy$diam,toy$group,length))-nsel),
 ncol=2,byrow=TRUE))
TFvec<-rep(c(TRUE,FALSE),length.out=length(selvec))
toynsel<-rep(TFvec,selvec)
by(toy[toynsel,]$diam,toy[toynsel,]$group,mean)

Jim

On 4/3/16, Pedro Mardones  wrote:
> Dear all;
>
> This must have a rather simple answer but haven't been able to figure it
> out: I have a data frame with say 2 groups (group 1 & 2). I want to select
> from group 1 say "n" rows and calculate the mean; then select "m" rows from
> group 2 and calculate the mean as well. So far I've been using a for loop
> for doing it but when it comes to a large data set is rather inefficient.
> Any hint to vectorize this would be appreciated.
>
> toy = data.frame(group = c(rep(1,10),rep(2,8)), diam =
> c(rnorm(10),rnorm(8)))
> nsel = c(6,4)
> smean <- c(0,0)
> for (i in 1:2)  smean[i] <- mean(toy$diam[1:nsel[i]])
>
> Thanks
>
> Pedro
>
>   [[alternative HTML version deleted]]
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply mean function to a subset of data

2016-04-02 Thread Jim Lemon
Hi Pedro,
This may not be much of an improvement, but it was a challenge.

selvec<-as.vector(matrix(c(nsel,unlist(by(toy$diam,toy$group,length))-nsel),
 ncol=2,byrow=TRUE))
TFvec<-rep(c(TRUE,FALSE),length.out=length(selvec))
toynsel<-rep(TFvec,selvec)
by(toy[toynsel,]$diam,toy[toynsel,]$group,mean)

Jim

On 4/3/16, Pedro Mardones  wrote:
> Dear all;
>
> This must have a rather simple answer but haven't been able to figure it
> out: I have a data frame with say 2 groups (group 1 & 2). I want to select
> from group 1 say "n" rows and calculate the mean; then select "m" rows from
> group 2 and calculate the mean as well. So far I've been using a for loop
> for doing it but when it comes to a large data set is rather inefficient.
> Any hint to vectorize this would be appreciated.
>
> toy = data.frame(group = c(rep(1,10),rep(2,8)), diam =
> c(rnorm(10),rnorm(8)))
> nsel = c(6,4)
> smean <- c(0,0)
> for (i in 1:2)  smean[i] <- mean(toy$diam[1:nsel[i]])
>
> Thanks
>
> Pedro
>
>   [[alternative HTML version deleted]]
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply mean function to a subset of data

2016-04-02 Thread Boris Steipe
Your toy code does not reproduce what you describe: mean(toy$diam[1:nsel[i]]) 
both times selects from elements of group 1. YOu probably want to subset like 
toy$diam[toy$group == i]. Also, if there is any real inefficiency here, it is 
_not_ because you are executing a for-loop for two iterations. What makes you 
think you have an efficiency problem?


B.

On Apr 2, 2016, at 2:46 PM, Pedro Mardones  wrote:

> Dear all;
> 
> This must have a rather simple answer but haven't been able to figure it
> out: I have a data frame with say 2 groups (group 1 & 2). I want to select
> from group 1 say "n" rows and calculate the mean; then select "m" rows from
> group 2 and calculate the mean as well. So far I've been using a for loop
> for doing it but when it comes to a large data set is rather inefficient.
> Any hint to vectorize this would be appreciated.
> 
> toy = data.frame(group = c(rep(1,10),rep(2,8)), diam =
> c(rnorm(10),rnorm(8)))
> nsel = c(6,4)
> smean <- c(0,0)
> for (i in 1:2)  smean[i] <- mean(toy$diam[1:nsel[i]])
> 
> Thanks
> 
> Pedro
> 
>   [[alternative HTML version deleted]]
> 
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply function across dataframe columns for non-exclusive groups

2015-10-21 Thread Jeff Newmiller
The calculation appears to be sum(a)/(sum(a)+sum(b)).

library(dplyr)
library(tidyr)
result <- (   this_df
  %>% gather( group, truth, -c(a,b) )
  %>% group_by( group, truth )
  %>% summarise( calc = sum(a)/(sum(a)+sum(b)) )
  %>% as.data.frame
  )

---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On October 21, 2015 1:30:46 PM GMT+02:00, Alexander Shenkin  
wrote:
>Hello all,
>
>I've been banging my head over what must be a simple solution.  I would
>
>like to apply a function across columns of a dataframe for rows grouped
>
>across different columns.  These groups are not exclusive.  See below 
>for an example.  Happy to use dplyr, data.table, or whatever.  Any 
>guidance appreciated!
>
>Thanks,
>Allie
>
>
>desired algorithm: calculate a/(a+b) for each TRUE and FALSE grouping
>of 
>columns grp1 and grp2.
>
>this_df = data.frame(a = c(1,2,3,4,5), b = c(7,8,9,10,11), grp1 = 
>c(T,T,F,F,F), grp2 = c(F,T,F,T,F))
>
>desired output (doesn't have to be exactly this format, but something 
>along these lines):
>
>grp1 T 0.166
>grp1 F 0.286
>grp2 T 0.25
>grp2 F 0.25
>
>__
>[email protected] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply regression to an array

2015-10-06 Thread David Winsemius

On Oct 6, 2015, at 10:42 AM, Adrienne Wootten wrote:

> R-Helpers,
> 
> I've seen some similar threads about this question online, but not quite
> what I'm looking for.  I apologize in advance if someone's already answered
> this and I just can't find it online.
> 
> Say that I have an array like test3 in the little example code I have below:
> 
> test1 = array(rep(1:10,each = 25),dim=c(5,5,10))
> test2 = array(rnorm(250,0,0.35),dim=c(5,5,10))
> test3 = test1+test2 # array with 5 rows, 5 columns, 10 slices
> 
> time=1:10
> 
> Where the dimensions are x, y, and time.  What I'd like to do is run a
> regression (for the sake of this example, say lm) on each x,y in time.  So
> for a single cell the formula might be test3[1,1,]~time, but I'd like to
> that for all cells.  The only way I can immediately think of is to use a
> loop, but I'm wondering if there's a way to do this without a loop.
> Perhaps with tapply?

 Would not be expecting a 5x5x10 results since you are using the last dimension 
to calculate a two parameters for each row and col. Why not use a loop? Doing 
it with an index is just a a disguised loop:

apply( test3, 1:2, function(x) coef(lm(x~time) ) ) # iterates over rows and 
cols.
# results is 5 x 5 x2

> 
> I'm actually doing a fourth order regression with a much larger array, but
> this simple example illustrates the question I have.
> 
> Many thanks for the help! Sorry if someone's already answered this and I
> can't find it.
> 
> Adrienne
> 
> -- 
> Adrienne Wootten
> Graduate Research Assistant
> State Climate Office of North Carolina
> Department of Marine, Earth and Atmospheric Sciences
> North Carolina State University
> 
>   [[alternative HTML version deleted]]
> 
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply regression to an array

2015-10-06 Thread Adrienne Wootten
Bill,

Thanks a bunch that works great!

A

On Tue, Oct 6, 2015 at 2:56 PM, William Dunlap  wrote:

> Since the model matrix, cbind(1,time) is the same for all your
> response variables,
> you can calculate this on one call to lm, but you have to rearrange the
> response
> values so that each x,y set is in one column.  I think the following
> function does it:
>
> f <- function (time, y)
> {
> stopifnot(length(dim(y)) == 3, dim(y)[3] == length(time))
> yMatrix <- matrix(aperm(y, c(3, 1, 2)), dim(y)[3])
> fit <- lm(yMatrix ~ time)
> aperm(array(fitted.values(fit), dim(y)[c(3, 1, 2)]), c(2,
> 3, 1))
> }
>
> E.g.,
> > fitted.values(lm(test1[2,5,]~time))
>  1  2  3  4  5  6  7  8  9 10
>  1  2  3  4  5  6  7  8  9 10
> > f(time, test1)[2,5,]
>  [1]  1  2  3  4  5  6  7  8  9 10
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>
> On Tue, Oct 6, 2015 at 10:42 AM, Adrienne Wootten 
> wrote:
> > R-Helpers,
> >
> > I've seen some similar threads about this question online, but not quite
> > what I'm looking for.  I apologize in advance if someone's already
> answered
> > this and I just can't find it online.
> >
> > Say that I have an array like test3 in the little example code I have
> below:
> >
> > test1 = array(rep(1:10,each = 25),dim=c(5,5,10))
> > test2 = array(rnorm(250,0,0.35),dim=c(5,5,10))
> > test3 = test1+test2 # array with 5 rows, 5 columns, 10 slices
> >
> > time=1:10
> >
> > Where the dimensions are x, y, and time.  What I'd like to do is run a
> > regression (for the sake of this example, say lm) on each x,y in time.
> So
> > for a single cell the formula might be test3[1,1,]~time, but I'd like to
> > that for all cells.  The only way I can immediately think of is to use a
> > loop, but I'm wondering if there's a way to do this without a loop.
> > Perhaps with tapply?
> >
> > I'm actually doing a fourth order regression with a much larger array,
> but
> > this simple example illustrates the question I have.
> >
> > Many thanks for the help! Sorry if someone's already answered this and I
> > can't find it.
> >
> > Adrienne
> >
> > --
> > Adrienne Wootten
> > Graduate Research Assistant
> > State Climate Office of North Carolina
> > Department of Marine, Earth and Atmospheric Sciences
> > North Carolina State University
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > [email protected] mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>



-- 
Adrienne Wootten
Graduate Research Assistant
State Climate Office of North Carolina
Department of Marine, Earth and Atmospheric Sciences
North Carolina State University

[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply regression to an array

2015-10-06 Thread William Dunlap
Since the model matrix, cbind(1,time) is the same for all your
response variables,
you can calculate this on one call to lm, but you have to rearrange the response
values so that each x,y set is in one column.  I think the following
function does it:

f <- function (time, y)
{
stopifnot(length(dim(y)) == 3, dim(y)[3] == length(time))
yMatrix <- matrix(aperm(y, c(3, 1, 2)), dim(y)[3])
fit <- lm(yMatrix ~ time)
aperm(array(fitted.values(fit), dim(y)[c(3, 1, 2)]), c(2,
3, 1))
}

E.g.,
> fitted.values(lm(test1[2,5,]~time))
 1  2  3  4  5  6  7  8  9 10
 1  2  3  4  5  6  7  8  9 10
> f(time, test1)[2,5,]
 [1]  1  2  3  4  5  6  7  8  9 10


Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Tue, Oct 6, 2015 at 10:42 AM, Adrienne Wootten  wrote:
> R-Helpers,
>
> I've seen some similar threads about this question online, but not quite
> what I'm looking for.  I apologize in advance if someone's already answered
> this and I just can't find it online.
>
> Say that I have an array like test3 in the little example code I have below:
>
> test1 = array(rep(1:10,each = 25),dim=c(5,5,10))
> test2 = array(rnorm(250,0,0.35),dim=c(5,5,10))
> test3 = test1+test2 # array with 5 rows, 5 columns, 10 slices
>
> time=1:10
>
> Where the dimensions are x, y, and time.  What I'd like to do is run a
> regression (for the sake of this example, say lm) on each x,y in time.  So
> for a single cell the formula might be test3[1,1,]~time, but I'd like to
> that for all cells.  The only way I can immediately think of is to use a
> loop, but I'm wondering if there's a way to do this without a loop.
> Perhaps with tapply?
>
> I'm actually doing a fourth order regression with a much larger array, but
> this simple example illustrates the question I have.
>
> Many thanks for the help! Sorry if someone's already answered this and I
> can't find it.
>
> Adrienne
>
> --
> Adrienne Wootten
> Graduate Research Assistant
> State Climate Office of North Carolina
> Department of Marine, Earth and Atmospheric Sciences
> North Carolina State University
>
> [[alternative HTML version deleted]]
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply regression to an array

2015-10-06 Thread Adrienne Wootten
Almost forgot that function lmfunc is this:

lmfunc = function(valist,input){
  fitted.values(lm(valist~input))
}

A


On Tue, Oct 6, 2015 at 2:41 PM, Adrienne Wootten  wrote:

> FYI I did try something like this:
>
> test = apply(test3,c(1,2),lmfunc,input=t)
>
> but that gives me an array that is 10 rows by 5 columns by 5 slices, and I
> need it to keep the same dimensions as test3 (5x5x10)
>
> A
>
> On Tue, Oct 6, 2015 at 1:42 PM, Adrienne Wootten 
> wrote:
>
>> R-Helpers,
>>
>> I've seen some similar threads about this question online, but not quite
>> what I'm looking for.  I apologize in advance if someone's already answered
>> this and I just can't find it online.
>>
>> Say that I have an array like test3 in the little example code I have
>> below:
>>
>> test1 = array(rep(1:10,each = 25),dim=c(5,5,10))
>> test2 = array(rnorm(250,0,0.35),dim=c(5,5,10))
>> test3 = test1+test2 # array with 5 rows, 5 columns, 10 slices
>>
>> time=1:10
>>
>> Where the dimensions are x, y, and time.  What I'd like to do is run a
>> regression (for the sake of this example, say lm) on each x,y in time.  So
>> for a single cell the formula might be test3[1,1,]~time, but I'd like to
>> that for all cells.  The only way I can immediately think of is to use a
>> loop, but I'm wondering if there's a way to do this without a loop.
>> Perhaps with tapply?
>>
>> I'm actually doing a fourth order regression with a much larger array,
>> but this simple example illustrates the question I have.
>>
>> Many thanks for the help! Sorry if someone's already answered this and I
>> can't find it.
>>
>> Adrienne
>>
>> --
>> Adrienne Wootten
>> Graduate Research Assistant
>> State Climate Office of North Carolina
>> Department of Marine, Earth and Atmospheric Sciences
>> North Carolina State University
>>
>
>
>
> --
> Adrienne Wootten
> Graduate Research Assistant
> State Climate Office of North Carolina
> Department of Marine, Earth and Atmospheric Sciences
> North Carolina State University
>



-- 
Adrienne Wootten
Graduate Research Assistant
State Climate Office of North Carolina
Department of Marine, Earth and Atmospheric Sciences
North Carolina State University

[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply regression to an array

2015-10-06 Thread Adrienne Wootten
FYI I did try something like this:

test = apply(test3,c(1,2),lmfunc,input=t)

but that gives me an array that is 10 rows by 5 columns by 5 slices, and I
need it to keep the same dimensions as test3 (5x5x10)

A

On Tue, Oct 6, 2015 at 1:42 PM, Adrienne Wootten  wrote:

> R-Helpers,
>
> I've seen some similar threads about this question online, but not quite
> what I'm looking for.  I apologize in advance if someone's already answered
> this and I just can't find it online.
>
> Say that I have an array like test3 in the little example code I have
> below:
>
> test1 = array(rep(1:10,each = 25),dim=c(5,5,10))
> test2 = array(rnorm(250,0,0.35),dim=c(5,5,10))
> test3 = test1+test2 # array with 5 rows, 5 columns, 10 slices
>
> time=1:10
>
> Where the dimensions are x, y, and time.  What I'd like to do is run a
> regression (for the sake of this example, say lm) on each x,y in time.  So
> for a single cell the formula might be test3[1,1,]~time, but I'd like to
> that for all cells.  The only way I can immediately think of is to use a
> loop, but I'm wondering if there's a way to do this without a loop.
> Perhaps with tapply?
>
> I'm actually doing a fourth order regression with a much larger array, but
> this simple example illustrates the question I have.
>
> Many thanks for the help! Sorry if someone's already answered this and I
> can't find it.
>
> Adrienne
>
> --
> Adrienne Wootten
> Graduate Research Assistant
> State Climate Office of North Carolina
> Department of Marine, Earth and Atmospheric Sciences
> North Carolina State University
>



-- 
Adrienne Wootten
Graduate Research Assistant
State Climate Office of North Carolina
Department of Marine, Earth and Atmospheric Sciences
North Carolina State University

[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply kendall tau to a split data set

2015-07-25 Thread John Kane
It helps to have a bit more detail and some sample data. Have a look at 
Reproducibility
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
 and http://adv-r.had.co.nz/Reproducibility.html
for some ideas of how to pose a question on the list.

John Kane
Kingston ON Canada


> -Original Message-
> From: [email protected]
> Sent: Fri, 24 Jul 2015 04:08:08 -0700 (PDT)
> To: [email protected]
> Subject: [R] apply kendall tau to a split data set
> 
> $`19179222`
>Unique.IDStart.YearL1.Risk.Category
> Gross.amount.sum
> 17  19179222   2013 Execution, Delivery & Process Management
> 161212.1
> 18  19179222   2015 Execution, Delivery & Process Management
> 110880.0
> 
> $`25182498`
>Unique.ID   Start.YearL1.Risk.Category
> Gross.amount.sum
> 19  25182498   2014   Clients, Products & Business Practices
> 59384
> 20  25182498   2014 Execution, Delivery & Process Management
> 355000
> 21  25182498   2015 Execution, Delivery & Process Management
> 27720
> 
> $`32506027`
>Unique.IDStart.YearL1.Risk.Category
> Gross.amount.sum
> 22  32506027   2003 Execution, Delivery & Process Management
> 360.0
> 23  32506027   2013 Execution, Delivery & Process Management
> 161212.1
> 24  32506027   2014   Clients, Products & Business Practices
> 14846.0
> 
> 
> Hi, I have split a data frame and output is as above.
> 
> I would like to apply Kendall's tau to calculate correlations. For
> example,
> for the last Unique.ID 32506027, I want to create a time series for each
> L1
> risk category, and then calculate the correlation between each L1 risk
> category using the Kendall tau approach.
> 
> Any help would be much appreciated.
> 
> Thanks
> s
> 
> 
> 
> 
> --
> View this message in context:
> http://r.789695.n4.nabble.com/apply-kendall-tau-to-a-split-data-set-tp4710303.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your 
desktop!

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply a function to a list of data frames

2015-05-26 Thread Stefano Sofia
Thank you for your help.
Your explanations have been very useful.

Stefano


Da: Rui Barradas [[email protected]]
Inviato: venerdì 22 maggio 2015 20.26
A: Stefano Sofia; [email protected]
Oggetto: Re: [R] apply a function to a list of data frames

Hello,

You should change your function to accept only one argument, the
data.frames, and then use lapply (not sapply).
Something like the following.

calc <- function(dat)
{
bias_dmo_max <- round(mean((dat$dmo_12-dat$Eonestep_12), na.rm=TRUE),
digits=2)
rmse_dmo_max <- round(sqrt(mean((dat$dmo_12-dat$Eonestep_12)^2,
na.rm=TRUE)), digits=2)
bias_dmo_min <- round(mean((dat$dmo_27-dat$Eonestep_27), na.rm=TRUE),
digits=2)
rmse_dmo_min <- round(sqrt(mean((dat$dmo_27-dat$Eonestep_27)^2,
na.rm=TRUE)), digits=2)
result <- list(bias_dmo_max, rmse_dmo_max, bias_dmo_min, rmse_dmo_min)
result
}

result_lst <- lapply(lst, calc)


Hope this helps,

Rui Barradas


Em 22-05-2015 19:02, Stefano Sofia escreveu:
> Dear R-users,
> given a list of dataframes (like below reported), for each month I need to 
> apply a function (called calc).
> The result should be written in a new list of data frames, one row for each 
> month.
>
> I have been trying to use sapply, with no success.
> Could somebody help me in this?
>
>
> $df1
> day dmo_12 Eonestep_12 tmax dmo_27 Eonestep_27 tmin
> 2012-12-01 11 13 13 9 8 8
> 2012-12-02 11 13 13 5 6 8
> 2012-12-03 6 10 8 6 6 7
> 2012-12-04 11 13 9 6 6 3
> 2012-12-05 8 10 12 5 6 7
> 2012-12-06 8 10 8 4 5 4
> 2012-12-07 7 9 8 6 6 5
> ...
>
>
> calc <- function(dmo_12, Eonestep_12, dmo_27, Eonestep_27)
> {
> bias_dmo_max <- round(mean((dmo_12-Eonestep_12), na.rm=TRUE), digits=2)
> rmse_dmo_max <- round(sqrt(mean((dmo_12-Eonestep_12)^2, na.rm=TRUE)), 
> digits=2)
> bias_dmo_min <- round(mean((dmo_27-Eonestep_27), na.rm=TRUE), digits=2)
> rmse_dmo_min <- round(sqrt(mean((dmo_27-Eonestep_27)^2, na.rm=TRUE)), 
> digits=2)
> result <- list(bias_dmo_max, rmse_dmo_max, bias_dmo_min, rmse_dmo_min)
> result
> }
>
>
> Thank you for your help
> Stefano
>
>
> 
>
> AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere 
> informazioni confidenziali, pertanto è destinato solo a persone autorizzate 
> alla ricezione. I messaggi di posta elettronica per i client di Regione 
> Marche possono contenere informazioni confidenziali e con privilegi legali. 
> Se non si è il destinatario specificato, non leggere, copiare, inoltrare o 
> archiviare questo messaggio. Se si è ricevuto questo messaggio per errore, 
> inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio 
> computer. Ai sensi dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso 
> di necessità ed urgenza, la risposta al presente messaggio di posta 
> elettronica può essere visionata da persone estranee al destinatario.
> IMPORTANT NOTICE: This e-mail message is intended to be received only by 
> persons entitled to receive the confidential information it may contain. 
> E-mail messages to clients of Regione Marche may contain information that is 
> confidential and legally privileged. Please do not read, copy, forward, or 
> store this message unless you are an intended recipient of it. If you have 
> received this message in error, please forward it to the sender and delete it 
> completely from your computer system.
>
>   [[alternative HTML version deleted]]
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere 
informazioni confidenziali, pertanto è destinato solo a persone autorizzate 
alla ricezione. I messaggi di posta elettronica per i client di Regione Marche 
possono contenere informazioni confidenziali e con privilegi legali. Se non si 
è il destinatario specificato, non leggere, copiare, inoltrare o archiviare 
questo messaggio. Se si è ricevuto questo messaggio per errore, inoltrarlo al 
mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi 
dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessità ed 
urgenza, la risposta al presente messaggio di posta elettronica può essere 
visionata da persone estranee al destinatario.
IMPORTANT NOTICE: This e-mail message is intended to be received only by 
persons entitled to receive the confidential information it may contain. E-mail 
messages to clients o

Re: [R] apply a function to a list of data frames

2015-05-22 Thread Rui Barradas

Hello,

You should change your function to accept only one argument, the 
data.frames, and then use lapply (not sapply).

Something like the following.

calc <- function(dat)
{
	bias_dmo_max <- round(mean((dat$dmo_12-dat$Eonestep_12), na.rm=TRUE), 
digits=2)
	rmse_dmo_max <- round(sqrt(mean((dat$dmo_12-dat$Eonestep_12)^2, 
na.rm=TRUE)), digits=2)
	bias_dmo_min <- round(mean((dat$dmo_27-dat$Eonestep_27), na.rm=TRUE), 
digits=2)
	rmse_dmo_min <- round(sqrt(mean((dat$dmo_27-dat$Eonestep_27)^2, 
na.rm=TRUE)), digits=2)

result <- list(bias_dmo_max, rmse_dmo_max, bias_dmo_min, rmse_dmo_min)
result
}

result_lst <- lapply(lst, calc)


Hope this helps,

Rui Barradas


Em 22-05-2015 19:02, Stefano Sofia escreveu:

Dear R-users,
given a list of dataframes (like below reported), for each month I need to 
apply a function (called calc).
The result should be written in a new list of data frames, one row for each 
month.

I have been trying to use sapply, with no success.
Could somebody help me in this?


$df1
day dmo_12 Eonestep_12 tmax dmo_27 Eonestep_27 tmin
2012-12-01 11 13 13 9 8 8
2012-12-02 11 13 13 5 6 8
2012-12-03 6 10 8 6 6 7
2012-12-04 11 13 9 6 6 3
2012-12-05 8 10 12 5 6 7
2012-12-06 8 10 8 4 5 4
2012-12-07 7 9 8 6 6 5
...


calc <- function(dmo_12, Eonestep_12, dmo_27, Eonestep_27)
{
bias_dmo_max <- round(mean((dmo_12-Eonestep_12), na.rm=TRUE), digits=2)
rmse_dmo_max <- round(sqrt(mean((dmo_12-Eonestep_12)^2, na.rm=TRUE)), digits=2)
bias_dmo_min <- round(mean((dmo_27-Eonestep_27), na.rm=TRUE), digits=2)
rmse_dmo_min <- round(sqrt(mean((dmo_27-Eonestep_27)^2, na.rm=TRUE)), digits=2)
result <- list(bias_dmo_max, rmse_dmo_max, bias_dmo_min, rmse_dmo_min)
result
}


Thank you for your help
Stefano




AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere 
informazioni confidenziali, pertanto è destinato solo a persone autorizzate 
alla ricezione. I messaggi di posta elettronica per i client di Regione Marche 
possono contenere informazioni confidenziali e con privilegi legali. Se non si 
è il destinatario specificato, non leggere, copiare, inoltrare o archiviare 
questo messaggio. Se si è ricevuto questo messaggio per errore, inoltrarlo al 
mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi 
dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessità ed 
urgenza, la risposta al presente messaggio di posta elettronica può essere 
visionata da persone estranee al destinatario.
IMPORTANT NOTICE: This e-mail message is intended to be received only by 
persons entitled to receive the confidential information it may contain. E-mail 
messages to clients of Regione Marche may contain information that is 
confidential and legally privileged. Please do not read, copy, forward, or 
store this message unless you are an intended recipient of it. If you have 
received this message in error, please forward it to the sender and delete it 
completely from your computer system.

[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: [R] apply a function to a list of data frames

2015-05-22 Thread Bert Gunter
Where is your code? I see no invocation of sapply.

-- Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Fri, May 22, 2015 at 11:02 AM, Stefano Sofia
 wrote:
> Dear R-users,
> given a list of dataframes (like below reported), for each month I need to 
> apply a function (called calc).
> The result should be written in a new list of data frames, one row for each 
> month.
>
> I have been trying to use sapply, with no success.
> Could somebody help me in this?
>
>
> $df1
> day dmo_12 Eonestep_12 tmax dmo_27 Eonestep_27 tmin
> 2012-12-01 11 13 13 9 8 8
> 2012-12-02 11 13 13 5 6 8
> 2012-12-03 6 10 8 6 6 7
> 2012-12-04 11 13 9 6 6 3
> 2012-12-05 8 10 12 5 6 7
> 2012-12-06 8 10 8 4 5 4
> 2012-12-07 7 9 8 6 6 5
> ...
>
>
> calc <- function(dmo_12, Eonestep_12, dmo_27, Eonestep_27)
> {
> bias_dmo_max <- round(mean((dmo_12-Eonestep_12), na.rm=TRUE), digits=2)
> rmse_dmo_max <- round(sqrt(mean((dmo_12-Eonestep_12)^2, na.rm=TRUE)), 
> digits=2)
> bias_dmo_min <- round(mean((dmo_27-Eonestep_27), na.rm=TRUE), digits=2)
> rmse_dmo_min <- round(sqrt(mean((dmo_27-Eonestep_27)^2, na.rm=TRUE)), 
> digits=2)
> result <- list(bias_dmo_max, rmse_dmo_max, bias_dmo_min, rmse_dmo_min)
> result
> }
>
>
> Thank you for your help
> Stefano
>
>
> 
>
> AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere 
> informazioni confidenziali, pertanto è destinato solo a persone autorizzate 
> alla ricezione. I messaggi di posta elettronica per i client di Regione 
> Marche possono contenere informazioni confidenziali e con privilegi legali. 
> Se non si è il destinatario specificato, non leggere, copiare, inoltrare o 
> archiviare questo messaggio. Se si è ricevuto questo messaggio per errore, 
> inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio 
> computer. Ai sensi dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso 
> di necessità ed urgenza, la risposta al presente messaggio di posta 
> elettronica può essere visionata da persone estranee al destinatario.
> IMPORTANT NOTICE: This e-mail message is intended to be received only by 
> persons entitled to receive the confidential information it may contain. 
> E-mail messages to clients of Regione Marche may contain information that is 
> confidential and legally privileged. Please do not read, copy, forward, or 
> store this message unless you are an intended recipient of it. If you have 
> received this message in error, please forward it to the sender and delete it 
> completely from your computer system.
>
> [[alternative HTML version deleted]]
>
> __
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: [R] Apply t-test on list in R

2015-02-25 Thread Zilefac Elvis via R-help
Many thanks, Petr.You solved my problem.AT.
[[alternative HTML version deleted]]

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply t-test on list in R

2015-02-25 Thread PIKAL Petr
Hi

I did not seen any answer so I try one.

If you have data frames organised in list I would use a for cycle.

If your list is named mylist something like

result <- list()
k=0
for(i in n:m) {
k=k+1
result[[k]] <- t.test(mylist[[x]], mylist[[i]])
}

where x is a number for the one data frame you want to compare with others and 
m, n are numbers selecting a range where are located data frames for comparison.

You can use regular expressions to select data frames but it is not my strength.

Cheers
Petr



> -Original Message-
> From: R-help [mailto:[email protected]] On Behalf Of Zilefac
> Elvis via R-help
> Sent: Wednesday, February 18, 2015 4:46 AM
> To: R. Help
> Subject: [R] Apply t-test on list in R
>
> I have a list object in R with dataframe names as:
>
>
> "pav_DJF_histo.csv""pav_DJF_rcp26_2040s.csv"
> "pav_DJF_rcp26_2080s.csv"
> "pav_DJF_rcp45_2040s.csv""pav_DJF_rcp45_2080s.csv"
> "pav_DJF_rcp85_2040s.csv"  "pav_DJF_rcp85_2080s.csv"
>
>
>
> "pav_JJA_histo.csv"
> "pav_JJA_rcp26_2040s.csv"  "pav_JJA_rcp26_2080s.csv"
> "pav_JJA_rcp45_2040s.csv"  "pav_JJA_rcp45_2080s.csv"
> "pav_JJA_rcp85_2040s.csv""pav_JJA_rcp85_2080s.csv"
>
> and so on ...
>
> I would like to apply a t-test of difference in mean between:
>
> "pav_DJF_histo.csv" and all dataframes with names having "pav_DJF..."
>
> the same for
>
> "pav_JJA_histo.csv" and all dataframes with names having "pav_JJA..."
>
> and so on...
>
> There are 84 dataframes in my list. Calculations will be performed as
> above on all 84 dataframes.
>
> Thanks for your suggestions.
> Asong.
>
> Reproducible example for  "pav_DJF_histo.csv" and all dataframes with
> names having "pav_DJF...".
>
> Each df has 1 row * 120 columns.  I will like to do element-wise:
>
> t.test(pav_DJF_histo.csv,all_dataframes)
>
> structure(list(pav_DJF_histo.csv = structure(list(G100_pav_DJF =
> 0.0314208328712871,
> G101_pav_DJF = 0.0316052879207921, G102_pav_DJF = 0.0338115233663366,
> G103_pav_DJF = 0.0349753320792079, G104_pav_DJF = 0.0340410627722772,
> G105_pav_DJF = 0.0344961831683168, G106_pav_DJF = 0.0331672699009901,
> G107_pav_DJF = 0.0335578704950495, G108_pav_DJF = 0.0318934661386139,
> G109_pav_DJF = 0.0326319041584158, G110_pav_DJF = 0.0314491928712871,
> G111_pav_DJF = 0.0295078394059406, G112_pav_DJF = 0.0312701207920792,
> G113_pav_DJF = 0.0274926542574257, G114_pav_DJF = 0.0280412045544554,
> G115_pav_DJF = 0.0308147467326733, G116_pav_DJF = 0.0276809968316832,
> G117_pav_DJF = 0.0334523455445545, G118_pav_DJF = 0.0231678550495049,
> G119_pav_DJF = 0.0329736546534653, G120_pav_DJF = 0.0293986465346535,
> GG10_pav_DJF = 0.0272844384158416, GG11_pav_DJF = 0.0250696742574257,
> GG12_pav_DJF = 0.0267913558415842, GG13_pav_DJF = 0.028447095049505,
> GG14_pav_DJF = 0.0258856714851485, GG15_pav_DJF = 0.0284966926732673,
> GG16_pav_DJF = 0.0259450320792079, GG17_pav_DJF = 0.0275422631683168,
> GG18_pav_DJF = 0.0267659001980198, GG19_pav_DJF = 0.0239620502970297,
> GG20_pav_DJF = 0.0235523667326733, GG21_pav_DJF = 0.0280495275247525,
> GG22_pav_DJF = 0.0260046952475248, GG23_pav_DJF = 0.0245813871287129,
> GG24_pav_DJF = 0.0225754382178218, GG25_pav_DJF = 0.0340031586138614,
> GG26_pav_DJF = 0.0281897057425743, GG27_pav_DJF = 0.0290264237623762,
> GG28_pav_DJF = 0.0387512556435644, GG29_pav_DJF = 0.0316748243564356,
> GG30_pav_DJF = 0.0268749459405941, GG31_pav_DJF = 0.0306790738613861,
> GG32_pav_DJF = 0.0265153081188119, GG33_pav_DJF = 0.0287865821782178,
> GG34_pav_DJF = 0.0269848536633663, GG35_pav_DJF = 0.0237527348514851,
> GG36_pav_DJF = 0.0264141081188119, GG37_pav_DJF = 0.0273517104950495,
> GG38_pav_DJF = 0.0299628936633663, GG39_pav_DJF = 0.0275048685148515,
> GG40_pav_DJF = 0.0196275314851485, GG41_pav_DJF = 0.0226415651485149,
> GG42_pav_DJF = 0.0292957691089109, GG43_pav_DJF = 0.0240719154455446,
> GG44_pav_DJF = 0.0264125300990099, GG45_pav_DJF = 0.0245377025742574,
> GG46_pav_DJF = 0.0254801978217822, GG47_pav_DJF = 0.0264283477227723,
> GG48_pav_DJF = 0.0221284198019802, GG49_pav_DJF = 0.0281992881188119,
> GG50_pav_DJF = 0.0251214203960396, GG51_pav_DJF = 0.022804923960396,
> GG52_pav_DJF = 0.0253265572277228, GG53_pav_DJF = 0.0248078140594059,
> GG54_pav_DJF = 0.0229847805940594, GG55_pav_DJF = 0.0245689685148515,
> GG56_pav_DJF = 0.024459103960396, GG57_pav_DJF = 0.0261233461386139,
> GG58_pav_DJF = 0.0248389976237624, GG59_pav_DJF = 0.0238194382178218,
> GG60_pav_DJF = 0.025920022970297, GG61_pav_DJF = 0.0232416265346535,
> GG62_pav_DJF = 0.0254770396039604, GG63_pav_DJF = 0.0248223295049505,
> GG64_pav_DJF = 0.0249457611881188, GG65_pav_DJF = 0.0237617085148515,
> GG66_pav_DJF = 0.023653757029703, GG67_pav_DJF = 0.0225660194059406,
> GG68_pav_DJF = 0.0209042742574257, GG69_pav_DJF = 0.0253348401980198,
> GG70_pav_DJF = 0.0269268635643564, GG71_pav_DJF = 0.0257322499009901,
> GG72_pav_DJF = 0.0261817491089109, GG73_pav_DJF = 0.0267062437623762,
> GG74_pav_DJF = 0.0254928786138614, GG75_pav_DJF = 0.0263220520792079,
> GG76_pa

Re: [R] apply two functions to column

2015-02-12 Thread Jeff Newmiller
You don't need to do these operations in pieces so the mapply is unnecessary. 
Neither dplyr nor data.table can go faster than (assuming your data frame is 
called DF):

DF$dtm <- as.POSIXct( as.numeric( DF$ts ), tz="GMT", origin="1970-01-01" )

They can in this case save you from having to retype DF more than once, but in 
this case even achieving that requires more typing than the above does.
---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On February 12, 2015 6:15:35 AM EST, arnaud gaboury  
wrote:
>I am little lost between all the possibilities to apply a function to
>a data.frame or data.table.
>
>Here is mine:
>
>structure(list(name = c("poisonivy", "poisonivy", "poisonivy",
>"poisonivy", "poisonivy", "poisonivy", "poisonivy", "poisonivy",
>"cruzecontrol", "agreenmamba", "agreenmamba", "vairis", "vairis",
>"vairis", "vairis", "vairis", "vairis", "xaeth"), text = c("ok",
>"need items ?", "i didn't submit pass codes for a long now",
>"ok", "<@U03AEKYL4>: what app are you talking about ?", "some testing
>with my irc client",
>"ha ha sorry", "for me there is no such question", "Lol.",
>"<@U03AEKWTL|agreenmamba> uploaded a file:
>should I stay or should I go?>",
>"<@U032FHV3S> ", "ok, see you around",
>"yeah, I had a procrastination rush so I started to decode a little",
>" when you submit
>passcodes",
>"intel", "what is the cooldown time or how does it work...;",
>"anybody knows how does \"Passcode circuitry too hot. Wait for cool
>down to enter another passcode.\" works?",
>"and people told that agent their geocities experience would never
>amount to anything (the convo yesterday) "
>), ts = c("1423594336.000138", "1423594311.000136",
>"1423594294.000135",
>"1423594258.000133", "1423594244.000131", "1423497058.000127",
>"1423497041.000126", "1423478555.000123", "1423494427.000125",
>"1423492370.000124", "1423478364.000121", "1423594358.000139",
>"1423594329.000137", "1423594264.000134", "1423594251.000132",
>"1423592204.000130", "1423592174.000129", "1423150354.000112"
>)), .Names = c("name", "text", "ts"), class = c("data.table",
>"data.frame"), row.names = c(NA, -18L))
>
>As you can see, it is of class data.frame and data.table.
>
>I need to work on last column "t"s. These are characters and are times
>in epoch format. I want to have time in Posix. I must thus do two
>things:
>- characters to numeric : myData$ts <- as.numeric(myData$ts)
>- epoch to Posix : mapply(as.POSIXct, tz = 'GMT', origin =
>'1970-01-01', myMerge$ts)
>
>I wonder:
>- if I can apply these two functions at the same time
>- what will be the fastest way : use dplyr package, or work with
>data.table ? My real data will be much more important than the one
>posted.
>
>Thank you for advice and hint.

__
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply Function to Columns

2014-10-24 Thread Rui Barradas

Hello,

Please cc the list, the odds of getting more and better answers are greater.
And you should tell us from what package do the function plot.correlog 
comes. library(what)?


As for your question, assuming you want to save your plots as PNG files, 
you could do something like the following.



fun <- function(p){
fname <- paste0("correlog", p)
png(filename = fname)
plot.correlog(...)
dev.off()
}
ddeg.correlog.list <- lapply(9:20, fun)

See the help page for ?png. It lists several other graphics file formats 
you can use.


Hope this helps,

Rui Barradas

Em 24-10-2014 08:23, Sarah escreveu:

Thank you very much, it helped a lot!

I just have another question know. I want to make plot for every
species. I just add the function « plot correlog » to the previous
function and I have now the following script:

ddeg.correlog.list <- lapply(9:20, function(p)
*plot.correlog*(correlog(plant[plant[,p]=="1", 2], plant[plant[,p]=="1",
3],
  plant[plant[,p]=="1", 4],increment=2500)))

It’s working, but I wanted to know if there is a way to save each plot.
I found that I can use the dev.off function or the ggplot function of
the package ggplot2, but I can’t figure out how to use it within the
lapply function. Do you think there is a way?

Thank you for your help!

Sarah


Le 23 oct. 2014 à 17:49, Rui Barradas mailto:[email protected]>> a écrit :

Hello,

Yes, you can use lapply. Maybe something like the following. Note that
the result is a list with one member per species. (Untested).

ddeg.correlog.list <- lapply(9:11, function(p)
correlog(plant[plant[,p]=="1", 2], plant[plant[,p]=="1", 3],
plant[plant[,p]=="1", 4]))


Hope this helps,

Rui Barradas

Em 23-10-2014 16:06, Sarah escreveu:

Hello List,

I have a database which consist of 912 plots. For each plot, I have
the presence/absence information of 260 species of plants and also 5
different environmental variables (ddeg, mind, srad, slp, topo).

The dataframe looks like this:

  Plot_NumberX Y ddeg mind   srad slp topo Galium_mollugo
Gentiana_nivalis
11 557747.6 149726.8 2598 -625 236363   8  176  0
   0
22 572499.4 145503.5 2178 -176 161970  14 -137  0
   0
33 579100.4 151800.4 1208  632 267572  33  129  0
   0
44 581301.7 150300.1 1645   83 246633  15  -70  0
   0
55 579838.7 124770.9 1102 1637 158300   2 -231  0
   0
66 577011.1 121328.6  731 2223 180286  41   70  0
   0

Now, what I wanted to do is to calculate spatial autocorrelation of
each environmental variable for each species, but only for the plots
where the species is present.

I will use the correlog function of the package ncf (doesn’t really
matter). The correlog function work with an argument X which is the
longitude, an argument Y which is the latitude and an argument Z
which is the variable you want to test for autocorrelation (in my
case, the different environmental variables).

So, for the first species I have the following script:

ddeg.correlog.9<-correlog(plant[plant[,9]=="1", 2],
plant[plant[,9]=="1", 3], plant[plant[,9]=="1", 4])

X = plant[plant[,9]=="1", 2] —>  only the X coordinate where my
species 9 is present
Y = plant[plant[,9]=="1", 3] —>  only the Y coordinate where my
species 9 is present
Z = plant[plant[,9]=="1", 4] —>  only the value of the environmental
variable where my species 9 is present

plant: dataframe
9: column corresponding to the first species
2: column corresponding to the X coordinate
3: column correspondind to the Y coordinate
4: column corresponding to the first environmental variable

So my question is: how do I repeat this script for every species
(basically, I just have to change the number « 9 » into 10, 11 and so
on) ?

I try to write a function but I’m new in R and didn’t manage to do
it. I was also considering to use the function « lapply », but I
don’t think I can use it in this case, isn’t it?

Thank you very much for your help !

Sarah
__
[email protected]  mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply Function to Columns

2014-10-24 Thread Sarah

> Le 24 oct. 2014 à 09:23, Sarah  a écrit :
> 
> Thank you very much, it helped a lot!
> 
> I just have another question know. I want to make plot for every species. I 
> just add the function « plot correlog » to the previous function and I have 
> now the following script: 
> 
> ddeg.correlog.list <- lapply(9:20, function(p)
>   plot.correlog(correlog(plant[plant[,p]=="1", 2], plant[plant[,p]=="1", 3], 
>  plant[plant[,p]=="1", 4],increment=2500)))
> 
> It’s working, but I wanted to know if there is a way to save each plot. I 
> found that I can use the dev.off function or the ggplot function of the 
> package ggplot2, but I can’t figure out how to use it within the lapply 
> function. Do you think there is a way?
> 
> Thank you for your help!
> 
> Sarah
> 
>> Le 23 oct. 2014 à 17:49, Rui Barradas > > a écrit :
>> 
>> Hello,
>> 
>> Yes, you can use lapply. Maybe something like the following. Note that the 
>> result is a list with one member per species. (Untested).
>> 
>> ddeg.correlog.list <- lapply(9:11, function(p)
>>  correlog(plant[plant[,p]=="1", 2], plant[plant[,p]=="1", 3], 
>> plant[plant[,p]=="1", 4]))
>> 
>> 
>> Hope this helps,
>> 
>> Rui Barradas
>> 
>> Em 23-10-2014 16:06, Sarah escreveu:
>>> Hello List,
>>> 
>>> I have a database which consist of 912 plots. For each plot, I have the 
>>> presence/absence information of 260 species of plants and also 5 different 
>>> environmental variables (ddeg, mind, srad, slp, topo).
>>> 
>>> The dataframe looks like this:
>>> 
>>>   Plot_NumberX   Y  ddeg mind   srad slp topo 
>>> Galium_mollugo Gentiana_nivalis
>>> 11  557747.6 149726.8 2598 -625 236363   8  176 
>>>  00
>>> 22  572499.4 145503.5 2178 -176 161970  14 -137 
>>>  00
>>> 33  579100.4 151800.4 1208  632 267572  33  129 
>>>  00
>>> 44  581301.7 150300.1 1645   83 246633  15  -70 
>>>  00
>>> 55  579838.7 124770.9 1102 1637 158300   2 -231 
>>>  00
>>> 66  577011.1 121328.6  731 2223 180286  41   70 
>>>  00
>>> 
>>> Now, what I wanted to do is to calculate spatial autocorrelation of each 
>>> environmental variable for each species, but only for the plots where the 
>>> species is present.
>>> 
>>> I will use the correlog function of the package ncf (doesn’t really 
>>> matter). The correlog function work with an argument X which is the 
>>> longitude, an argument Y which is the latitude and an argument Z which is 
>>> the variable you want to test for autocorrelation (in my case, the 
>>> different environmental variables).
>>> 
>>> So, for the first species I have the following script:
>>> 
>>> ddeg.correlog.9<-correlog(plant[plant[,9]=="1", 2], plant[plant[,9]=="1", 
>>> 3], plant[plant[,9]=="1", 4])
>>> 
>>> X = plant[plant[,9]=="1", 2] —>  only the X coordinate where my species 9 
>>> is present
>>> Y = plant[plant[,9]=="1", 3] —>  only the Y coordinate where my species 9 
>>> is present
>>> Z = plant[plant[,9]=="1", 4] —>  only the value of the environmental 
>>> variable where my species 9 is present
>>> 
>>> plant: dataframe
>>> 9: column corresponding to the first species
>>> 2: column corresponding to the X coordinate
>>> 3: column correspondind to the Y coordinate
>>> 4: column corresponding to the first environmental variable
>>> 
>>> So my question is: how do I repeat this script for every species 
>>> (basically, I just have to change the number « 9 » into 10, 11 and so on) ?
>>> 
>>> I try to write a function but I’m new in R and didn’t manage to do it. I 
>>> was also considering to use the function « lapply », but I don’t think I 
>>> can use it in this case, isn’t it?
>>> 
>>> Thank you very much for your help !
>>> 
>>> Sarah
>>> __
>>> [email protected]  mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help 
>>> 
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>> 
> 


[[alternative HTML version deleted]]

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply Function to Columns

2014-10-23 Thread Rui Barradas

Hello,

Yes, you can use lapply. Maybe something like the following. Note that 
the result is a list with one member per species. (Untested).


ddeg.correlog.list <- lapply(9:11, function(p)
	correlog(plant[plant[,p]=="1", 2], plant[plant[,p]=="1", 3], 
plant[plant[,p]=="1", 4]))



Hope this helps,

Rui Barradas

Em 23-10-2014 16:06, Sarah escreveu:

Hello List,

I have a database which consist of 912 plots. For each plot, I have the 
presence/absence information of 260 species of plants and also 5 different 
environmental variables (ddeg, mind, srad, slp, topo).

The dataframe looks like this:

   Plot_NumberX  Y  ddeg mind   srad slp topo 
Galium_mollugo Gentiana_nivalis
11  557747.6 149726.8 2598 -625 236363   8  176 
 00
22  572499.4 145503.5 2178 -176 161970  14 -137 
 00
33  579100.4 151800.4 1208  632 267572  33  129 
 00
44  581301.7 150300.1 1645   83 246633  15  -70 
 00
55  579838.7 124770.9 1102 1637 158300   2 -231 
 00
66  577011.1 121328.6  731 2223 180286  41   70 
 00

Now, what I wanted to do is to calculate spatial autocorrelation of each 
environmental variable for each species, but only for the plots where the 
species is present.

I will use the correlog function of the package ncf (doesn’t really matter). 
The correlog function work with an argument X which is the longitude, an 
argument Y which is the latitude and an argument Z which is the variable you 
want to test for autocorrelation (in my case, the different environmental 
variables).

So, for the first species I have the following script:

ddeg.correlog.9<-correlog(plant[plant[,9]=="1", 2], plant[plant[,9]=="1", 3], 
plant[plant[,9]=="1", 4])

X = plant[plant[,9]=="1", 2] —>  only the X coordinate where my species 9 is 
present
Y = plant[plant[,9]=="1", 3] —>  only the Y coordinate where my species 9 is 
present
Z = plant[plant[,9]=="1", 4] —>  only the value of the environmental variable 
where my species 9 is present

plant: dataframe
9: column corresponding to the first species
2: column corresponding to the X coordinate
3: column correspondind to the Y coordinate
4: column corresponding to the first environmental variable

So my question is: how do I repeat this script for every species (basically, I 
just have to change the number « 9 » into 10, 11 and so on) ?

I try to write a function but I’m new in R and didn’t manage to do it. I was 
also considering to use the function « lapply », but I don’t think I can use it 
in this case, isn’t it?

Thank you very much for your help !

Sarah
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply function to multiple list arguments

2014-10-14 Thread Rui Barradas

Hello,

Have you tried

mapply(f, list_df, list_par, MoreArgs = list(z = fix), SIMPLIFY = FALSE)

?


Hope this helps,

Rui Barradas

Em 14-10-2014 19:42, Carlos Nasher escreveu:

Hi R helpers,

I'm struggling how to apply a function to multiple lists. My function uses
a dataframe, a list of parameters and a fixed value as arguments. Now I
want to apply that function to several dataframes contained in list, with
several lists of parameters (also contained in a list) and the fixed value.
Here's an example:


#

fix <- 2 # fixed value

x <- c(1,2,3)
y <- c(4,5,6)

df_1 <- data.frame(x,y) # first dataframe
df_2 <- 2*df_1 # second dataframe

list_df <- list(df_1,df_2) # list containing dataframes

par_1 <- list(a=5,b=10) # first list of parameters
par_2 <- list(a=6,b=11) # second list of parameters

list_par <- list(par_1,par_2) # list of lists of parameters

f <- function(data,params,z){
   res <- (data$x*params$a+data$y*params$b)*z
   return(res)
}

res_1 <- f(data = df_1, params = par_1, z = fix) # result of applying
function to first dataframe and first list of parameters
res_2 <- f(data = df_2, params = par_2, z = fix) # result of applying
function to second dataframe and second list of parameters

#

I got the list of dataframes and parameters from a former use of lapply. I
was hoping to get the desired results (res_1, res_2) again in a list. I
tried mapply, but I don't get it running. Can anybody help?

Thanks and best regards,
Carlos





__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply if else statement to vector

2014-10-02 Thread Berend Hasselman

On 02-10-2014, at 11:01, [email protected] wrote:

> Subscribers,
> 
> What is the correct syntax to apply the 'if else' conditional statement to 
> vector objects?
> 
> Example:
> 
> vectorx<-c(50,50,20,70)
> vectory<-c(50,50,20,20)
> vectorz<-function () {
>   if (vectorx>vectory)
>   vectorx
>   else vectorx<-0
> }
> 
> vectorz()
> Warning message:
> In if (vectorx > vectory) vectorx else vectorx <- 0 :
>  the condition has length > 1 and only the first element will be used
> 
> The help manual (?'if') explains that only length=0 is acceptable; what is an 
> appropriate alternative function to use please?
> 

And at the bottom of that page (section See Also) there is a reference to 
ifelse. Why didn’t you have a look at that?

Replace your complete if/else statement with ifelse(vectorx>vectory,vectorx,0)

Berend


> 
> 0 0 0 70
> 
> __
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply if else statement to vector

2014-10-02 Thread PIKAL Petr
Hi

in this case correct syntax is not to use if at all

vectorx*(vectorx>vectory)
1]  0  0  0 70

if you insist you can use

?ifelse

which is "vectorised" if

Regards
Petr


> -Original Message-
> From: [email protected] [mailto:r-help-bounces@r-
> project.org] On Behalf Of [email protected]
> Sent: Thursday, October 02, 2014 11:02 AM
> To: [email protected]
> Subject: [R] apply if else statement to vector
>
> Subscribers,
>
> What is the correct syntax to apply the 'if else' conditional statement
> to vector objects?
>
> Example:
>
> vectorx<-c(50,50,20,70)
> vectory<-c(50,50,20,20)
> vectorz<-function () {
>   if (vectorx>vectory)
>   vectorx
>   else vectorx<-0
> }
>
> vectorz()
> Warning message:
> In if (vectorx > vectory) vectorx else vectorx <- 0 :
>the condition has length > 1 and only the first element will be used
>
> The help manual (?'if') explains that only length=0 is acceptable; what
> is an appropriate alternative function to use please?
>
> The desired result for vectorz is:
>
> 0 0 0 70
>
> __
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.


Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately 
accept such offer; The sender of this e-mail (offer) excludes any acceptance of 
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an 
express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into 
any contracts on behalf of the company except for cases in which he/she is 
expressly authorized to do so in writing, and such authorization or power of 
attorney is submitted to the recipient or the person represented by the 
recipient, or the existence of such authorization is known to the recipient of 
the person represented by the recipient.
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply block of if statements with menu function

2014-09-22 Thread PIKAL Petr
Hi

> -Original Message-
> From: [email protected] [mailto:[email protected]]
> Sent: Thursday, September 18, 2014 4:35 PM
> To: PIKAL Petr
> Cc: [email protected]
> Subject: RE: [R] apply block of if statements with menu function
>
> On 2014-09-16 12:35, PIKAL Petr wrote:
> >
> > So if result of menu is 0 (you did not choose anything) you can
> either
> > stay with 0, then switch does not return anything or add 1 and let
> > evaluate something meaningful specified in second and following
> > positions of switch command.
> >
>
> Thanks for your explanation, which completed my understanding! :) For
> the benefit of other novices, below is an example to demonstrate how
> 'switch' and 'menu' can be used:
>
> switch(menu(c(1,2),graphics=FALSE,title='select something'),
> {(seq(1:10))}, {(rnorm(20))})
>
> However, how to make the option '0 to exit' to appear in the command
> terminal?

Hm. Put it in a title.

switch(menu(c(1,2), title="select item from menu, 0 to exit"), {(seq(1:10))}, 
{(rnorm(20))})

Or modify source code of menu function to suite your needs. Nobody can restrain 
you to change it.

Regards
Petr

>



Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately 
accept such offer; The sender of this e-mail (offer) excludes any acceptance of 
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an 
express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into 
any contracts on behalf of the company except for cases in which he/she is 
expressly authorized to do so in writing, and such authorization or power of 
attorney is submitted to the recipient or the person represented by the 
recipient, or the existence of such authorization is known to the recipient of 
the person represented by the recipient.
__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply block of if statements with menu function

2014-09-18 Thread rl

On 2014-09-16 12:35, PIKAL Petr wrote:


So if result of menu is 0 (you did not choose anything) you can either
stay with 0, then switch does not return anything or add 1 and let
evaluate something meaningful specified in second and following
positions of switch command.



Thanks for your explanation, which completed my understanding! :) For 
the benefit of other novices, below is an example to demonstrate how 
'switch' and 'menu' can be used:


switch(menu(c(1,2),graphics=FALSE,title='select something'), 
{(seq(1:10))}, {(rnorm(20))})


However, how to make the option '0 to exit' to appear in the command 
terminal?


__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply block of if statements with menu function

2014-09-16 Thread PIKAL Petr
Hi

One comment: I never used menu or switch, this is just how I understand its 
function. So you probably are on a wrong track and do not understand what are 
objects and functions in R languange.

menu

Description
menu presents the user with a menu of choices labelled from 1 to the number of 
choices. To exit without choosing an item one can select 0.

Value
The number corresponding to the selected item, or 0 if no choice was made.

It is obvious (for me :) that results of menu command is 0,1,2, ... depending 
on how many items are in menu

switch

Description
switch evaluates EXPR and accordingly chooses one of the further arguments (in 
...).

Details
switch works in two distinct ways depending whether the first argument 
evaluates to a character string or a number.

If the value of EXPR is not a character string it is coerced to integer. If the 
integer is between 1 and nargs()-1 then the corresponding element of ... is 
evaluated and the result returned: thus if the first argument is 3 then the 
fourth argument is evaluated and returned.

> switch(1,"a","b","c","d")
[1] "a"
> switch(2,"a","b","c","d")
[1] "b"
> switch(3,"a","b","c","d")
[1] "c"
> switch(4,"a","b","c","d")
[1] "d"
> switch(0,"a","b","c","d")
>

So if result of menu is 0 (you did not choose anything) you can either stay 
with 0, then switch does not return anything or add 1 and let evaluate 
something meaningful specified in second and following positions of switch 
command.

Regards
Petr


> -Original Message-
> From: [email protected] [mailto:[email protected]]
> Sent: Tuesday, September 16, 2014 2:17 PM
> To: PIKAL Petr
> Cc: [email protected]
> Subject: RE: [R] apply block of if statements with menu function
>
> On 2014-09-16 10:50, PIKAL Petr wrote:
> >
> >> switch(menu(c("List letters", "List LETTERS")) + 1,
> >>   cat("Nothing done\n"), letters, LETTERS)
> >>
> >> why is the result changed if ' + 1,' removed?
> >
> > Because +1 belongs to switch not to menu. You can translate above to:
> >
>
> The help pages ?switch, ?menu do not describe (in my eyes! :) ) the
> significance of '+ 1'; where is this described in more detail please?



Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately 
accept such offer; The sender of this e-mail (offer) excludes any acceptance of 
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on

Re: [R] apply block of if statements with menu function

2014-09-16 Thread rl

On 2014-09-16 10:50, PIKAL Petr wrote:



switch(menu(c("List letters", "List LETTERS")) + 1,
  cat("Nothing done\n"), letters, LETTERS)

why is the result changed if ' + 1,' removed?


Because +1 belongs to switch not to menu. You can translate above to:



The help pages ?switch, ?menu do not describe (in my eyes! :) ) the 
significance of '+ 1'; where is this described in more detail please?


__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply block of if statements with menu function

2014-09-16 Thread PIKAL Petr
Hi

> Selection: 1
> Error in if (menu1 == 1) menu1a <- menu(c("1a", "2a", "3a", "4a"),
> graphics = FALSE,  :
>   argument is of length zero
>
> How to correct this error please, for this zero length argument?

Instead of writing functions which you are unable to debug and resolve look 
what elements of your function returns.

Here is what your first line does

> menu1<-switch(menu(c(1,2,3,4),graphics=FALSE,title="select something"))
select something

1: 1
2: 2
3: 3
4: 4

Selection: 3
> menu1
NULL

Regardless of selection menu1 stays NULL, what is probably not what you want.

> switch(menu(c("List letters", "List LETTERS")) + 1,
>   cat("Nothing done\n"), letters, LETTERS)
>
> why is the result changed if ' + 1,' removed?

Because +1 belongs to switch not to menu. You can translate above to:

open menu with 2 items
select one of those 2 items  or press 0
return 0, 1 or 2 based on your choice
add 1 to returned value to enable **switch** to perform first, second or third 
item
perform desired item e.g print Nothing done, letters or LETTERS.

Nothing more, nothing less.

so in your case it shall be

menu1 <- menu(c(1,2,3,4),graphics=FALSE,title='select something')
if (menu1 == 1) ...

or
menu1 <- menu1+1

switch (menu1, "Nothing selected",
{do something1},
{do something2},
{do something3},
{do something4})

Regards
Petr

> -Original Message-
> From: [email protected] [mailto:r-help-bounces@r-
> project.org] On Behalf Of [email protected]
> Sent: Tuesday, September 16, 2014 11:10 AM
> To: David L Carlson
> Cc: [email protected]
> Subject: Re: [R] apply block of if statements with menu function
>
> On 2014-09-15 14:22, David L Carlson wrote:
> > I think switch() should work for you here, but it is not clear how
> > much flexibility you are trying to have (different tests based on the
> > first response; different tests based on first, then second response;
> > different tests based on each successive response).
> >
>
> The help page for 'menu' shows:
>
> switch(menu(c("List letters", "List LETTERS")) + 1,
>   cat("Nothing done\n"), letters, LETTERS)
>
> why is the result changed if ' + 1,' removed?
>
> The help page for switch states that "arguments" are required, but this
> conflicts with the syntax for the function 'menu':
>
> > testswitch<-function (testfunctionname) {
> + menu1<-switch(menu(c(1,2,3,4),graphics=FALSE,title='select
> something'))
> + if (menu1==1)
> + menu1a<-menu(c('1a','2a','3a','4a'),graphics=FALSE,title='select
> something else')
> + if (menu1a==1)
> + object1<-seq(1:10)
> + object2<-seq(20:30)
> + menu2a<-menu(c('5a','6a','7a','8a'),graphics=FALSE,title='select
> something else again')
> + if (menu2a==2)
> + object3<-'3y'
> + finalobject<<-object3
> + if (menu1==2)
> + menu2x<-menu(c('5a','15a','20a'),graphics=FALSE,title='select
> something else')
> + if (menu2x==1)
> + object22<-seq(10:100)
> + object22<-seq(2:30)
> + object33<-rnorm(10)
> + menu2y<-menu(c('5a','6a','7a','8a'),graphics=FALSE,title='select
> something else again')
> + if (menu2y==2)
> + object3<-'3y'
> + finalobject<<-object3
> + }
> > testswitch(testfunctionname)
> select something
>
> 1: 1
> 2: 2
> 3: 3
> 4: 4
>
> Selection: 1
> Error in if (menu1 == 1) menu1a <- menu(c("1a", "2a", "3a", "4a"),
> graphics = FALSE,  :
>   argument is of length zero
>
> How to correct this error please, for this zero length argument?
>
> __
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.


Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpoždě

Re: [R] apply block of if statements with menu function

2014-09-16 Thread rl

On 2014-09-15 14:22, David L Carlson wrote:

I think switch() should work for you here, but it is not clear how
much flexibility you are trying to have (different tests based on the
first response; different tests based on first, then second response;
different tests based on each successive response).



The help page for 'menu' shows:

switch(menu(c("List letters", "List LETTERS")) + 1,
cat("Nothing done\n"), letters, LETTERS)

why is the result changed if ' + 1,' removed?

The help page for switch states that "arguments" are required, but this 
conflicts with the syntax for the function 'menu':



testswitch<-function (testfunctionname) {
+ menu1<-switch(menu(c(1,2,3,4),graphics=FALSE,title='select 
something'))

+ if (menu1==1)
+ menu1a<-menu(c('1a','2a','3a','4a'),graphics=FALSE,title='select 
something else')

+ if (menu1a==1)
+ object1<-seq(1:10)
+ object2<-seq(20:30)
+ menu2a<-menu(c('5a','6a','7a','8a'),graphics=FALSE,title='select 
something else again')

+ if (menu2a==2)
+ object3<-'3y'
+ finalobject<<-object3
+ if (menu1==2)
+ menu2x<-menu(c('5a','15a','20a'),graphics=FALSE,title='select 
something else')

+ if (menu2x==1)
+ object22<-seq(10:100)
+ object22<-seq(2:30)
+ object33<-rnorm(10)
+ menu2y<-menu(c('5a','6a','7a','8a'),graphics=FALSE,title='select 
something else again')

+ if (menu2y==2)
+ object3<-'3y'
+ finalobject<<-object3
+ }

testswitch(testfunctionname)

select something

1: 1
2: 2
3: 3
4: 4

Selection: 1
Error in if (menu1 == 1) menu1a <- menu(c("1a", "2a", "3a", "4a"), 
graphics = FALSE,  :

argument is of length zero

How to correct this error please, for this zero length argument?

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply block of if statements with menu function

2014-09-15 Thread rl

On 2014-09-15 14:22, David L Carlson wrote:

I think switch() should work for you here, but it is not clear how
much flexibility you are trying to have (different tests based on the
first response; different tests based on first, then second response;
different tests based on each successive response).



Yes, different tests are to be written dependent upon the first 
response.


__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply block of if statements with menu function

2014-09-15 Thread David L Carlson
I think switch() should work for you here, but it is not clear how much 
flexibility you are trying to have (different tests based on the first 
response; different tests based on first, then second response; different tests 
based on each successive response). 

?switch

For the second question just index the return value:

> let <- letters[1:4]
> let[menu(let)]

1: a
2: b
3: c
4: d

Selection: 3
[1] "c"

Or a bit more polished:

> cat("Choice: ", let[menu(let)], "\n")

1: a
2: b
3: c
4: d

Selection: 4
Choice:  d

-
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-Original Message-
From: [email protected] [mailto:[email protected]] On 
Behalf Of [email protected]
Sent: Monday, September 15, 2014 3:53 AM
To: [email protected]
Subject: [R] apply block of if statements with menu function

Subscribers,

apply block of if statements with menu function
Subscribers,

For a menu:

menu(c('a','b','c','d'))

How to create a function that will apply to specific menu choice 
objects? For example:

object1<-function (menuifchoices) {
menu1<-menu(c('a','b','c','d'))
if (menu1==1)
...
menu1a<-menu...
if (menu1a==1)
...
menu2a<-menu...
if (menu2a==1)
...
menu2
<-menu(c('a','b','c','d'))
if (menu1==2)
...
}

The request action is that a user can select a menu option that will 
activate a series of "multiple choice" questions, results in "menu1" 
being activated without menu2 being activated. If someone could direct 
to the relevant terminology, thank you.

Separate question; for a menu:

menu(c('a','b','c','d'))

1: a
2: b
3: c
4: d

Selection: 1
[1] 1

is it possible to change behaviour so that result of the selection is 
not the integer, but the original menu choice:

Selection: 1
[1] a

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply rmarkdown::render() outside the RStudio don't find pandoc

2014-08-28 Thread walmes .
Yihui,

Thank you so much. I read the PANDOC.md as you suggested and this solved my
issue. I've created the soft-links and then I run in linux terminal

$ echo "rmarkdown::render('teste01.Rmd')" | R --vanilla

R version 3.1.1 (2014-07-10) -- "Sock it to Me"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
... skip ...

> rmarkdown::render('teste01.Rmd')

processing file: teste01.Rmd
  |.|  20%

label: unnamed-chunk-2 (with options)
List of 1
 $ echo: logi FALSE

  |.| 100%
  ordinary text without R code


output file: teste01.knit.md

/usr/local/bin/pandoc teste01.utf8.md --to html --from
markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures
--output teste01.html --smart --email-obfuscation none --standalone
--section-divs --table-of-contents --toc-depth 3 --template
/home/walmes/R/x86_64-pc-linux-gnu-library/3.1/rmarkdown/rmd/h/default.html
--css /home/walmes/Dropbox/ridiculas/markdown/ridiculas.css --variable
theme:bootstrap --include-in-header
/tmp/RtmpW1cQMQ/rmarkdown-str14a07b76054a.html --mathjax --variable
mathjax-url:
https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML
--no-highlight --variable highlightjs=teste01_files/highlight

Output created: teste01.html

Everything works! Thanks you.

W.


​

[[alternative HTML version deleted]]

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply rmarkdown::render() outside the RStudio don't find pandoc

2014-08-28 Thread Yihui Xie
Please check out the instructions here:
https://github.com/rstudio/rmarkdown/blob/master/PANDOC.md

Creating an alias might work, although I have not really tried. What I
have been doing is to uninstall the old version of Pandoc in my
system, and create symlinks following the instructions above. You said

> I try overcome this by creating an alias
> *pandoc='/usr/lib/rstudio/bin/pandoc/pandoc'*, bus this doesn't work.

but you did not describe what "this doesn't work" really means
(whenever you report something that does not work, please attach the
error message so we do not have to imagine what could be wrong).
Anyway, at least you have to let your system know where is
pandoc-citeproc (per instructions above again), which may or may not
be the problem given the missing error message.

Regards,
Yihui
--
Yihui Xie 
Web: http://yihui.name


On Thu, Aug 28, 2014 at 2:06 PM, walmes .  wrote:
> Hello,
>
> I want to render a Rmd file using rmarkdown::render() function but I'm used
> to edit files with Emacs and I open a linux terminal session to compile
> this file with knitr::knit2html()
>
> ## On the linux terminal.
> $ echo "require(knitr); knit2html('teste01.Rmd')" | R --vanilla
>
> I want to use rmarkdown::render instead. When I tried this, with a R
> session open in the linux terminal, I get the message
>
> ## In a R session inside the linux terminal
>> rmarkdown::render("teste01.Rmd")
> Error: pandoc version 1.12.3 or higher is required and was not found.
>
> So I open the the teste01.Rmd file in RStudio and clicked on the "knit
> HTML" button and it works. The output is bellow.
>
> ## Output in the RStudio after clicking on the "Knit HTML" button.
>
> processing file: teste01.Rmd
>   |.|  20%
>   ordinary text without R code
>
>   |..   |  40%
> label: unnamed-chunk-1
>   |...  |  60%
>   ordinary text without R code
>
>   | |  80%
> label: unnamed-chunk-2 (with options)
> List of 1
>  $ echo: logi FALSE
>
>   |.| 100%
>   ordinary text without R code
>
>
> output file: teste01.knit.md
>
> */usr/lib/rstudio/bin/pandoc/pandoc* teste01.utf8.md --to html --from
> markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures
> --output teste01.html --smart --email-obfuscation none --standalone
> --section-divs --table-of-contents --toc-depth 3 --template
> /home/walmes/R/x86_64-pc-linux-gnu-library/3.1/rmarkdown/rmd/h/default.html
> --css /home/walmes/Dropbox/ridiculas/markdown/ridiculas.css --variable
> theme:cerulean --include-in-header
> /tmp/RtmpkgSjDp/rmarkdown-str4aee5c225085.html --mathjax --variable
> mathjax-url:
> https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML
> --no-highlight --variable highlightjs=teste01_files/highlight --variable
> highlightjs-theme=textmate
>
> Output created: teste01.html
>
> Its possible to see that RStudio call the pandoc that is at
> */usr/lib/rstudio/bin/pandoc/* and not the old version of pandoc in my
> system.
>
> I try overcome this by creating an alias
> *pandoc='/usr/lib/rstudio/bin/pandoc/pandoc'*, bus this doesn't work.
> Someone has any idea to solve this? I want to use rmarkdown::render()
> outside the RStudio.
>
> Thanks.
> Walmes.
>
> ==
> Walmes Marques Zeviani
> LEG (Laboratório de Estatística e Geoinformação, 25.450418 S, 49.231759 W)
> Departamento de Estatística - Universidade Federal do Paraná
> fone: (+55) 41 3361 3573
> skype: walmeszeviani
> homepage: http://www.leg.ufpr.br/~walmes
> linux user number: 531218
> ==

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply quantile function to each dataframe in a List R

2014-08-06 Thread Zilefac Elvis
Great! Thanks.
AT.


On Monday, August 4, 2014 8:59 PM, rhelpmaillist  wrote:



Try this:
lapply(a,function(x) apply(x[,-c(1,2)],2,quantile,probs=0.95))
# a is your example list
At 2014-08-05 09:17:23, "Zilefac Elvis"  wrote:
>Hello,
>I would like to calculate for each numeric column in a dataframe, 
>quantile(x,probs=o.95).
>My list object has 120 dataframes, each dataframe has 102 columns.
>Here is an example as well as a reproducible example:
>
>[[120]] 
>  Year Site Sim001 Sim002 Sim003 Sim004 Sim005 Sim006 Sim007 Sim008 Sim009 
>Sim010 Sim011 Sim012 Sim013 Sim014 Sim015
>...
>1 2001 GGG9 -26.96 -27.30 -31.62 -31.56 -28.58 -33.82 -32.53 -30.56 -34.09 
>-31.94 -28.85 -30.76 -25.44 -31.83 -32.98 
>2 2002 GGG9 -32.57 -39.15 -32.87 -32.91 -32.50 -35.35 -31.82 -38.22 -37.76 
>-36.70 -38.07 -36.99 -35.78 -33.61 -34.47 
>3 2003 GGG9 -37.14 -33.33 -31.53 -32.37 -37.97 -35.00 -31.79 -36.03 -34.48 
>-43.28 -30.47 -33.91 -39.28 -33.28 -34.13
>
>
>I have tried something like:
>
>output<-lapply(lst3,function(x) quantile(x,0.95,na.rm = TRUE)) but failed.
>
>Please help.
>AT.
>
>
>list(structure(list(Year = 2001:2005, Site = structure(c(1L, 
>1L, 1L, 1L, 1L), .Label = "G100", class = "factor"), Sim001 = c(-33.25, 
>-37.52, -34.58, -36.56, -41.62), Sim002 = c(-29.39, -29.35, -36.49, 
>-37.82, -31.6), Sim003 = c(-35.24, -35.2, -41.98, -32.87, -35.92 
>), Sim004 = c(-33.41, -38.76, -34.92, -34.46, -38.54), Sim005 = c(-38.22, 
>-40.83, -37.55, -39.85, -39.02), Sim006 = c(-39.5, -34.35, -35.35, 
>-37.08, -31.21), Sim007 = c(-31.24, -32.77, -35.44, -41.1, -31.81 
>), Sim008 = c(-31.34, -32.61, -41.48, -49.67, -37.88), Sim009 = c(-33.62, 
>-41.62, -35.9, -35.3, -30.11), Sim010 = c(-39.99, -41.97, -36.96, 
>-47.55, -30.46), Sim011 = c(-34.39, -39.31, -33.68, -33.83, -39.24 
>), Sim012 = c(-33.38, -39.17, -41.91, -42.09, -37.97), Sim013 = c(-31.29, 
>-36.91, -34.52, -39.92, -50.34), Sim014 = c(-26, -43.03, -39.93, 
>-39.49, -40.47), Sim015 = c(-36.16, -40.72, -41.89, -38.75, -40.51 
>), Sim016 = c(-34.6, -47.22, -43.12, -42.55, -31.65), Sim017 = c(-35.47, 
>-50.25, -38.42, -39.82, -46.7), Sim018 = c(-40.57, -32.57, -35.16, 
>-42.74, -36.55), Sim019 = c(-34.48, -39.42, -33.47, -40.78, -42.11 
>), Sim020 = c(-37.23, -43.42, -37, -41.89, -33.36), Sim021 = c(-32.85, 
>-37.18, -37.75, -38.22, -36.5), Sim022 = c(-42.2, -31.78, -38.01, 
>-43.85, -40.05), Sim023 = c(-36.94, -40.03, -42.37, -33.45, -36.9 
>), Sim024 = c(-40.03, -32.38, -45.69, -39.67, -36.97), Sim025 = c(-31.62, 
>-45.37, -39.73, -39.7, -40.93), Sim026 = c(-32.57, -38.02, -40.12, 
>-39.19, -41.55), Sim027 = c(-35.85, -31.56, -40.41, -47.34, -33.9 
>), Sim028 = c(-34.7, -38.88, -46.31, -32.77, -36.01), Sim029 = c(-36.67, 
>-51.07, -38.01, -39.3, -34.99), Sim030 = c(-30.54, -42.89, -35.83, 
>-42.01, -38.85), Sim031 = c(-31.85, -40.38, -37.87, -38.88, -40.28 
>), Sim032 = c(-33.56, -36.81, -37.11, -38.8, -34.17), Sim033 = c(-37.87, 
>-38.4, -39.56, -39.82, -36.37), Sim034 = c(-37.13, -32.84, -42.93, 
>-37.4, -44.44), Sim035 = c(-26.66, -41.08, -40.88, -39.22, -40.84 
>), Sim036 = c(-34.51, -44.35, -39.38, -33.86, -33.55), Sim037 = c(-33.89, 
>-33.5, -37.59, -53.07, -36.6), Sim038 = c(-35.14, -34.92, -36.96, 
>-43.16, -44.19), Sim039 = c(-31.4, -33.95, -39.67, -39.75, -38.53 
>), Sim040 = c(-33.18, -32.84, -36.04, -39.64, -40.71), Sim041 = c(-37.38, 
>-32.74, -44.38, -45.02, -43.96), Sim042 = c(-28.09, -34.78, -42.21, 
>-43.18, -40.85), Sim043 = c(-36.86, -35.93, -42.46, -41.11, -42.35 
>), Sim044 = c(-35.3, -41.2, -36.65, -44.91, -49.74), Sim045 = c(-32.84, 
>-37.83, -42.22, -41.14, -37.1), Sim046 = c(-32.85, -41.35, -37.01, 
>-35.27, -41.13), Sim047 = c(-38.19, -49.26, -40.83, -35.04, -38.49 
>), Sim048 = c(-37.98, -37.82, -40.33, -35.01, -36.94), Sim049 = c(-40.37, 
>-34.5, -39.55, -45.6, -31.79), Sim050 = c(-40.67, -36.04, -47.68, 
>-38.26, -46.9), Sim051 = c(-37.13, -31.42, -38.49, -36.58, -38.52 
>), Sim052 = c(-43.54, -32.82, -37.32, -41.71, -37.87), Sim053 = c(-31.61, 
>-37.32, -40.25, -37.24, -49.9), Sim054 = c(-38.85, -33.53, -46.48, 
>-38.27, -34.13), Sim055 = c(-30.71, -40.04, -43.77, -33.78, -33.5 
>), Sim056 = c(-30.3, -36.91, -38.49, -36.69, -31.43), Sim057 = c(-27.56, 
>-37.13, -39.45, -45.52, -33.24), Sim058 = c(-33.47, -35.26, -33.83, 
>-42.43, -45.4), Sim059 = c(-36.46, -35.64, -39.62, -37.95, -41.32 
>), Sim060 = c(-39.12, -41.71, -36.28, -36.27, -36.94), Sim061 = c(-29.86, 
>-45.73, -37.04, -43.84, -36.87), Sim062 = c(-29.46, -40.96, -39.43, 
>-41.75, -38.83), Sim063 = c(-36.48, -41.18, -39.59, -41.01, -36.31 
>), Sim064 = c(-32.49, -38.86, -33.16, -40.2, -32.65), Sim065 = c(-35.37, 
>-39.73, -41.7, -44.36, -41.26), Sim066 = c(-36.78, -38.5, -38.72, 
>-42.46, -39.25), Sim067 = c(-38.12, -37.92, -34.8, -35.18, -33.63 
>), Sim068 = c(-31.75, -36.77, -32.5, -44.28, -36.65), Sim069 = c(-33.48, 
>-46.06, -39.48, -42.89, -39.72), Sim070 = c(-33.53, -36.53, -36.28, 
>-37.78, -39.22), Sim071 = c(-36.27, -42.68, -35.28, -38.86, -36.63 
>), Sim072 = c(-34.63, 

Re: [R] Apply quantile function to each dataframe in a List R

2014-08-04 Thread rhelpmaillist
Try this:
lapply(a,function(x) apply(x[,-c(1,2)],2,quantile,probs=0.95))
# a is your example list
At 2014-08-05 09:17:23, "Zilefac Elvis"  wrote:
>Hello,
>I would like to calculate for each numeric column in a dataframe, 
>quantile(x,probs=o.95).
>My list object has 120 dataframes, each dataframe has 102 columns.
>Here is an example as well as a reproducible example:
>
>[[120]] 
>  Year Site Sim001 Sim002 Sim003 Sim004 Sim005 Sim006 Sim007 Sim008 Sim009 
> Sim010 Sim011 Sim012 Sim013 Sim014 Sim015
>...
>1 2001 GGG9 -26.96 -27.30 -31.62 -31.56 -28.58 -33.82 -32.53 -30.56 -34.09 
>-31.94 -28.85 -30.76 -25.44 -31.83 -32.98 
>2 2002 GGG9 -32.57 -39.15 -32.87 -32.91 -32.50 -35.35 -31.82 -38.22 -37.76 
>-36.70 -38.07 -36.99 -35.78 -33.61 -34.47 
>3 2003 GGG9 -37.14 -33.33 -31.53 -32.37 -37.97 -35.00 -31.79 -36.03 -34.48 
>-43.28 -30.47 -33.91 -39.28 -33.28 -34.13
>
>
>I have tried something like:
>
>output<-lapply(lst3,function(x) quantile(x,0.95,na.rm = TRUE)) but failed.
>
>Please help.
>AT.
>
>
>list(structure(list(Year = 2001:2005, Site = structure(c(1L, 
>1L, 1L, 1L, 1L), .Label = "G100", class = "factor"), Sim001 = c(-33.25, 
>-37.52, -34.58, -36.56, -41.62), Sim002 = c(-29.39, -29.35, -36.49, 
>-37.82, -31.6), Sim003 = c(-35.24, -35.2, -41.98, -32.87, -35.92 
>), Sim004 = c(-33.41, -38.76, -34.92, -34.46, -38.54), Sim005 = c(-38.22, 
>-40.83, -37.55, -39.85, -39.02), Sim006 = c(-39.5, -34.35, -35.35, 
>-37.08, -31.21), Sim007 = c(-31.24, -32.77, -35.44, -41.1, -31.81 
>), Sim008 = c(-31.34, -32.61, -41.48, -49.67, -37.88), Sim009 = c(-33.62, 
>-41.62, -35.9, -35.3, -30.11), Sim010 = c(-39.99, -41.97, -36.96, 
>-47.55, -30.46), Sim011 = c(-34.39, -39.31, -33.68, -33.83, -39.24 
>), Sim012 = c(-33.38, -39.17, -41.91, -42.09, -37.97), Sim013 = c(-31.29, 
>-36.91, -34.52, -39.92, -50.34), Sim014 = c(-26, -43.03, -39.93, 
>-39.49, -40.47), Sim015 = c(-36.16, -40.72, -41.89, -38.75, -40.51 
>), Sim016 = c(-34.6, -47.22, -43.12, -42.55, -31.65), Sim017 = c(-35.47, 
>-50.25, -38.42, -39.82, -46.7), Sim018 = c(-40.57, -32.57, -35.16, 
>-42.74, -36.55), Sim019 = c(-34.48, -39.42, -33.47, -40.78, -42.11 
>), Sim020 = c(-37.23, -43.42, -37, -41.89, -33.36), Sim021 = c(-32.85, 
>-37.18, -37.75, -38.22, -36.5), Sim022 = c(-42.2, -31.78, -38.01, 
>-43.85, -40.05), Sim023 = c(-36.94, -40.03, -42.37, -33.45, -36.9 
>), Sim024 = c(-40.03, -32.38, -45.69, -39.67, -36.97), Sim025 = c(-31.62, 
>-45.37, -39.73, -39.7, -40.93), Sim026 = c(-32.57, -38.02, -40.12, 
>-39.19, -41.55), Sim027 = c(-35.85, -31.56, -40.41, -47.34, -33.9 
>), Sim028 = c(-34.7, -38.88, -46.31, -32.77, -36.01), Sim029 = c(-36.67, 
>-51.07, -38.01, -39.3, -34.99), Sim030 = c(-30.54, -42.89, -35.83, 
>-42.01, -38.85), Sim031 = c(-31.85, -40.38, -37.87, -38.88, -40.28 
>), Sim032 = c(-33.56, -36.81, -37.11, -38.8, -34.17), Sim033 = c(-37.87, 
>-38.4, -39.56, -39.82, -36.37), Sim034 = c(-37.13, -32.84, -42.93, 
>-37.4, -44.44), Sim035 = c(-26.66, -41.08, -40.88, -39.22, -40.84 
>), Sim036 = c(-34.51, -44.35, -39.38, -33.86, -33.55), Sim037 = c(-33.89, 
>-33.5, -37.59, -53.07, -36.6), Sim038 = c(-35.14, -34.92, -36.96, 
>-43.16, -44.19), Sim039 = c(-31.4, -33.95, -39.67, -39.75, -38.53 
>), Sim040 = c(-33.18, -32.84, -36.04, -39.64, -40.71), Sim041 = c(-37.38, 
>-32.74, -44.38, -45.02, -43.96), Sim042 = c(-28.09, -34.78, -42.21, 
>-43.18, -40.85), Sim043 = c(-36.86, -35.93, -42.46, -41.11, -42.35 
>), Sim044 = c(-35.3, -41.2, -36.65, -44.91, -49.74), Sim045 = c(-32.84, 
>-37.83, -42.22, -41.14, -37.1), Sim046 = c(-32.85, -41.35, -37.01, 
>-35.27, -41.13), Sim047 = c(-38.19, -49.26, -40.83, -35.04, -38.49 
>), Sim048 = c(-37.98, -37.82, -40.33, -35.01, -36.94), Sim049 = c(-40.37, 
>-34.5, -39.55, -45.6, -31.79), Sim050 = c(-40.67, -36.04, -47.68, 
>-38.26, -46.9), Sim051 = c(-37.13, -31.42, -38.49, -36.58, -38.52 
>), Sim052 = c(-43.54, -32.82, -37.32, -41.71, -37.87), Sim053 = c(-31.61, 
>-37.32, -40.25, -37.24, -49.9), Sim054 = c(-38.85, -33.53, -46.48, 
>-38.27, -34.13), Sim055 = c(-30.71, -40.04, -43.77, -33.78, -33.5 
>), Sim056 = c(-30.3, -36.91, -38.49, -36.69, -31.43), Sim057 = c(-27.56, 
>-37.13, -39.45, -45.52, -33.24), Sim058 = c(-33.47, -35.26, -33.83, 
>-42.43, -45.4), Sim059 = c(-36.46, -35.64, -39.62, -37.95, -41.32 
>), Sim060 = c(-39.12, -41.71, -36.28, -36.27, -36.94), Sim061 = c(-29.86, 
>-45.73, -37.04, -43.84, -36.87), Sim062 = c(-29.46, -40.96, -39.43, 
>-41.75, -38.83), Sim063 = c(-36.48, -41.18, -39.59, -41.01, -36.31 
>), Sim064 = c(-32.49, -38.86, -33.16, -40.2, -32.65), Sim065 = c(-35.37, 
>-39.73, -41.7, -44.36, -41.26), Sim066 = c(-36.78, -38.5, -38.72, 
>-42.46, -39.25), Sim067 = c(-38.12, -37.92, -34.8, -35.18, -33.63 
>), Sim068 = c(-31.75, -36.77, -32.5, -44.28, -36.65), Sim069 = c(-33.48, 
>-46.06, -39.48, -42.89, -39.72), Sim070 = c(-33.53, -36.53, -36.28, 
>-37.78, -39.22), Sim071 = c(-36.27, -42.68, -35.28, -38.86, -36.63 
>), Sim072 = c(-34.63, -34.58, -41.13, -41.34, -43.57), Sim073 = c(-32.21, 
>-31.49, -42.97, -38.86, -3

Re: [R] apply diff type function to matrix

2014-06-19 Thread David L Carlson
I'm not sure we have enough information, but given your matrix:

> mt <- matrix(c(1, 2, 3, 10, 15, 20, 20, 40, 80, 30, 60, 120), 3, 4)
> mt
 [,1] [,2] [,3] [,4]
[1,]1   10   20   30
[2,]2   15   40   60
[3,]3   20   80  120
# Get differences between adjacent values in each column
> mt.d <- abs(diff(mt))
> mt.d
 [,1] [,2] [,3] [,4]
[1,]15   20   30
[2,]15   40   60
# Get ratio of cols 2-4 to col 1
> mt.d[,-1]/mt.d[,1]
 [,1] [,2] [,3]
[1,]5   20   30
[2,]5   40   60
# Get ratio of cols 2-4 to cols 1-3
> mt.d[,-1]/mt.d[,1:3]
 [,1] [,2] [,3]
[1,]54  1.5
[2,]58  1.5

-
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352


-Original Message-
From: [email protected] [mailto:[email protected]] On 
Behalf Of message
Sent: Thursday, June 19, 2014 8:42 AM
To: [email protected]
Subject: [R] apply diff type function to matrix

Readers,

For a matrix:
1 10 20 30
2 15 40 60
3 20 80 120

Using a spreadsheet, a differential function can be applied to the data 
as a formula:
|(10-15)/(1-2)| ... repeated for each pair of adjacent values
What is the corresponding function in R please?
Is it better to write a new function, or to use some form of function 
'diff'?

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply switch to a table

2014-06-17 Thread David L Carlson
If you are not determined to apply switch(), you can apply apply():

Data <- data.frame(V1=c(1, 5), V2=c(3, 4))
Data
##   V1 V2
## 1  1  3
## 2  5  4
label <- data.frame(freq=c(1:5), lbl=c("One", "Two", "Three", "Four", "Five"))
label
##  freq   lbl
## 11   One
## 22   Two
## 33 Three
## 44  Four
## 55  Five
Data2 <- as.data.frame(apply(Data, 2, function(x) label[x, 2]))
Data2
##  V1 V2 
## [1,] "One"  "Three"
## [2,] "Five" "Four"

This will work if Data is a data.frame, a matrix, or a table.

David Carlson

-Original Message-
From: [email protected] [mailto:[email protected]] On 
Behalf Of Pooya Lalehzari
Sent: Tuesday, June 17, 2014 3:31 PM
To: R help
Subject: [R] apply switch to a table

Hello,

I have a simple Data table like below:
> Data
  V1 V2
1  1  3
2  5  4

And I like to apply a switch to all the values of the table.
Exp for one value:
switch(as.character(Data[2,1]),
   "1"="One",
   "2"="Two",
   "3"="Three",
   "4"="Four",
   "5"="Five"
)

What would be the best way to do this?

Thank you.

Pooya





THIS E-MAIL IS FOR THE SOLE USE OF THE INTENDED RECIPIENT(S) AND MAY CONTAIN 
CONFIDENTIAL AND PRIVILEGED INFORMATION.ANY UNAUTHORIZED REVIEW, USE, 
DISCLOSURE OR DISTRIBUTION IS PROHIBITED. IF YOU ARE NOT THE INTENDED 
RECIPIENT, PLEASE CONTACT THE SENDER BY REPLY E-MAIL AND DESTROY ALL COPIES OF 
THE ORIGINAL E-MAIL.
[[alternative HTML version deleted]]

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply switch to a table

2014-06-17 Thread Jeff Newmiller
That would depend on the actual type of object that your Data variable is. Use 
the dput function so we can get your example days in a reproducible way [1].

[1] 
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On June 17, 2014 1:30:57 PM PDT, Pooya Lalehzari  
wrote:
>Hello,
>
>I have a simple Data table like below:
>> Data
>  V1 V2
>1  1  3
>2  5  4
>
>And I like to apply a switch to all the values of the table.
>Exp for one value:
>switch(as.character(Data[2,1]),
>   "1"="One",
>   "2"="Two",
>   "3"="Three",
>   "4"="Four",
>   "5"="Five"
>)
>
>What would be the best way to do this?
>
>Thank you.
>
>Pooya
>
>
>
>
>
>THIS E-MAIL IS FOR THE SOLE USE OF THE INTENDED RECIPIENT(S) AND MAY
>CONTAIN
>CONFIDENTIAL AND PRIVILEGED INFORMATION.ANY UNAUTHORIZED REVIEW, USE,
>DISCLOSURE
>OR DISTRIBUTION IS PROHIBITED. IF YOU ARE NOT THE INTENDED RECIPIENT,
>PLEASE
>CONTACT THE SENDER BY REPLY E-MAIL AND DESTROY ALL COPIES OF THE
>ORIGINAL E-MAIL.
>   [[alternative HTML version deleted]]
>
>__
>[email protected] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply a function containing a reduce statement to all rows of a 2d array?

2014-03-14 Thread Bert Gunter
Ummm...

totalfunc(0)
totalfunc(100)

totalfunc() always gives the same result, because its argument is not
used in the function body. You probably want something like:

totalfunc <- function (y,z) {Reduce(func1, x = y,
   accumulate = TRUE, init = z)
   }

Also note that your argument names in your apply call are all wrong (I
leave it to you to figure out why). Which all suggests that you really
needed to (re?)-read An Introduction to R or other web tutorial (there
are many good ones).

-- Bert


Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
H. Gilbert Welch




On Fri, Mar 14, 2014 at 6:32 AM, Jim Maas  wrote:
> If possible I would like to apply a function, that contains a "reduce"
> statement, across the rows of 2d array. This code works for me right up
> until the last. Can it be done or is there a better way? My next challenge
> is to pass it a vector of starting values, i.e. each row will have a
> different initial value.
>
> func1 <- function (x,y) {x * 0.6}
> Reduce(func1, x = seq_len(10), accumulate = TRUE, init = 3)
>
> totalfunc <- function (x) {Reduce(func1, x = seq_len(10),
>accumulate = TRUE, init = 3)
>}
>
> totalfunc(0)
>
> startarray <- t(array(1:100, c(10,10)))
>
> newarray <- apply(x = startarray, margin = 1, fun = totalfunc)
>
>
> Thanks
>
> J
>
> --
> Jim Maas
>
> [[alternative HTML version deleted]]
>
> __
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply function to one specific column / Alternative to for loop

2013-11-17 Thread Stageexp

Hi,
Try:
indx <- grep("Test",test_df[,1])  ##assuming that there is some pattern
 res <- within(test_df[-indx,],titel <- rep(test_df$titel[indx],
diff(c(indx,nrow(test_df)+1))-1))

## If you need to change the class

res[] <- lapply(res,function(x) if(any(grepl("[[:alpha:]]",x)))
as.character(x) else as.numeric(as.character(x)))


##Using data.frame(cbind()), etc. creates 


A.K.


This option worked great for me! I knew there was a nicer and much faster
way to solve this. One thing I already learned about R: Never use for-loops,
there is always a better way :-)



--
View this message in context: 
http://r.789695.n4.nabble.com/Apply-function-to-one-specific-column-Alternative-to-for-loop-tp4680566p4680621.html
Sent from the R help mailing list archive at Nabble.com.

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply function to one specific column / Alternative to for loop

2013-11-16 Thread arun
Hi,
Try:
indx <- grep("Test",test_df[,1])  ##assuming that there is some pattern
 res <- within(test_df[-indx,],titel <- rep(test_df$titel[indx], 
diff(c(indx,nrow(test_df)+1))-1))

## If you need to change the class

res[] <- lapply(res,function(x) if(any(grepl("[[:alpha:]]",x))) as.character(x) 
else as.numeric(as.character(x)))


##Using data.frame(cbind()), etc. creates 


A.K.




On Saturday, November 16, 2013 11:14 AM, Stageexp  wrote:
Hi guys, I am a total newbie to R, so I hope this isn't a totally dumb
question. I have a dataframe with a title in one row and the corresponding
values in the next rows. Let's take this example: 

test_df <- data.frame(cbind(titel = "", x = 4:5, y = 1:2))
test_df = rbind(cbind(titel="1.Test", x="", y=""), test_df,
cbind(titel="2.Test", x="", y=""), test_df, cbind(titel="3.Test", x="",
y=""), test_df)

test_df
   titel x y
1 1.Test    
2        4 1
3        5 2
4 2.Test    
5        4 1
6        5 2
7 3.Test    
8        4 1
9        5 2

What I want to have is:
   titel x y
2 1.Test 4 1
3 1.Test 5 2
5 2.Test 4 1
6 2.Test 5 2
8 3.Test 4 1
9 3.Test 5 2

In my example, the title is in every third line, but in my real data there
is no pattern. Each title has at least one line but can have x lines.

I was able to solve my problem in a for loop with the following code:
test_df$titel <- as.character(test_df$titel)
for (i in 1:nrow(test_df))
{
  if (nchar(test_df$titel[i])==0){
    test_df$titel[i]=test_df$titel[i-1]
  }
}
test_df <- subset(test_df,test_df$x!="")


The problem is, I have a lot of data and the for loop is obviously very
slow. Is there a more elegant way to achieve the same? I think I have to use
the apply function, but I don't know how to use it with just one column.




--
View this message in context: 
http://r.789695.n4.nabble.com/Apply-function-to-one-specific-column-Alternative-to-for-loop-tp4680566.html
Sent from the R help mailing list archive at Nabble.com.

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply function to one specific column / Alternative to for loop

2013-11-16 Thread umair durrani
This might be of some use : 
http://nsaunders.wordpress.com/2010/08/20/a-brief-introduction-to-apply-in-r/

Umair Durrani

email: [email protected]


> Date: Sat, 16 Nov 2013 07:30:29 -0800
> From: [email protected]
> To: [email protected]
> Subject: [R] Apply function to one specific column / Alternative to for loop
> 
> Hi guys, I am a total newbie to R, so I hope this isn't a totally dumb
> question. I have a dataframe with a title in one row and the corresponding
> values in the next rows. Let's take this example: 
> 
> test_df <- data.frame(cbind(titel = "", x = 4:5, y = 1:2))
> test_df = rbind(cbind(titel="1.Test", x="", y=""), test_df,
> cbind(titel="2.Test", x="", y=""), test_df, cbind(titel="3.Test", x="",
> y=""), test_df)
> 
> test_df
>titel x y
> 1 1.Test
> 24 1
> 35 2
> 4 2.Test
> 54 1
> 65 2
> 7 3.Test
> 84 1
> 95 2
> 
> What I want to have is:
>titel x y
> 2 1.Test 4 1
> 3 1.Test 5 2
> 5 2.Test 4 1
> 6 2.Test 5 2
> 8 3.Test 4 1
> 9 3.Test 5 2
> 
> In my example, the title is in every third line, but in my real data there
> is no pattern. Each title has at least one line but can have x lines.
> 
> I was able to solve my problem in a for loop with the following code:
> test_df$titel <- as.character(test_df$titel)
> for (i in 1:nrow(test_df))
> {
>   if (nchar(test_df$titel[i])==0){
> test_df$titel[i]=test_df$titel[i-1]
>   }
> }
> test_df <- subset(test_df,test_df$x!="")
> 
> 
> The problem is, I have a lot of data and the for loop is obviously very
> slow. Is there a more elegant way to achieve the same? I think I have to use
> the apply function, but I don't know how to use it with just one column.
> 
> 
> 
> 
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/Apply-function-to-one-specific-column-Alternative-to-for-loop-tp4680566.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> __
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
  
[[alternative HTML version deleted]]

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply a function to a list

2013-11-12 Thread arun
Hi,
You may also flatten the list of lists and apply the function. In the example I 
provided,
lapply(do.call(c,unlist(lst1,recursive=FALSE)),sum) 


#or
use one of the functions from this link
http://stackoverflow.com/questions/8139677/how-to-flatten-a-list-to-a-list-without-coercion

lapply(flatten2(lst1),sum)
A.K.





On Tuesday, November 12, 2013 5:05 PM, arun  wrote:
Hi,

You may try:

(Please provide a reproducible example.)
lst1 <- list(list(list(c(14L, 13L, 5L, 4L, 9L), c(14L, 16L, 13L, 2L)), 
    list(c(3L, 2L, 7L, 1L, 8L), c(1L, 9L, 4L, 8L, 7L, 3L, 5L, 
    2L), c(4L, 2L, 1L))), list(list(c(7L, 14L, 15L, 4L, 3L), 
    c(10L, 12L, 6L, 1L)), list(c(5L, 8L, 10L, 3L, 4L), c(9L, 
4L, 2L, 8L, 5L, 7L, 3L, 1L), c(1L, 4L, 3L

lapply(lst1,function(x) lapply(x,function(y) lapply(y,function(z) sum(z


A.K.

I have a list that has a header with the following information. 
List of 6 
 $ :List of 3 
  ..$ :List of 48 

The structure of the list from the 1st to 4th looks like this. 
[[1]] 
[[1]][[1]] 

[[2]] 
[[2]][[2]] 

[[3]] 
[[3]][[3]] 

[[4]] 
[[4]][[4]] 

I want to apply a function to the entire list but am unable to do it. How can 
one apply such a function to the list?

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply a function to a list

2013-11-12 Thread arun
Hi,

You may try:

(Please provide a reproducible example.)
lst1 <- list(list(list(c(14L, 13L, 5L, 4L, 9L), c(14L, 16L, 13L, 2L)), 
    list(c(3L, 2L, 7L, 1L, 8L), c(1L, 9L, 4L, 8L, 7L, 3L, 5L, 
    2L), c(4L, 2L, 1L))), list(list(c(7L, 14L, 15L, 4L, 3L), 
    c(10L, 12L, 6L, 1L)), list(c(5L, 8L, 10L, 3L, 4L), c(9L, 
4L, 2L, 8L, 5L, 7L, 3L, 1L), c(1L, 4L, 3L

lapply(lst1,function(x) lapply(x,function(y) lapply(y,function(z) sum(z


A.K.

I have a list that has a header with the following information. 
List of 6 
 $ :List of 3 
  ..$ :List of 48 

The structure of the list from the 1st to 4th looks like this. 
[[1]] 
[[1]][[1]] 

[[2]] 
[[2]][[2]] 

[[3]] 
[[3]][[3]] 

[[4]] 
[[4]][[4]] 

I want to apply a function to the entire list but am unable to do it. How can 
one apply such a function to the list?

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply function to every 20 rows between pairs of columns in a matrix

2013-11-11 Thread arun






HI,


set.seed(25)
dat1 <- 
as.data.frame(matrix(sample(c("A","T","G","C"),46482*56,replace=TRUE),ncol=56,nrow=46482),stringsAsFactors=FALSE)
 lst1 <- split(dat1,as.character(gl(nrow(dat1),20,nrow(dat1
res <- lapply(lst1,function(x) sapply(x[,1:8],function(y) sapply(x[,9:56], 
function(z) sum(y==z)/20)))

 length(res)
#[1] 2325  ### check here
 dim(res[[1]])
#[1] 48  8

A.K.



Hi all, I have a set of genetic SNP data that looks like 

Founder1 Founder2 Founder3 Founder4 Founder5 Founder6 Founder7 Founder8 Sample1 
Sample2 Sample3 Sample... 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 

The size of the matrix is 56 columns by 46482 rows. I need to 
first bin the matrix by every 20 rows, then compare each of the first 8 
columns (founders) to each columns 9-56, and divide the total number of 
matching letters/alleles by the total number of rows (20). Ultimately I 
need 48 8 column by 2342 row matrices, which are essentially similarity 
matrices. I have tried to extract each pair separately by something like 

"length(cbind(odd[,9],odd[,1])[cbind(odd[,9],cbind(odd[,9],odd[,1])[,1])[,1]=="T"
& cbind(odd[,9],odd[,1])[,2]=="T",])/nrow(cbind(odd[,9],odd[,1]))" 

but this is no where near efficient, and I do not know of a 
faster way of applying the function to every 20 rows and across multiple
pairs. 

In the example given above, if the rows were all identical like 
shown across 20 rows, then the first row of the matrix for Sample1 would
be 

1 1 1 0 0 0 0

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply function to every 20 rows between pairs of columns in a matrix

2013-11-11 Thread arun


Hi,
May be this what you wanted.
res2 <- lapply(row.names(res[[1]]),function(x) 
do.call(rbind,lapply(res,function(y) y[match(x, row.names(y)),])))
 length(res2)
#[1] 48
 dim(res2[[1]])
#[1] 2325    8

A.K.


On Monday, November 11, 2013 10:20 PM, Yu-yu Ren  wrote:

Thank you so much for that script, it works great. One additional request; how 
can I go about binding each of the 2325 matrices for each sample, resulting in 
48 matrices of 8 column by 2325 row?




On Mon, Nov 11, 2013 at 10:02 PM, arun  wrote:


>
>Hi,
>I already sent a reply to R-help.  I am not sure about the "2342".
>
>set.seed(25)
>dat1 <- 
>as.data.frame(matrix(sample(c("A","T","G","C"),46482*56,replace=TRUE),ncol=56,nrow=46482),stringsAsFactors=FALSE)
> lst1 <- split(dat1,as.character(gl(nrow(dat1),20,nrow(dat1
>res <- lapply(lst1,function(x) sapply(x[,1:8],function(y) sapply(x[,9:56], 
>function(z) sum(y==z)/20)))
>
> length(res)
>#[1] 2325  ### check here
> dim(res[[1]])
>#[1] 48  8
>
>A.K.
>
>
>
>
>On Monday, November 11, 2013 10:00 PM, Yu-yu Ren  wrote:
>
>Thank you, I have uploaded several example files, with intermediate outputs of 
>what I have done and the logic flow.
>
>
>
>
>On Mon, Nov 11, 2013 at 9:37 PM,  wrote:
>
>
>>Hi,
>>
>>Comparing the first 8 columns separately with 9-56 columns is not clear.  
>>Also, please provide a reproducible example (using ?dput) for others to work 
>>on.
>>
>>A.K.
>>
>>Hi all, I have a set of genetic SNP data that looks like
>>
>>Founder1 Founder2 Founder3 Founder4 Founder5 Founder6 Founder7 Founder8
>>Sample1 Sample2 Sample3 Sample...
>>A A A T T T T T A T A T
>>A A A T T T T T A T A T
>>A A A T T T T T A T A T
>>A A A T T T T T A T A T
>>A A A T T T T T A T A T
>>A A A T T T T T A T A T
>>A A A T T T T T A T A T
>>A A A T T T T T A T A T
>>A A A T T T T T A T A T
>>A A A T T T T T A T A T
>>A A A T T T T T A T A T
>>A A A T T T T T A T A T
>>
>>The size of the matrix is 56 columns by 46482 rows. I need to first bin the
>>matrix by every 20 rows, then compare each of the first 8 columns (founders)
>>to each columns 9-56, and divide the total number of matching
>>letters/alleles by the total number of rows (20). Ultimately I need 48 8
>>column by 2342 row matrices, which are essentially similarity matrices. I
>>have tried to extract each pair separately by something like
>>
>>"length(cbind(odd[,9],odd[,1])[cbind(odd[,9],cbind(odd[,9],odd[,1])[,1])[,1]=="T"
>>& cbind(odd[,9],odd[,1])[,2]=="T",])/nrow(cbind(odd[,9],odd[,1]))"
>>
>>but this is no where near efficient, and I do not know of a faster way of
>>applying the function to every 20 rows and across multiple pairs.
>>
>>In the example given above, if the rows were all identical like shown across
>>20 rows, then the first row of the matrix for Sample1 would be
>>
>>1 1 1 0 0 0 0
>>
>>
>>Quoted from:
>>http://r.789695.n4.nabble.com/Apply-function-to-every-20-rows-between-pairs-of-columns-in-a-matrix-tp4680272.html
>>
>>
>>_
>>Sent from http://r.789695.n4.nabble.com
>>
>>
>

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply function to every 20 rows between pairs of columns in a matrix

2013-11-11 Thread arun
HI,

It's not very clear.
set.seed(25)
dat1 <- 
as.data.frame(matrix(sample(c("A","T","G","C"),46482*56,replace=TRUE),ncol=56,nrow=46482),stringsAsFactors=FALSE)
 lst1 <- split(dat1,as.character(gl(nrow(dat1),20,nrow(dat1
res <- lapply(lst1,function(x) sapply(x[,1:8],function(y) sapply(x[,9:56], 
function(z) sum(y==z)/20)))

 length(res)
#[1] 2325  ### check here
 dim(res[[1]])
#[1] 48  8

A.K.



Hi all, I have a set of genetic SNP data that looks like 

Founder1 Founder2 Founder3 Founder4 Founder5 Founder6 Founder7 Founder8 Sample1 
Sample2 Sample3 Sample... 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 
A A A T T T T T A T A T 

The size of the matrix is 56 columns by 46482 rows. I need to 
first bin the matrix by every 20 rows, then compare each of the first 8 
columns (founders) to each columns 9-56, and divide the total number of 
matching letters/alleles by the total number of rows (20). Ultimately I 
need 48 8 column by 2342 row matrices, which are essentially similarity 
matrices. I have tried to extract each pair separately by something like 

"length(cbind(odd[,9],odd[,1])[cbind(odd[,9],cbind(odd[,9],odd[,1])[,1])[,1]=="T"
 & cbind(odd[,9],odd[,1])[,2]=="T",])/nrow(cbind(odd[,9],odd[,1]))" 

but this is no where near efficient, and I do not know of a 
faster way of applying the function to every 20 rows and across multiple
 pairs. 

In the example given above, if the rows were all identical like 
shown across 20 rows, then the first row of the matrix for Sample1 would
 be 

1 1 1 0 0 0 0

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Apply a function with multiple argument on each column of matrix

2013-11-11 Thread Mohammad Tanvir Ahamed
Thanks !!
 
Best regards


... 
Tanvir Ahamed
Göteborg, Sweden



On Monday, 11 November 2013, 13:49, Uwe Ligges 
 wrote:
 


On 11.11.2013 13:31, Mohammad Tanvir Ahamed wrote:
> Hi there !!
> I have a function like
> fun <- function(x,y)
> {
> loe<-loess(y ~ x,span=0.9,family="gaussian")
> pre<-predict(loe,data.frame(x=x))
> return(pre)
> }
>
> Now i have defined :
> x<-1:500
>
> y<-matrix(rnorm(1000,3),ncol=2)
>
> I can manipulate fun(x,y[,1]) .
> But i want to apply the function on each column of matrix y .

apply(y, 2, function(i) fun(x, i))

Uwe Ligges



> Any suggestion will be appreciated .
> Thanks .
>
>
> Best regards
>
>
> ...
> Tanvir Ahamed
> Göteborg, Sweden
>     [[alternative HTML version deleted]]
>
>
>
> __
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]

__
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


  1   2   3   4   5   >