Re: [R] Help with function and survey data

2022-10-31 Thread Edjabou Vincent
Dear Bert

Thank you for your suggestion. I have tried it but it did not work.


For record, I am reposting the post with the plain text.
library(tidyverse)
library(plyr)
library(survey)

dat <- structure(list(
   r3a_1 = structure(c(3L, 2L, 3L, 3L, 3L, 3L, 3L,3L, 3L, 3L, 3L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L),
.Label = c("Don't Know", "No", "Yes"), class = "factor"),
  r3a_2 = structure(c(3L, 3L,3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L),
.Label = c("Don't Know", "No", "Yes"), class = "factor"),
  r3a_3 = structure(c(3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L),
.Label = c("Don't Know","No", "Yes"), class = "factor"),
  r3a_4 = structure(c(3L,2L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L,
2L, 3L, 2L, 2L,3L, 3L, 3L, 1L),
.Label = c("Don't Know", "No", "Yes"), class = "factor"),
  r3a_5 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L,2L, 3L,
2L, 3L, 3L, 2L, 3L, 2L, 3L, 1L),
.Label = c("Don't Know","No", "Yes"), class = "factor"),
  r3a_6 = structure(c(3L,3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L,
2L, 2L, 2L, 3L,2L, 3L, 3L, 3L),
.Label = c("Don't Know", "No", "Yes"), class = "factor"),
  r3a_7 = structure(c(1L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 2L,3L, 3L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L),
.Label = c("Don't Know", "No", "Yes"), class = "factor"),
  r3a_8 = structure(c(3L,2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L,
2L, 3L, 3L, 2L,  2L, 2L, 3L, 3L),
.Label = c("Don't Know", "No", "Yes"), class = "factor"),
  r3a_9 = structure(c(1L, 3L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 3L,3L, 3L,
2L, 2L, 2L, 3L, 2L, 2L, 3L, 3L),
.Label = c("Don't Know","No", "Yes"), class = "factor"),
  weight = c(0.34, 0.34, 0.34,0.34, 0.34, 0.34, 0.34, 0.34, 0.34,
0.34, 0.34, 0.34, 0.43, 0.43, 0.43, 0.34, 0.34, 0.34, 0.34, 0.34),
  seg_2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L,
2L, 1L, 1L, 1L,1L, 1L, 1L, 1L),
.Label = c("1", "2"), class = "factor"),
  seg_3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L,
2L, 1L, 1L, 1L,1L, 2L, 2L, 2L),
.Label = c("1", "2"), class = "factor"),
  seg_4 = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L,
2L, 1L, 1L, 1L,1L, 1L, 1L, 1L),
.Label = c("1", "2"), class = "factor")),
  .Names = c("r3a_1", "r3a_2", "r3a_3", "r3a_4", "r3a_5", "r3a_6",
"r3a_7", "r3a_8", "r3a_9", "weight", "seg_2","seg_3","seg_4"),
row.names = c(NA, 20L), class = "data.frame")

 dat_weight <- svydesign(ids = ~1, weights = ~weight, data = dat)

my_funca <- function(mk,y){
   my_re <-   ldply( mk, function(x)
  svytable(bquote(~ y + .(as.name(x))), dat_weight) %>%
as_tibble() %>%
dplyr::group_by({{y}}) %>%
transmute(!!(as.name(x)), Procent = round(n/sum(n,na.rm = T)*100,2)) %>%
pivot_wider(names_from = (as.name(x)),
values_from = Procent)
   )
   return(my_re)
}

my_funca(mk =names(dat)[1:9], y = dat$seg_2)
Regards,

Vincent Edjabou
Mobile: +45 31 95 99 33
linkedin.com/vincent

Orcid: -0003-2849-6151

Regards,

Vincent Edjabou
Mobile: +45 31 95 99 33
linkedin.com/vincent

Orcid: -0003-2849-6151




On Mon, Oct 31, 2022 at 5:21 PM Bert Gunter  wrote:
>
> 1. This is a plain text list. Set your email to post in plain text, not html, 
> which often gets mangled (see below).
>
> 2. I did not run your example, but try:
> my_funca(mk =names(dat)[1:9], y = dat$seg_2)
>
> ## seg_2 is a component of dat and is not in the environment of the call. I 
> did not see any data argument that would tell it to look elsewhere, but I am 
> not familiar with tidy_whatever's nonstandard evaluation conventions.
>
> -- Bert
>
> On Mon, Oct 31, 2022 at 8:39 AM Edjabou Vincent  wrote:
>>
>> Dear R-Help
>> I am working with complex survey data using the survey package.
>> I would like to create a function for the generate multi crosstable. The
>> problem is that I am getting error with the following message:
>> "Error in eval(predvars, data, env) : object 'y' not found"
>>
>> Here is the example:
>> library(tidyverse)
>> library(plyr)
>> library(survey)
>>
>> dat <- structure(list(
>>r3a_1 = structure(c(3L, 2L, 3L, 3L, 3L, 3L, 3L,3L, 3L, 3L, 3L, 2L, 2L,
>> 3L, 3L, 3L, 3L, 3L, 3L, 3L),
>> .Label = c("Don't Know", "No", "Yes"), class = "factor"),
>>   r3a_2 = structure(c(3L, 3L,3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
>> 3L, 3L, 3L, 3L, 3L, 3L, 3L),
>> .Label = c("Don't Know", "No", "Yes"), class =
>> "factor"),
>>   r3a_3 = structure(c(3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L,
>> 2L, 2L, 2L, 2L, 2L, 3L, 3L),
>> .Label = c("Don't Know","No", "Yes"), class =
>> "factor"),
>>   r3a_4 = structure(c(3L,2L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 2L,
>> 3L, 2L, 2L,3L, 3L, 3L, 1L),
>> .Label = c("Don't 

Re: [R] Help with function and survey data

2022-10-31 Thread Bert Gunter
1. This is a plain text list. Set your email to post in plain text, not
html, which often gets mangled (see below).

2. I did not run your example, but try:
my_funca(mk =names(dat)[1:9], y = dat$seg_2)

## seg_2 is a component of dat and is not in the environment of the call. I
did not see any data argument that would tell it to look elsewhere, but I
am not familiar with tidy_whatever's nonstandard evaluation conventions.

-- Bert

On Mon, Oct 31, 2022 at 8:39 AM Edjabou Vincent  wrote:

> Dear R-Help
> I am working with complex survey data using the survey package.
> I would like to create a function for the generate multi crosstable. The
> problem is that I am getting error with the following message:
> "Error in eval(predvars, data, env) : object 'y' not found"
>
> Here is the example:
> library(tidyverse)
> library(plyr)
> library(survey)
>
> dat <- structure(list(
>r3a_1 = structure(c(3L, 2L, 3L, 3L, 3L, 3L, 3L,3L, 3L, 3L, 3L, 2L, 2L,
> 3L, 3L, 3L, 3L, 3L, 3L, 3L),
> .Label = c("Don't Know", "No", "Yes"), class = "factor"),
>   r3a_2 = structure(c(3L, 3L,3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
> 3L, 3L, 3L, 3L, 3L, 3L, 3L),
> .Label = c("Don't Know", "No", "Yes"), class =
> "factor"),
>   r3a_3 = structure(c(3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 3L, 3L),
> .Label = c("Don't Know","No", "Yes"), class =
> "factor"),
>   r3a_4 = structure(c(3L,2L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 2L,
> 3L, 2L, 2L,3L, 3L, 3L, 1L),
> .Label = c("Don't Know", "No", "Yes"), class =
> "factor"),
>   r3a_5 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L,2L, 3L, 2L,
> 3L, 3L, 2L, 3L, 2L, 3L, 1L),
> .Label = c("Don't Know","No", "Yes"), class =
> "factor"),
>   r3a_6 = structure(c(3L,3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 2L,
> 2L, 2L, 3L,2L, 3L, 3L, 3L),
> .Label = c("Don't Know", "No", "Yes"), class =
> "factor"),
>   r3a_7 = structure(c(1L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 2L,3L, 3L, 2L,
> 2L, 2L, 2L, 2L, 3L, 3L, 3L),
> .Label = c("Don't Know", "No", "Yes"), class =
> "factor"),
>   r3a_8 = structure(c(3L,2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 2L,
> 3L, 3L, 2L,  2L, 2L, 3L, 3L),
> .Label = c("Don't Know", "No", "Yes"), class =
> "factor"),
>   r3a_9 = structure(c(1L, 3L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 3L,3L, 3L, 2L,
> 2L, 2L, 3L, 2L, 2L, 3L, 3L),
> .Label = c("Don't Know","No", "Yes"), class =
> "factor"),
>   weight = c(0.34, 0.34, 0.34,0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34,
> 0.34, 0.34, 0.43, 0.43, 0.43, 0.34, 0.34, 0.34, 0.34, 0.34),
>   seg_2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L,
> 1L, 1L, 1L,1L, 1L, 1L, 1L),
> .Label = c("1", "2"), class = "factor"),
>   seg_3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L,
> 1L, 1L, 1L,1L, 2L, 2L, 2L),
> .Label = c("1", "2"), class = "factor"),
>   seg_4 = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L,
> 1L, 1L, 1L,1L, 1L, 1L, 1L),
> .Label = c("1", "2"), class = "factor")),
>   .Names = c("r3a_1", "r3a_2", "r3a_3", "r3a_4", "r3a_5", "r3a_6", "r3a_7",
> "r3a_8", "r3a_9", "weight", "seg_2","seg_3","seg_4"), row.names = c(NA,
> 20L), class = "data.frame")
>
>  dat_weight <- svydesign(ids = ~1, weights = ~weight, data = dat)
>
> my_funca <- function(mk,y){
>my_re <-   ldply( mk, function(x)
>   svytable(bquote(~ y + .(as.name(x))), dat_weight) %>%
> as_tibble() %>%
> dplyr::group_by({{y}}) %>%
> transmute(!!(as.name(x)), Procent = round(n/sum(n,na.rm =
> T)*100,2)) %>%
> pivot_wider(names_from = (as.name(x)),
> values_from = Procent)
>)
>return(my_re)
> }
>
> my_funca(mk =names(dat)[1:9], y = seg_2)
>
>
> I will appreciate any help you will provide.
>
>
>
>
> Regards,
>
> Vincent Edjabou
> Mobile: +45 31 95 99 33
> linkedin.com/vincent
> 
>
> Orcid: -0003-2849-6151
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org 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]]

__
R-help@r-project.org 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.


[R] Help with function and survey data

2022-10-31 Thread Edjabou Vincent
Dear R-Help
I am working with complex survey data using the survey package.
I would like to create a function for the generate multi crosstable. The
problem is that I am getting error with the following message:
"Error in eval(predvars, data, env) : object 'y' not found"

Here is the example:
library(tidyverse)
library(plyr)
library(survey)

dat <- structure(list(
   r3a_1 = structure(c(3L, 2L, 3L, 3L, 3L, 3L, 3L,3L, 3L, 3L, 3L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L),
.Label = c("Don't Know", "No", "Yes"), class = "factor"),
  r3a_2 = structure(c(3L, 3L,3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L),
.Label = c("Don't Know", "No", "Yes"), class =
"factor"),
  r3a_3 = structure(c(3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L),
.Label = c("Don't Know","No", "Yes"), class =
"factor"),
  r3a_4 = structure(c(3L,2L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 2L,
3L, 2L, 2L,3L, 3L, 3L, 1L),
.Label = c("Don't Know", "No", "Yes"), class =
"factor"),
  r3a_5 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L,2L, 3L, 2L,
3L, 3L, 2L, 3L, 2L, 3L, 1L),
.Label = c("Don't Know","No", "Yes"), class =
"factor"),
  r3a_6 = structure(c(3L,3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 2L,
2L, 2L, 3L,2L, 3L, 3L, 3L),
.Label = c("Don't Know", "No", "Yes"), class =
"factor"),
  r3a_7 = structure(c(1L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 2L,3L, 3L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L),
.Label = c("Don't Know", "No", "Yes"), class =
"factor"),
  r3a_8 = structure(c(3L,2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 2L,
3L, 3L, 2L,  2L, 2L, 3L, 3L),
.Label = c("Don't Know", "No", "Yes"), class =
"factor"),
  r3a_9 = structure(c(1L, 3L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 3L,3L, 3L, 2L,
2L, 2L, 3L, 2L, 2L, 3L, 3L),
.Label = c("Don't Know","No", "Yes"), class =
"factor"),
  weight = c(0.34, 0.34, 0.34,0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34,
0.34, 0.34, 0.43, 0.43, 0.43, 0.34, 0.34, 0.34, 0.34, 0.34),
  seg_2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L,
1L, 1L, 1L,1L, 1L, 1L, 1L),
.Label = c("1", "2"), class = "factor"),
  seg_3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L,
1L, 1L, 1L,1L, 2L, 2L, 2L),
.Label = c("1", "2"), class = "factor"),
  seg_4 = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L,
1L, 1L, 1L,1L, 1L, 1L, 1L),
.Label = c("1", "2"), class = "factor")),
  .Names = c("r3a_1", "r3a_2", "r3a_3", "r3a_4", "r3a_5", "r3a_6", "r3a_7",
"r3a_8", "r3a_9", "weight", "seg_2","seg_3","seg_4"), row.names = c(NA,
20L), class = "data.frame")

 dat_weight <- svydesign(ids = ~1, weights = ~weight, data = dat)

my_funca <- function(mk,y){
   my_re <-   ldply( mk, function(x)
  svytable(bquote(~ y + .(as.name(x))), dat_weight) %>%
as_tibble() %>%
dplyr::group_by({{y}}) %>%
transmute(!!(as.name(x)), Procent = round(n/sum(n,na.rm =
T)*100,2)) %>%
pivot_wider(names_from = (as.name(x)),
values_from = Procent)
   )
   return(my_re)
}

my_funca(mk =names(dat)[1:9], y = seg_2)


I will appreciate any help you will provide.




Regards,

Vincent Edjabou
Mobile: +45 31 95 99 33
linkedin.com/vincent


Orcid: -0003-2849-6151

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


[R] Help with function lmodel2

2016-06-12 Thread avcommande




Hello everyone,

 

Does anybody know whether it's possible to calculate extract major axis 
regression residuals on the results of a major axis Model II regression done by 
lmodel2? 

Many thanks, 

Orca

 

Example :

 

> data1 data1

    SSTo  SSTp

1  17.21 20.36

2  19.61 20.19

3  18.60 20.08

4  18.76 19.71

5  18.24 20.48

6  19.10 20.56

7  20.45 20.56

8  18.68 19.71

9  16.55 20.48

10 17.33 20.60

11 18.12 18.93

12 18.02 18.65

13 18.43 18.90

14 18.57 18.62

15 17.40 18.79

16 18.91 19.28

17 18.43 18.58

18 17.02 18.40

19 16.96 17.79

20 17.05 17.82

21 16.98 18.07

22 16.86 18.00

23 17.00 17.79

24 16.70 18.18

25 15.93 18.07

26 17.06 17.85

 

> Reg1<-lmodel2(SSTo~SSTp,data=data1,"interval","interval",99)

> Reg1

 

Model II regression

 

Call: lmodel2(formula = SSTo ~ SSTp, data = data1, range.y =

"interval", range.x = "interval", nperm = 99)

 

n = 26   r = 0.5671202   r-square = 0.3216254 

Parametric P-values:   2-tailed = 0.002517785    1-tailed = 0.001258892 

Angle between the two OLS regression lines = 30.86925 degrees

 

Permutation tests of OLS, MA, RMA slopes: 1-tailed, tail corresponding to sign

A permutation test of r is equivalent to a permutation test of the OLS slope

P-perm for SMA = NA because the SMA slope cannot be tested

 

Regression results

  Method Intercept Slope Angle (degrees) P-perm (1-tailed)

1    OLS  6.651441 0.5862273    30.38000  0.01

2 MA -2.397667 1.0601457    46.67227  0.01

3    SMA -1.892541 1.0336913    45.94911    NA

4    RMA  3.144884 0.7698721    37.59167  0.01

 

Confidence intervals

  Method 2.5%-Intercept 97.5%-Intercept 2.5%-Slope 97.5%-Slope

1    OLS -0.2070452   13.509927  0.2275463   0.9449083

2 MA    -22.1054387    7.308805  0.5517999   2.0922780

3    SMA -9.8957401    3.801737  0.7354715   1.4528336

4    RMA -7.3480741   11.432676  0.3358252   1.3194076

 

Eigenvalues: 1.751428 0.4828554 

 

H statistic used for computing C.I. of MA: 0.09327046

[[alternative HTML version deleted]]

__
R-help@r-project.org 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] help with function calls

2016-03-27 Thread Bert Gunter
...
and here is a maybe slightly neater approach using ?mapply (again with
the method column changed to character():

f <- function(meth,i,fr) do.call(meth,list((fr[i,])))

mapply(FUN=f,meth=input.df[,4],seq_len(nrow(input.df)),
   MoreArgs = list(fr = input.df[,1:3]) )


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 Sun, Mar 27, 2016 at 11:44 AM, Bert Gunter  wrote:
> OOPS! I forgot to tell you that I first changed the "method" column,
> which is a factor, to character, with
>
> input.df$method <- as.character(input.df$method)
>
> Then things will work properly.
>
> -- 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 Sun, Mar 27, 2016 at 11:35 AM, Bert Gunter  wrote:
>> 1. return() is not needed in R functions (it's harmless, however). You
>> might wish to go through an R function tutorial (many good ones are on
>> the web) to learn about what slick things you can do with functions in
>> R.
>>
>> 2. The following is just a brute force loop, so more elegant
>> approaches are likely possible, but this seems to do what you want:
>>
>> sapply(seq_len(nrow(input.df)),
>>function(i)do.call(input.df[i,4],list(x=unlist(input.df[i,1:3])))
>>   )
>>
>> ## see ?do.call
>>
>> 3. I suspect that a change in your data structure might facilitate
>> your task, but of course, not knowing the task, I would not know what
>> changes would be useful. See ?switch  for something that might be
>> related to what you are trying to do.
>>
>> 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 Sun, Mar 27, 2016 at 8:46 AM, Naresh Gurbuxani
>>  wrote:
>>> I have a data frame with with several columns of parameters and one column 
>>> of function name which should be used with these parameters.  My goal is to 
>>> call the appropriate function for each row and summarize the results in a 
>>> list or data frame.  I can partition the data frame according to function 
>>> name, then call each function at a time.  Is there a more elegant way to 
>>> achieve this result with one call only?
>>>
>>> Below is an example.  Here the functions are simple.  It is possible to 
>>> write a function that incorporates both  the functions.  It is not so easy 
>>> in my actual task.  My goal is to make one call and obtain res.df.
>>>
>>> Thanks,
>>> Naresh
>>>
>>>
>>>
>>> sum.sq <- function(x) {return(sum(x^2))}
>>>
>>> sum.cube <- function(x) {return(sum(x^3))}
>>>
>>> input.df <- data.frame(x1 = c(1,2,3,4), x2 = c(2,3,4,5), x3 = c(3,4,5,6), 
>>> method = c(rep("sum.sq", 2), rep("sum.cube", 2)), case = c("a", "b", "c", 
>>> "d"))
>>>
>>> library(plyr)
>>>
>>> res.df1 <- ddply(subset(input.df, method == "sum.sq"), c("case"), 
>>> function(df) {
>>> x.vec <- c(df$x1, df$x2, df$x3)
>>> return(data.frame(sum.power = sum.sq(x.vec)))
>>> })
>>>
>>> res.df2 <- ddply(subset(input.df, method == "sum.cube"), c("case"), 
>>> function(df) {
>>> x.vec <- c(df$x1, df$x2, df$x3)
>>> return(data.frame(sum.power = sum.cube(x.vec)))
>>> })
>>>
>>> res.df <- rbind(res.df1, res.df2)
>>> res.df <- merge(res.df, input.df[,c("method", "case")], by = "case")
>>>
>>>
>>>
>>>
>>> __
>>> R-help@r-project.org 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.

__
R-help@r-project.org 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] help with function calls

2016-03-27 Thread Bert Gunter
OOPS! I forgot to tell you that I first changed the "method" column,
which is a factor, to character, with

input.df$method <- as.character(input.df$method)

Then things will work properly.

-- 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 Sun, Mar 27, 2016 at 11:35 AM, Bert Gunter  wrote:
> 1. return() is not needed in R functions (it's harmless, however). You
> might wish to go through an R function tutorial (many good ones are on
> the web) to learn about what slick things you can do with functions in
> R.
>
> 2. The following is just a brute force loop, so more elegant
> approaches are likely possible, but this seems to do what you want:
>
> sapply(seq_len(nrow(input.df)),
>function(i)do.call(input.df[i,4],list(x=unlist(input.df[i,1:3])))
>   )
>
> ## see ?do.call
>
> 3. I suspect that a change in your data structure might facilitate
> your task, but of course, not knowing the task, I would not know what
> changes would be useful. See ?switch  for something that might be
> related to what you are trying to do.
>
> 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 Sun, Mar 27, 2016 at 8:46 AM, Naresh Gurbuxani
>  wrote:
>> I have a data frame with with several columns of parameters and one column 
>> of function name which should be used with these parameters.  My goal is to 
>> call the appropriate function for each row and summarize the results in a 
>> list or data frame.  I can partition the data frame according to function 
>> name, then call each function at a time.  Is there a more elegant way to 
>> achieve this result with one call only?
>>
>> Below is an example.  Here the functions are simple.  It is possible to 
>> write a function that incorporates both  the functions.  It is not so easy 
>> in my actual task.  My goal is to make one call and obtain res.df.
>>
>> Thanks,
>> Naresh
>>
>>
>>
>> sum.sq <- function(x) {return(sum(x^2))}
>>
>> sum.cube <- function(x) {return(sum(x^3))}
>>
>> input.df <- data.frame(x1 = c(1,2,3,4), x2 = c(2,3,4,5), x3 = c(3,4,5,6), 
>> method = c(rep("sum.sq", 2), rep("sum.cube", 2)), case = c("a", "b", "c", 
>> "d"))
>>
>> library(plyr)
>>
>> res.df1 <- ddply(subset(input.df, method == "sum.sq"), c("case"), 
>> function(df) {
>> x.vec <- c(df$x1, df$x2, df$x3)
>> return(data.frame(sum.power = sum.sq(x.vec)))
>> })
>>
>> res.df2 <- ddply(subset(input.df, method == "sum.cube"), c("case"), 
>> function(df) {
>> x.vec <- c(df$x1, df$x2, df$x3)
>> return(data.frame(sum.power = sum.cube(x.vec)))
>> })
>>
>> res.df <- rbind(res.df1, res.df2)
>> res.df <- merge(res.df, input.df[,c("method", "case")], by = "case")
>>
>>
>>
>>
>> __
>> R-help@r-project.org 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.

__
R-help@r-project.org 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] help with function calls

2016-03-27 Thread Bert Gunter
1. return() is not needed in R functions (it's harmless, however). You
might wish to go through an R function tutorial (many good ones are on
the web) to learn about what slick things you can do with functions in
R.

2. The following is just a brute force loop, so more elegant
approaches are likely possible, but this seems to do what you want:

sapply(seq_len(nrow(input.df)),
   function(i)do.call(input.df[i,4],list(x=unlist(input.df[i,1:3])))
  )

## see ?do.call

3. I suspect that a change in your data structure might facilitate
your task, but of course, not knowing the task, I would not know what
changes would be useful. See ?switch  for something that might be
related to what you are trying to do.

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 Sun, Mar 27, 2016 at 8:46 AM, Naresh Gurbuxani
 wrote:
> I have a data frame with with several columns of parameters and one column of 
> function name which should be used with these parameters.  My goal is to call 
> the appropriate function for each row and summarize the results in a list or 
> data frame.  I can partition the data frame according to function name, then 
> call each function at a time.  Is there a more elegant way to achieve this 
> result with one call only?
>
> Below is an example.  Here the functions are simple.  It is possible to write 
> a function that incorporates both  the functions.  It is not so easy in my 
> actual task.  My goal is to make one call and obtain res.df.
>
> Thanks,
> Naresh
>
>
>
> sum.sq <- function(x) {return(sum(x^2))}
>
> sum.cube <- function(x) {return(sum(x^3))}
>
> input.df <- data.frame(x1 = c(1,2,3,4), x2 = c(2,3,4,5), x3 = c(3,4,5,6), 
> method = c(rep("sum.sq", 2), rep("sum.cube", 2)), case = c("a", "b", "c", 
> "d"))
>
> library(plyr)
>
> res.df1 <- ddply(subset(input.df, method == "sum.sq"), c("case"), 
> function(df) {
> x.vec <- c(df$x1, df$x2, df$x3)
> return(data.frame(sum.power = sum.sq(x.vec)))
> })
>
> res.df2 <- ddply(subset(input.df, method == "sum.cube"), c("case"), 
> function(df) {
> x.vec <- c(df$x1, df$x2, df$x3)
> return(data.frame(sum.power = sum.cube(x.vec)))
> })
>
> res.df <- rbind(res.df1, res.df2)
> res.df <- merge(res.df, input.df[,c("method", "case")], by = "case")
>
>
>
>
> __
> R-help@r-project.org 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.

__
R-help@r-project.org 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.


[R] Help with function jtest - inputs other than from lm

2015-05-03 Thread Reiko
Hi all,

the function jtest allows to compare two non-nested models. The comparison
is made such that the fitted values of one model are included in the
regressor matrix of the other model. It then looks whether there is any
predictive power of these fitted values.

Unfortunately, the input has to come from a linear regression of class lm. I
think that in the function jtest, both the regressor matrix of model 1 and
the fitted values of model 2 will be estimated. Afterwards, it will perform
the J test. 

However, I don't use lm and wanted therefore to ask if you know whether
there is a way to manually define a regressor matrix (e.g. a matrix) and the
fitted values (vector) as inputs? 

Best,
Reiko



--
View this message in context: 
http://r.789695.n4.nabble.com/Help-with-function-jtest-inputs-other-than-from-lm-tp4706738.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org 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.


[R] Help - plot function in Windows

2015-01-16 Thread Nafiseh Mehdipour
Hi,

I am writing regarding to my problem about using plot function. I am using R 
x64 3.1.1 (installed on Windows 7 SP1 (laptop Sony VGN-SR) ), and when I use 
type this code:


 x - c(-30,30)
 y - c(-5,5)
 plot.window(x , y)

or any other form of plot function, it doesn't show anything. I used

 dev.off()

to set the graphic device to the default of R or null, but it didn't help. Then 
I used:

 png(filename=name.png)
 plot.window(x , y)
 dev.off()

to see the output as an image instead. But this is time consuming for me to do 
that every time to see the output. Please help me to resolve this problem.

Very looking forward to your reply.
Thank you.

Regards,
Nafiseh.



[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


[R] Help - plot function in Windows

2015-01-16 Thread Nafise Mehdipour
​Hi,

I am writing regarding to my problem about using plot function. I am
using R x64 3.1.1 (installed on Windows 7 SP1 (laptop Sony VGN-SR) ), and
when I use type this code:


 x - c(-30,30)
 y - c(-5,5)
 plot.window(x , y)

or any other form of plot function, it doesn't show anything. I used

 dev.off()

to set the graphic device to the default of R or null, but it didn't help. Then
I used:

 png(filename=name.png)
 plot.window(x , y)
 dev.off()

to see the output as an image instead. But this is time consuming for me to
do that every time to see the output. Please help me to resolve this
problem.

Very looking forward to your reply.
Thank you.


Best Regards,
Nafiseh Mehdipour.

[[alternative HTML version deleted]]

__
R-help@r-project.org 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] Help - plot function in Windows

2015-01-16 Thread Richard M. Heiberger
plot.window() is an internal support function, not a user function.
use
plot()

x - c(-30,30)
y - c(-5,5)
plot(x , y)

On Fri, Jan 16, 2015 at 4:18 PM, Nafise Mehdipour
nfs.mehdip...@gmail.com wrote:
 Hi,

 I am writing regarding to my problem about using plot function. I am
 using R x64 3.1.1 (installed on Windows 7 SP1 (laptop Sony VGN-SR) ), and
 when I use type this code:


 x - c(-30,30)
 y - c(-5,5)
 plot.window(x , y)

 or any other form of plot function, it doesn't show anything. I used

 dev.off()

 to set the graphic device to the default of R or null, but it didn't help. 
 Then
 I used:

 png(filename=name.png)
 plot.window(x , y)
 dev.off()

 to see the output as an image instead. But this is time consuming for me to
 do that every time to see the output. Please help me to resolve this
 problem.

 Very looking forward to your reply.
 Thank you.


 Best Regards,
 Nafiseh Mehdipour.

 [[alternative HTML version deleted]]

 __
 R-help@r-project.org 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.

__
R-help@r-project.org 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] Help - plot function in Windows

2015-01-16 Thread Duncan Murdoch
On 16/01/2015 1:18 PM, Nafiseh Mehdipour wrote:
 Hi,
 
 I am writing regarding to my problem about using plot function. I am using 
 R x64 3.1.1 (installed on Windows 7 SP1 (laptop Sony VGN-SR) ), and when I 
 use type this code:
 
 
 x - c(-30,30)
 y - c(-5,5)
 plot.window(x , y)
 
 or any other form of plot function, it doesn't show anything. I used

What are you expecting to see?  As the help says, plot.window() doesn't
plot anything, it just sets up the coordinates.  Use plot(x, y) to plot
the two points in your vectors.

Duncan Murdoch

 
 dev.off()
 
 to set the graphic device to the default of R or null, but it didn't help. 
 Then I used:
 
 png(filename=name.png)
 plot.window(x , y)
 dev.off()
 
 to see the output as an image instead. But this is time consuming for me to 
 do that every time to see the output. Please help me to resolve this problem.
 
 Very looking forward to your reply.
 Thank you.
 
 Regards,
 Nafiseh.
 
 
 
   [[alternative HTML version deleted]]
 
 __
 R-help@r-project.org 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.


__
R-help@r-project.org 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] help plsr function

2014-06-24 Thread Bjørn-Helge Mevik
annie Zhang annie.zhang2...@gmail.com writes:

 ## the predicted scores from the model
 (pred - predict(data.cpls,n.comp=1:2,newdata=x.new,type=score))
 ## the predicted scores using x%*%projection
 cbind(x.new.centered%*%data.cpls$projection[,1],x.new.centered%*%data.cpls$projection[,2])

 Can someone please tell me why the two predicted scores don't match?

If you look at the code that does the prediction:

 pls:::predict.mvr
function (object, newdata, ncomp = 1:object$ncomp, comps, type = c(response, 
scores), na.action = na.pass, ...) 
{
[...]
   TT - (newX - rep(object$Xmeans, each = nobs)) %*% 
object$projection[, comps]

you will see that it subtracts the _old X_ coloumn means from the new X
matrix, not the _new X_ coloumn means.  So

sweep(x.new, 2, data.cpls$Xmeans, -) %*% data.cpls$projection[,1:2]

will reproduce the values from predict().

-- 
Regards,
Bjørn-Helge Mevik

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


Re: [R] help plsr function

2014-06-24 Thread cindy Guo
Ok, yes, I can match them now. Thank you very much!



On Tue, Jun 24, 2014 at 3:58 AM, Bjørn-Helge Mevik b.h.me...@usit.uio.no
wrote:

 annie Zhang annie.zhang2...@gmail.com writes:

  ## the predicted scores from the model
  (pred - predict(data.cpls,n.comp=1:2,newdata=x.new,type=score))
  ## the predicted scores using x%*%projection
 
 cbind(x.new.centered%*%data.cpls$projection[,1],x.new.centered%*%data.cpls$projection[,2])
 
  Can someone please tell me why the two predicted scores don't match?

 If you look at the code that does the prediction:

  pls:::predict.mvr
 function (object, newdata, ncomp = 1:object$ncomp, comps, type =
 c(response,
 scores), na.action = na.pass, ...)
 {
 [...]
TT - (newX - rep(object$Xmeans, each = nobs)) %*%
 object$projection[, comps]

 you will see that it subtracts the _old X_ coloumn means from the new X
 matrix, not the _new X_ coloumn means.  So

 sweep(x.new, 2, data.cpls$Xmeans, -) %*% data.cpls$projection[,1:2]

 will reproduce the values from predict().

 --
 Regards,
 Bjørn-Helge Mevik

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


[[alternative HTML version deleted]]

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


[R] help plsr function

2014-06-23 Thread annie Zhang
Hi All,

I want to produce scores from X using $projection. When I predict, I cannot
match the predicted scores and scores using x%*%projection.

Below is a very simple example,

set.seed(seed=1)
y - c(1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0)
x - matrix(runif(200),nrow=20)
data - data.frame(cbind(y,x))
colnames(data) -
c(out,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10)
data.cpls - plsr(out~x1+x2+x3+x4+x5+x6+x7+x8+x9+x10,2,data=data)

x.new - matrix(runif(50),nrow=5)
x.new.centered - x.new
for (i in 1:ncol(x.new)) {
 x.mean - mean(x.new[,i])
 x.new.centered[,i] - (x.new[,i]-x.mean)
}
## the predicted scores from the model
(pred - predict(data.cpls,n.comp=1:2,newdata=x.new,type=score))
## the predicted scores using x%*%projection
cbind(x.new.centered%*%data.cpls$projection[,1],x.new.centered%*%data.cpls$projection[,2])

Can someone please tell me why the two predicted scores don't match?

Thanks,
Annie

[[alternative HTML version deleted]]

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


Re: [R] Help on function please

2012-11-27 Thread Jean V Adams
Andras,

What do you want your code to do?  Give us a little explanation with your 
code.

When I try to run your code, I get
Error: could not find function genoud.
Either supply the code for the functions included in your code, or tell us 
what packages we need to run it.

Jean



Andras Farkas motyoc...@yahoo.com wrote on 11/26/2012 02:43:25 PM:
 
 Dear All,
  
 I could use a bit of help here, this function is hard to figure out 
 (for me at least) I have the following so far:
  
 PKindex-data.frame(Subject=c(1),time=c(1,2,3,4,6,10,12),conc=c(32,
 28,25,22,18,14,11))
 Dose-200
 Tinf -0.5
  
 defun- function(time, y, parms) { 
  dCpdt - -parms[kel] * y[1] 
  list(dCpdt) 
  } 
 modfun - function(time,kel, Vd) {  
  out - lsoda(((Dose/Tinf)*(1/(kel*Vd)))*(1-exp(-kel*time)),c
 (0,time),defun,parms=c(kel=kel,Vd=Vd),rtol=1e-3,atol=1e-5) 
  out[-1,2]
  }
 objfun - function(par) {
  out - modfun(PKindex$time, par[1], par[2])
  gift - which( PKindex$conc != 0 )
  sum((PKindex$conc[gift]-out[gift])^2)
  }
 gen-genoud
 
(objfun,nvars=2,max=FALSE,pop.size=30,max.generations=100,wait.generations=100,starting.value=c
 (0.7,
 60),BFGS=FALSE,print.level=0,boundary.enforcement=2,Domains=matrix(c
 (0.01,0.01,100,100),2,2),MemoryMatrix=TRUE)
  
 but get the following:
  Error in lsoda(((Dose/Tinf) * (1/(kel * Vd))) * (1 - exp(-kel * time)), 
 : 
   The number of derivatives returned by func() (1must equal the 
 length of the initial conditions vector (7)
  
 i figured that having the time parameter in the equation screws 
 things up, but do not now how to fix it bc I do not understand the 
 warning message.
  
  
 your help is greatly apreciated,
  
 Sincerely,
  
 Andras

[[alternative HTML version deleted]]

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


[R] Help on function please

2012-11-26 Thread Andras Farkas
Dear All,
 
I could use a bit of help here, this function is hard to figure out (for me at 
least) I have the following so far:
 
PKindex-data.frame(Subject=c(1),time=c(1,2,3,4,6,10,12),conc=c(32,28,25,22,18,14,11))
Dose-200
Tinf -0.5
 
defun- function(time, y, parms) { 
 dCpdt - -parms[kel] * y[1] 
 list(dCpdt) 
 } 
modfun - function(time,kel, Vd) {  
 out - 
lsoda(((Dose/Tinf)*(1/(kel*Vd)))*(1-exp(-kel*time)),c(0,time),defun,parms=c(kel=kel,Vd=Vd),rtol=1e-3,atol=1e-5)
 
 out[-1,2]
 }
objfun - function(par) {
 out - modfun(PKindex$time, par[1], par[2])
 gift - which( PKindex$conc != 0 )
 sum((PKindex$conc[gift]-out[gift])^2)
 }    
gen-genoud(objfun,nvars=2,max=FALSE,pop.size=30,max.generations=100,wait.generations=100,starting.value=c(0.7,60),BFGS=FALSE,print.level=0,boundary.enforcement=2,Domains=matrix(c(0.01,0.01,100,100),2,2),MemoryMatrix=TRUE)
 
but get the following:
 Error in lsoda(((Dose/Tinf) * (1/(kel * Vd))) * (1 - exp(-kel * time)),  : 
  The number of derivatives returned by func() (1must equal the length of the 
initial conditions vector (7)
 
i figured that having the time parameter in the equation screws things up, 
but do not now how to fix it bc I do not understand the warning message.
 
 
your help is greatly apreciated,
 
Sincerely,
 
Andras
[[alternative HTML version deleted]]

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


[R] Help writing function in R....

2012-05-13 Thread Tabby
I need help writing function that takes three categorical inputs and returns
a vector of summary statistics based on these inputs. The data set contains
information on retail goods that can be specified by their retail segment,
brand name, and type of good along with its retail price and what it
actually sold for. I need to write a function that will take these inputs
and average, count, and calculate whatever else is needed.

I have set the function up as follows (using made up data):

dataold=data.frame(segment=c(golf,tenis,football,tenis,golf,golf),
brand=c(x,y,z,y,x,a),type=c(iron,ball,helmet,shoe,driver,iron),
retail=c(124,.60,80,75,150,108),actual=c(112,.60,72,75,135,100))

retailsum=funtion(segment,brand,type){

datanew=dataold[which(dataoldsegment='segment'  dataoldbrand='brand' 
dataold$type='type'),c(retail,actaul)]

summary=c(dim(datanew)[1],colMeans(datanew)) return(summary) }

The code inside the function braces works on its own, but once I wrap a
function around it I start getting errors or it will just return 0 counts
and NaN for the means.

Any help would be greatly appreciated. I have very little experience in r,
so I apologize if this is a trivial question, but I have not been able to
find a solution.

Thank you, D


--
View this message in context: 
http://r.789695.n4.nabble.com/Help-writing-function-in-R-tp4629824.html
Sent from the R help mailing list archive at Nabble.com.

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


Re: [R] Help writing function in R....

2012-05-13 Thread Jeff Newmiller
This looks like homework. Please read the Posting Guide mentioned at the end of 
every posting regarding homework.
---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  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.

Tabby dj.lan...@gmail.com wrote:

I need help writing function that takes three categorical inputs and
returns
a vector of summary statistics based on these inputs. The data set
contains
information on retail goods that can be specified by their retail
segment,
brand name, and type of good along with its retail price and what it
actually sold for. I need to write a function that will take these
inputs
and average, count, and calculate whatever else is needed.

I have set the function up as follows (using made up data):

dataold=data.frame(segment=c(golf,tenis,football,tenis,golf,golf),
brand=c(x,y,z,y,x,a),type=c(iron,ball,helmet,shoe,driver,iron),
retail=c(124,.60,80,75,150,108),actual=c(112,.60,72,75,135,100))

retailsum=funtion(segment,brand,type){

datanew=dataold[which(dataoldsegment='segment'  dataoldbrand='brand' 
dataold$type='type'),c(retail,actaul)]

summary=c(dim(datanew)[1],colMeans(datanew)) return(summary) }

The code inside the function braces works on its own, but once I wrap a
function around it I start getting errors or it will just return 0
counts
and NaN for the means.

Any help would be greatly appreciated. I have very little experience in
r,
so I apologize if this is a trivial question, but I have not been able
to
find a solution.

Thank you, D


--
View this message in context:
http://r.789695.n4.nabble.com/Help-writing-function-in-R-tp4629824.html
Sent from the R help mailing list archive at Nabble.com.

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

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


[R] r help: source-function is very slow

2012-01-19 Thread Jos van Nijnatten
Hello everybody,
I noticed a performance problem when using the source-function in R. 
When I try to source an r script that is located in the same directory 
as the script I execute via command line ('R -f file.r' contents 
'source(someOtherScript.r)') it's very fast. But if I put 
'someOtherScript.r' in another directory (say ../../R) R takes moments, 
if not minutes to locate and load the script. Is there another way to do 
this that is not slow?
Thanks, Jos

-- 
Jos van Nijnatten, BSc Candidate
Erasmus MC, Rotterdam, NL
Department of Bioinformatics
(ph) +31 1 07 04 45 51
(mob) +31 6 14 92 14 01
(e) j.vannijnat...@erasmusmc.nl

[[alternative HTML version deleted]]

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


Re: [R] r help: source-function is very slow

2012-01-19 Thread Uwe Ligges



On 19.01.2012 14:25, Jos van Nijnatten wrote:

Hello everybody,
I noticed a performance problem when using the source-function in R.
When I try to source an r script that is located in the same directory
as the script I execute via command line ('R -f file.r' contents
'source(someOtherScript.r)') it's very fast. But if I put
'someOtherScript.r' in another directory (say ../../R) R takes moments,
if not minutes to locate and load the script. Is there another way to do
this that is not slow?


We are certainly interested in a reproducible example. Or could it be 
that your ../../R is on some other possibly network mounted file system?


Uwe Ligges





Thanks, Jos



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


Re: [R] r help: source-function is very slow

2012-01-19 Thread Duncan Murdoch

On 12-01-19 8:25 AM, Jos van Nijnatten wrote:

Hello everybody,
I noticed a performance problem when using the source-function in R.
When I try to source an r script that is located in the same directory
as the script I execute via command line ('R -f file.r' contents
'source(someOtherScript.r)') it's very fast. But if I put
'someOtherScript.r' in another directory (say ../../R) R takes moments,
if not minutes to locate and load the script. Is there another way to do
this that is not slow?


R uses the same method in both instances.  If there's a difference in 
timing, it's almost certainly because there's something going on with 
your OS or file system.


Duncan Murdoch

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


Re: [R] r help: source-function is very slow

2012-01-19 Thread Jos van Nijnatten

Hello Uwe, Duncan and the rest of the community,
It indeed seemed to be a file system problem and I'm lucky to figure 
that out before it crashed. New computer's great and everything is 
working fine again.

Sorry for wasting your time ;-)


On Thursday, January 19, 2012 15:16, Duncan Murdoch wrote:

On 12-01-19 8:25 AM, Jos van Nijnatten wrote:

Hello everybody,
I noticed a performance problem when using the source-function in R.
When I try to source an r script that is located in the same directory
as the script I execute via command line ('R -f file.r' contents
'source(someOtherScript.r)') it's very fast. But if I put
'someOtherScript.r' in another directory (say ../../R) R takes moments,
if not minutes to locate and load the script. Is there another way to do
this that is not slow?


R uses the same method in both instances. If there's a difference in 
timing, it's almost certainly because there's something going on with 
your OS or file system.


Duncan Murdoch




--
Jos van Nijnatten, BSc Candidate
Erasmus MC, Rotterdam, NL
Department of Bioinformatics
(ph) +31 1 07 04 45 51
(mob) +31 6 14 92 14 01
(e) j.vannijnat...@erasmusmc.nl

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


[R] Help ave() function

2011-04-28 Thread петрович
Hello again.

Following a great idea from Jim and Dennis, I began to use the function
ave() in my big data.
I proved to use this command in my data with FUN=max(x,na.rm=T), and I
received a Warning that translated looks like

Warning in max (x, na.rm = T):
no arguments to max; returning -Inf

Is this happening 'cause I´m using a big table : 254417 rows   9 columns?

[[alternative HTML version deleted]]

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


Re: [R] Help ave() function

2011-04-28 Thread David Winsemius


On Apr 28, 2011, at 12:30 PM, петрович wrote:


Hello again.

Following a great idea from Jim and Dennis, I began to use the  
function

ave() in my big data.
I proved to use this command in my data with FUN=max(x,na.rm=T),


Try instead:

 look at the 3rd example of help(ave)



and I
received a Warning that translated looks like

Warning in max (x, na.rm = T):
no arguments to max; returning -Inf

Is this happening 'cause I´m using a big table : 254417 rows   9  
columns?


[[alternative HTML version deleted]]


 ... and it is trivial to send plain text from gmail, so please do so.

David Winsemius, MD
West Hartford, CT

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


Re: [R] help with function

2010-12-18 Thread Iain Gallagher
Ok - used browser to step through the function. Thanks for the nod towards that 
Chuck.

 tf2 - cumulMetric(tf1, deMirs$up)
Called from: cumulMetric(tf1, deMirs$up)
Browse[1] fcVector - as.numeric(with (deMirs, FC[match(deMirPresGenes[,4], 
Probe)] ) )

Browse[1] metric - fcVector * as.numeric(deMirPresGenes[,11])

Browse[1] geneMetric - cbind(deMirPresGenes[,2], metric) 

Browse[1] ls()
[1] deMirPresGenes deMirs fcVector   geneMetric
[5] metric   

Browse[1] listMetric - unstack(geneMetric, 
as.numeric(geneMetric[,2])~geneMetric[,1])
Error in eval(expr, envir, enclos) : object 'geneMetric' not found

Browse[1] ls()
[1] deMirPresGenes deMirs fcVector   geneMetric
[5] metric   

Browse[1] head(geneMetric)
 sym  metric
[1,] AAK1   -0.35505
[2,] ABCA1  -0.34979
[3,] ABCA2  -1.0329 
[4,] ABCB10 -1.22558
[5,] ABCE1  -0.61348
[6,] ABCF3  -0.86584


So geneMetric is there. It looks right but for some reason the call to unstack 
cannot find it. Yet if I go through this line by line but not as a function the 
call to unstack works fine:

 fcVector - as.numeric(with (deMirs$up, FC[match(tf1[,4], Probe)] ) )
 metric - fcVector * as.numeric(tf1[,11])
  geneMetric - cbind(tf1[,2], metric)
 head(geneMetric)
  metric
[1,] AAK1   -0.35505
[2,] ABCA1  -0.34979
[3,] ABCA2  -1.0329 
[4,] ABCB10 -1.22558
[5,] ABCE1  -0.61348
[6,] ABCF3  -0.86584

 colnames(geneMetric) - c('sym', 'metric')

 listMetric - unstack(geneMetric, as.numeric(geneMetric[,2])~geneMetric[,1])
 head(listMetric)
$AAK1
[1] -0.35505

$ABCA1
[1] -0.34979

$ABCA2
[1] -1.0329

$ABCB10
[1] -1.22558

$ABCE1
[1] -0.61348

$ABCF3
[1] -0.86584

Any further advice would be much appreciated.

Thanks

i

 sessionInfo()
R version 2.12.0 (2010-10-15)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_GB.utf8   LC_NUMERIC=C 
 [3] LC_TIME=en_GB.utf8LC_COLLATE=en_GB.utf8
 [5] LC_MONETARY=C LC_MESSAGES=en_GB.utf8   
 [7] LC_PAPER=en_GB.utf8   LC_NAME=C
 [9] LC_ADDRESS=C  LC_TELEPHONE=C   
[11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C  

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base 
 


--- On Sat, 18/12/10, Charles C. Berry cbe...@tajo.ucsd.edu wrote:

 From: Charles C. Berry cbe...@tajo.ucsd.edu
 Subject: Re: [R] help with function
 To: Iain Gallagher iaingallag...@btopenworld.com
 Cc: r-help@r-project.org
 Date: Saturday, 18 December, 2010, 0:13
 On Fri, 17 Dec 2010, Iain Gallagher
 wrote:
 
  Hello List
 
  I'm moving this over from the bioC list as, although
 the problem I'm working on is biological, the current bottle
 neck is my poor understanding of R.
 
  I wonder if someone would help me with the following
 function.
 
 
 Here is how I'd take it apart.
 
 Either
 
   1) put browser() as the first line of the
 function,then feed lines to the
      browser one -by-one to see where
 it hangs,
 
   2) trace(cumulMetric) , then try to run it (much
 like 1, but it will
      handle feeding the lines of the
 function more easily)
 
   3) options( error = recover ), then run it till it
 hits the error, then
      browser thru the frames to see
 what is where
 
 See
 
      ?browser
      ?trace
      ?recover
 
 as background.
 
 HTH,
 
 Chuck
 
  cumulMetric - function(deMirPresGenes, deMirs){
     
  #need to match position of each miR in deMirPresGenes
 with its FC to form a vector of FC in correct order
      fc - deMirs
      fcVector - as.numeric(with (fc,
 FC[match(deMirPresGenes[,4], Probe)] ) )
 
      #multiply fc by context score for each
 interaction
      metric - fcVector *
 as.numeric(deMirPresGenes[,11])
      geneMetric - cbind(deMirPresGenes[,2],
 as.numeric(metric))
 
      #make cumul weighted score
      listMetric - unstack(geneMetric,
 as.numeric(geneMetric[,2])~geneMetric[,1])
      listMetric -
 as.data.frame(sapply(listMetric,sum)) #returns a dataframe
      colnames(listMetric) - c('cumulMetric')
 
      #return whole list
      return(listMetric)
  }
 
  deMirPresGenes looks like this:
 
  Gene.ID    Gene.Symbol    Species.ID    miRNA 
   Site.type    UTR_start    UTR_end   
 X3pairing_contr    local_AU_contr    position_contr   
 context_score    context_percentile
  22848    AAK1    9606    hsa-miR-183    2   
 1546    1552    -0.026    -0.047    0.099   
 -0.135    47
  19    ABCA1    9606    hsa-miR-183    2   
 1366    1372    -0.011    -0.048    0.087   
 -0.133    46
  20    ABCA2    9606    hsa-miR-495    2   
 666    672    -0.042    -0.092    -0.035   
 -0.33    93
  23456    ABCB10    9606    hsa-miR-183    3 
   1475    1481    0.003    -0.109    -0.05   
 -0.466    98
  6059    ABCE1    9606    hsa-miR-495    2   
 1474    1480    0.005    -0.046    0.006   
 -0.196    58
  55324    ABCF3    9606    hsa-miR-1275    3 
   90    96    0.007    0.042    -0.055   
 -0.316    94
 
  although it is much longer in 'real

[R] help with function

2010-12-17 Thread Iain Gallagher
Hello List

I'm moving this over from the bioC list as, although the problem I'm working on 
is biological, the current bottle neck is my poor understanding of R. 

I wonder if someone would help me with the following function.

cumulMetric - function(deMirPresGenes, deMirs){
   
#need to match position of each miR in deMirPresGenes with its FC to form a 
vector of FC in correct order
    fc - deMirs
    fcVector - as.numeric(with (fc, FC[match(deMirPresGenes[,4], Probe)] ) )

    #multiply fc by context score for each interaction
    metric - fcVector * as.numeric(deMirPresGenes[,11])
    geneMetric - cbind(deMirPresGenes[,2], as.numeric(metric))

    #make cumul weighted score
    listMetric - unstack(geneMetric, as.numeric(geneMetric[,2])~geneMetric[,1])
    listMetric - as.data.frame(sapply(listMetric,sum)) #returns a dataframe
    colnames(listMetric) - c('cumulMetric')

    #return whole list
    return(listMetric)
}

deMirPresGenes looks like this:

Gene.ID    Gene.Symbol    Species.ID    miRNA    Site.type    UTR_start    
UTR_end    X3pairing_contr    local_AU_contr    position_contr    
context_score    context_percentile
22848    AAK1    9606    hsa-miR-183    2    1546    1552    -0.026    -0.047   
 0.099    -0.135    47
19    ABCA1    9606    hsa-miR-183    2    1366    1372    -0.011    -0.048    
0.087    -0.133    46
20    ABCA2    9606    hsa-miR-495    2    666    672    -0.042    -0.092    
-0.035    -0.33    93
23456    ABCB10    9606    hsa-miR-183    3    1475    1481    0.003    -0.109  
  -0.05    -0.466    98
6059    ABCE1    9606    hsa-miR-495    2    1474    1480    0.005    -0.046    
0.006    -0.196    58
55324    ABCF3    9606    hsa-miR-1275    3    90    96    0.007    0.042    
-0.055    -0.316    94

although it is much longer in 'real life'.

The aim of the function is to extract a dataframe of gene symbols along with a 
weighted score from the above data. The weighted score is the FC column of 
deMirs * the context_score column of deMirPresGenes. This is easy peasy!

Where I'm falling down is that if I run this function it complains that 
'geneMetric' can't be found. Hmm - I've run it all line by line (i.e. not as a 
function) and it works but wrapped up like this it fails!

e.g.

testF2 - cumulMetric(testF1, deMirs$up)
Error in eval(expr, envir, enclos) : object 'geneMetric' not found

deMirs$up looks like this:

Probe    FC
hsa-miR-183    2.63
hsa-miR-1275    2.74
hsa-miR-495    3.13
hsa-miR-886-3p    3.73
hsa-miR-886-5p    3.97
hsa-miR-144*    6.62
hsa-miR-451    7.94

In an effort to debug this I have examined each object using 'print' statements 
(as suggested by cstrato on the bioC list).

All the objects in the function up until listMetric look ok in terms of 
structure and length. But the error persists and listMetric is not made?!?! Odd.

I have added some comments to the output below.

 tf2-cumulMetric(tf1, deMirs$up)#deMirs$up is a dataframe (see above - it is 
 the same as deMirs)


[1] 2.63 2.63 3.13 2.63 3.13 2.74 # print fcVector - looks ok
[1] -0.35505 -0.34979 -1.03290 -1.22558 -0.61348 -0.86584 # print metric - 
looks ok
[1] 1045 # length of metric - is correct
     sym      metric    # print geneMetric - looks ok
[1,] AAK1   -0.35505
[2,] ABCA1  -0.34979
[3,] ABCA2  -1.0329
[4,] ABCB10 -1.22558
[5,] ABCE1  -0.61348
[6,] ABCF3  -0.86584
[1] 1045 # nrow of geneMetric - is correct
Error in eval(expr, envir, enclos) : object 'geneMetric' not found


Could someone possibly point out where I falling down.

Thanks

iain

 sessionInfo()
R version 2.12.0 (2010-10-15)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_GB.utf8       LC_NUMERIC=C             
[3] LC_TIME=en_GB.utf8        LC_COLLATE=en_GB.utf8   
[5] LC_MONETARY=C             LC_MESSAGES=en_GB.utf8   
[7] LC_PAPER=en_GB.utf8       LC_NAME=C               
[9] LC_ADDRESS=C              LC_TELEPHONE=C           
[11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C     

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] tools_2.12.0




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


Re: [R] help with function

2010-12-17 Thread Charles C. Berry

On Fri, 17 Dec 2010, Iain Gallagher wrote:


Hello List

I'm moving this over from the bioC list as, although the problem I'm working on 
is biological, the current bottle neck is my poor understanding of R.

I wonder if someone would help me with the following function.



Here is how I'd take it apart.

Either

 1) put browser() as the first line of the function,then feed lines to the
browser one -by-one to see where it hangs,

 2) trace(cumulMetric) , then try to run it (much like 1, but it will
handle feeding the lines of the function more easily)

 3) options( error = recover ), then run it till it hits the error, then
browser thru the frames to see what is where

See

?browser
?trace
?recover

as background.

HTH,

Chuck


cumulMetric - function(deMirPresGenes, deMirs){
   
#need to match position of each miR in deMirPresGenes with its FC to form a 
vector of FC in correct order
    fc - deMirs
    fcVector - as.numeric(with (fc, FC[match(deMirPresGenes[,4], Probe)] ) )

    #multiply fc by context score for each interaction
    metric - fcVector * as.numeric(deMirPresGenes[,11])
    geneMetric - cbind(deMirPresGenes[,2], as.numeric(metric))

    #make cumul weighted score
    listMetric - unstack(geneMetric, as.numeric(geneMetric[,2])~geneMetric[,1])
    listMetric - as.data.frame(sapply(listMetric,sum)) #returns a dataframe
    colnames(listMetric) - c('cumulMetric')

    #return whole list
    return(listMetric)
}

deMirPresGenes looks like this:

Gene.ID    Gene.Symbol    Species.ID    miRNA    Site.type    UTR_start    
UTR_end    X3pairing_contr    local_AU_contr    position_contr    
context_score    context_percentile
22848    AAK1    9606    hsa-miR-183    2    1546    1552    -0.026    -0.047   
 0.099    -0.135    47
19    ABCA1    9606    hsa-miR-183    2    1366    1372    -0.011    -0.048    
0.087    -0.133    46
20    ABCA2    9606    hsa-miR-495    2    666    672    -0.042    -0.092    
-0.035    -0.33    93
23456    ABCB10    9606    hsa-miR-183    3    1475    1481    0.003    -0.109  
  -0.05    -0.466    98
6059    ABCE1    9606    hsa-miR-495    2    1474    1480    0.005    -0.046    
0.006    -0.196    58
55324    ABCF3    9606    hsa-miR-1275    3    90    96    0.007    0.042    
-0.055    -0.316    94

although it is much longer in 'real life'.

The aim of the function is to extract a dataframe of gene symbols along with a 
weighted score from the above data. The weighted score is the FC column of 
deMirs * the context_score column of deMirPresGenes. This is easy peasy!

Where I'm falling down is that if I run this function it complains that 
'geneMetric' can't be found. Hmm - I've run it all line by line (i.e. not as a 
function) and it works but wrapped up like this it fails!

e.g.


testF2 - cumulMetric(testF1, deMirs$up)

Error in eval(expr, envir, enclos) : object 'geneMetric' not found

deMirs$up looks like this:

Probe    FC
hsa-miR-183    2.63
hsa-miR-1275    2.74
hsa-miR-495    3.13
hsa-miR-886-3p    3.73
hsa-miR-886-5p    3.97
hsa-miR-144*    6.62
hsa-miR-451    7.94

In an effort to debug this I have examined each object using 'print' statements 
(as suggested by cstrato on the bioC list).

All the objects in the function up until listMetric look ok in terms of 
structure and length. But the error persists and listMetric is not made?!?! Odd.

I have added some comments to the output below.


tf2-cumulMetric(tf1, deMirs$up)#deMirs$up is a dataframe (see above - it is 
the same as deMirs)



[1] 2.63 2.63 3.13 2.63 3.13 2.74 # print fcVector - looks ok
[1] -0.35505 -0.34979 -1.03290 -1.22558 -0.61348 -0.86584 # print metric - 
looks ok
[1] 1045 # length of metric - is correct
     sym      metric    # print geneMetric - looks ok
[1,] AAK1   -0.35505
[2,] ABCA1  -0.34979
[3,] ABCA2  -1.0329
[4,] ABCB10 -1.22558
[5,] ABCE1  -0.61348
[6,] ABCF3  -0.86584
[1] 1045 # nrow of geneMetric - is correct
Error in eval(expr, envir, enclos) : object 'geneMetric' not found




Could someone possibly point out where I falling down.

Thanks

iain


sessionInfo()

R version 2.12.0 (2010-10-15)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_GB.utf8       LC_NUMERIC=C             
[3] LC_TIME=en_GB.utf8        LC_COLLATE=en_GB.utf8   
[5] LC_MONETARY=C             LC_MESSAGES=en_GB.utf8   
[7] LC_PAPER=en_GB.utf8       LC_NAME=C               
[9] LC_ADDRESS=C              LC_TELEPHONE=C           
[11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C     

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] tools_2.12.0






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



Charles C. Berry

[R] help using function merge_all()

2010-11-16 Thread arjun

Hi,
   I want to merge 4 data frames with one column in common but I am
getting error message while using this function. Can any one help me out. 
 merge_all(Br,Ki,Lu,Pr,by=Genes)
Error: could not find function merge_all

I have installed the package: reshape but I still get this error 
-- 
View this message in context: 
http://r.789695.n4.nabble.com/help-using-function-merge-all-tp3045173p3045173.html
Sent from the R help mailing list archive at Nabble.com.

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


Re: [R] help using function merge_all()

2010-11-16 Thread Joshua Wiley
Hi,

Did you also load the package (you'll need to load it every session
you want to use it)?

## load reshape package
library(reshape)
## now try
merge_all(Br, Ki, Lu, Pr, by=Genes)


If this does not resolve your problem, please run:   sessionInfo()
at your console and report the output (it will tell us information
about your version of R, loaded packages, etc.).

Cheers,

Josh

On Tue, Nov 16, 2010 at 8:33 AM, arjun gop...@mail.gvsu.edu wrote:

 Hi,
       I want to merge 4 data frames with one column in common but I am
 getting error message while using this function. Can any one help me out.
 merge_all(Br,Ki,Lu,Pr,by=Genes)
 Error: could not find function merge_all

 I have installed the package: reshape but I still get this error
 --
 View this message in context: 
 http://r.789695.n4.nabble.com/help-using-function-merge-all-tp3045173p3045173.html
 Sent from the R help mailing list archive at Nabble.com.

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




-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

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


Re: [R] help using function merge_all()

2010-11-16 Thread arjun

Hi Josh,
  Thanks for your reply.

  While I am loading the package, it says package plyr is
required and I tried to install plyr package but I am unable to do so
because it is giving an error message as shown below.

Please help me.




library(reshape)
Loading required package: plyr
Error: package 'plyr' could not be loaded
In addition: Warning message:
In library(pkg, character.only = TRUE, logical.return = TRUE, lib.loc =
lib.loc) :
  there is no package called 'plyr'

 utils:::menuInstallPkgs()
trying URL 'http://cran.mtu.edu/bin/windows/contrib/2.11/plyr_1.1.zip'
Error in download.file(url, destfile, method, mode = wb, ...) : 
  cannot open URL
'http://cran.mtu.edu/bin/windows/contrib/2.11/plyr_1.1.zip'
In addition: Warning message:
In download.file(url, destfile, method, mode = wb, ...) :
  cannot open: HTTP status was '404 Not Found'
Warning in download.packages(pkgs, destdir = tmpd, available = available,  :
  download of package 'plyr' failed

-- 
View this message in context: 
http://r.789695.n4.nabble.com/help-using-function-merge-all-tp3045173p3045537.html
Sent from the R help mailing list archive at Nabble.com.

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


Re: [R] help using function merge_all()

2010-11-16 Thread arjun

Hi Josh,
 I thought of giving up and started writing code in excel using
VBA but then I saw ur message and gave a try in R. I got it, the location
from which I am calling the package plyr is not working out so called from
different CRANmirror location and then I was able load the package. now the
merge_all function is working fine. Thanks for your help.  
-- 
View this message in context: 
http://r.789695.n4.nabble.com/help-using-function-merge-all-tp3045173p3045561.html
Sent from the R help mailing list archive at Nabble.com.

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


[R] Help with function writing

2010-10-11 Thread Tim Elwell-Sutton
Hello all
I have what seems like a simple question but have not been able to find an
answer on the forum. I'm trying to define a function which involves
regression models and a large number of covariates. 

I would like the function to accept any number of covariates and, ideally, I
would like to be able to enter the covariates in a group (e.g. as a list)
rather than individually. Is there any way of doing this? 

Example:

#define function involving regression model with several covariates 
custom - function(outcome, exposure, covar1, covar2, covar3){
  model - lm(outcome ~ exposure + covar1 +  covar2 + covar3)
  expected - predict(model)
  summary(expected)
}

library(MASS)
attach(birthwt)

custom(bwt, lwt, low, age, race) #Works when 3 covariates are specified

custom(bwt,lwt,low,age) # Does not work with  or  3 covariates

varlist - list(low,age,race)
custom(bwt,lwt, varlist) #Does not work if covariates are included as a list



Thanks very much for your help

Tim

--
Tim Elwell-Sutton
University of Hong Kong

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


Re: [R] Help with function writing

2010-10-11 Thread Michael Bedward
Hello Tim,

This function will do it where the covariates are provided as separate
arguments. It would be easy to modify this to handle a list too.

function(outcome, ...) {
  arg.names - as.character(match.call())[-1]
  nargs - length(arg.names)
  f - as.formula(paste(arg.names[1], ~, paste(arg.names[2:nargs],
collapse=+)))

  model - lm(f)

  # rest of your code here
}

Hope that helps you get started.

Michael


On 12 October 2010 14:35, Tim Elwell-Sutton tesut...@hku.hk wrote:
 Hello all
 I have what seems like a simple question but have not been able to find an
 answer on the forum. I'm trying to define a function which involves
 regression models and a large number of covariates.

 I would like the function to accept any number of covariates and, ideally, I
 would like to be able to enter the covariates in a group (e.g. as a list)
 rather than individually. Is there any way of doing this?

 Example:

 #define function involving regression model with several covariates
 custom - function(outcome, exposure, covar1, covar2, covar3){
  model - lm(outcome ~ exposure + covar1 +  covar2 + covar3)
  expected - predict(model)
  summary(expected)
 }

 library(MASS)
 attach(birthwt)

 custom(bwt, lwt, low, age, race) #Works when 3 covariates are specified

 custom(bwt,lwt,low,age) # Does not work with  or  3 covariates

 varlist - list(low,age,race)
 custom(bwt,lwt, varlist) #Does not work if covariates are included as a list



 Thanks very much for your help

 Tim

 --
 Tim Elwell-Sutton
 University of Hong Kong

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


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


[R] help in function in R akin to macro in SAS

2010-04-06 Thread Changbin Du
Dear Whom it may concern,

I need help  to figure the macro function in R:  I need to plot the
different data sets by a plotxyf function, I want the title to be different
for different data set.


# get the data set
final.xyf- xyf(data=as.matrix(my.final),
Y=classvec2classmat(final$outcome), xweight = 0.5, grid=somgrid(5, 4,
hexagonal))


#plot function
plotxyf-function (data) {

plot(data, type=mapping, labels=final$target, col=final$outcome+1,,
main=Supervised: Mapping plot for data)

  }


#use the plotxyf function.
plotxyf(final.xyf)


But the result is not I expected, the title is Supervised: Mapping plot for
data, not Supervised: Mapping plot for final.xyf

CAn anyone help me with this?

Thanks so much!

-- 
Sincerely,
Changbin
--

[[alternative HTML version deleted]]

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


Re: [R] help in function in R akin to macro in SAS

2010-04-06 Thread Stuart Luppescu
On Tue, 2010-04-06 at 09:33 -0700, Changbin Du wrote:
 #plot function
 plotxyf-function (data) {
 
 plot(data, type=mapping, labels=final$target, col=final$outcome+1,,
 main=Supervised: Mapping plot for data)

Change this to: main=paste(Supervised: Mapping plot for, data)

 
   }
 
 
 #use the plotxyf function.
 plotxyf(final.xyf)

BTW, I think it's better not to use data as a function parameter,
since it has reserved use in the language.

-- 
Stuart Luppescu s...@ccsr.uchicago.edu
University of Chicago

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


Re: [R] help in function in R akin to macro in SAS

2010-04-06 Thread Gang Liang
A small correction.

On Tue, Apr 6, 2010 at 9:43 AM, Stuart Luppescu s...@ccsr.uchicago.eduwrote:

 On Tue, 2010-04-06 at 09:33 -0700, Changbin Du wrote:
  #plot function
  plotxyf-function (data) {
 
  plot(data, type=mapping, labels=final$target, col=final$outcome+1,,
  main=Supervised: Mapping plot for data)

 Change this to: main=paste(Supervised: Mapping plot for, data)


You might want to introduce a new variable, such data_name, then use (data
field will expand here)

main=paste(Supervised: Mapping plot for, data_name)

Or get the argument name somehow inside the function...


}
 
 
  #use the plotxyf function.
  plotxyf(final.xyf)

 BTW, I think it's better not to use data as a function parameter,
 since it has reserved use in the language.

 --
 Stuart Luppescu s...@ccsr.uchicago.edu
 University of Chicago

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


[[alternative HTML version deleted]]

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


Re: [R] help in function in R akin to macro in SAS

2010-04-06 Thread Thomas Lumley

On Tue, 6 Apr 2010, Stuart Luppescu wrote:


On Tue, 2010-04-06 at 09:33 -0700, Changbin Du wrote:

#plot function
plotxyf-function (data) {

plot(data, type=mapping, labels=final$target, col=final$outcome+1,,
main=Supervised: Mapping plot for data)


Change this to: main=paste(Supervised: Mapping plot for, data)


Umm, no.  That will put the entire data set into the title

plotxyf-function(data){

data.name-deparse(substitute(data))
   plot(data, type=mapping, labels=final$target, col=final$outcome+1,
  main=paste(Supervised: Mapping plot for, data.name)

}

might work

   -thomas



  }


#use the plotxyf function.
plotxyf(final.xyf)


BTW, I think it's better not to use data as a function parameter,
since it has reserved use in the language.

--
Stuart Luppescu s...@ccsr.uchicago.edu
University of Chicago

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



Thomas Lumley   Assoc. Professor, Biostatistics
tlum...@u.washington.eduUniversity of Washington, Seattle

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


Re: [R] Help with function fitdistr in MASS

2010-01-04 Thread Saji Ren

Thank you,man. the problem solved.
Plus. when I got the parameters of the data.
And I used the truehist(mydata) to get a histogram of the data,
How can I draw a line of the distribution of the estimated parameters in the
histogram plot?

for example:
fitdistr(na.exclude(mydata),normal)
 mean sd
   4.052594   75.620350 
 ( 1.420743) ( 1.004617)
truehist(mydata)

After that, I got the histogram of mydata.
And I want plot a extra line of the density of a normal distribution of
mean=4.052594 and sd=75.620350 in the histogram plot?

thank you 



Bernardo Rangel tura wrote:
 
 On Sat, 2010-01-02 at 23:20 -0800, Saji Ren wrote:
 Hi, R users:
 
 I want to fit my data into a normal distribution by using the command
 fitdistr in MASS.
 I changed my data class from ts to numeric by
 
 class(mydata)=numeric
 
 but after using fitdistr, I got the result below
 
 fitdistr(mydata,normal)
   meansd 
   NA NA  
  (NA)   (NA) 
 
 the help doc of fitdistr does not mention anything about that, thus I
 need
 your help.
 
 Thank you in advanced,
 Saji from Shanghai
 
 Hi Sajj,
 
 You hava NA in your data
 
 try: fitdistr(na.exclude(mydata),normal)
 
 -- 
 Bernardo Rangel Tura, M.D,MPH,Ph.D
 National Institute of Cardiology
 Brazil
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.
 
 

-- 
View this message in context: 
http://n4.nabble.com/Help-with-function-fitdistr-in-MASS-tp997609p998258.html
Sent from the R help mailing list archive at Nabble.com.

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


Re: [R] Help with function fitdistr in MASS

2010-01-04 Thread Peter Ehlers



Saji Ren wrote:

Thank you,man. the problem solved.
Plus. when I got the parameters of the data.
And I used the truehist(mydata) to get a histogram of the data,
How can I draw a line of the distribution of the estimated parameters in the
histogram plot?

for example:

fitdistr(na.exclude(mydata),normal)
 mean sd
   4.052594   75.620350 
 ( 1.420743) ( 1.004617)

truehist(mydata)


After that, I got the histogram of mydata.
And I want plot a extra line of the density of a normal distribution of
mean=4.052594 and sd=75.620350 in the histogram plot?

add this:

curve(dnorm(x, mean=4.052594, sd=75.620350), add=TRUE)

 -Peter Ehlers



thank you 




Bernardo Rangel tura wrote:

On Sat, 2010-01-02 at 23:20 -0800, Saji Ren wrote:

Hi, R users:

I want to fit my data into a normal distribution by using the command
fitdistr in MASS.
I changed my data class from ts to numeric by


class(mydata)=numeric

but after using fitdistr, I got the result below


fitdistr(mydata,normal)
  meansd 
  NA NA  
 (NA)   (NA) 


the help doc of fitdistr does not mention anything about that, thus I
need
your help.

Thank you in advanced,
Saji from Shanghai

Hi Sajj,

You hava NA in your data

try: fitdistr(na.exclude(mydata),normal)

--
Bernardo Rangel Tura, M.D,MPH,Ph.D
National Institute of Cardiology
Brazil

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






--
Peter Ehlers
University of Calgary
403.202.3921

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


Re: [R] Help with function fitdistr in MASS

2010-01-03 Thread Prof Brian Ripley

Please read the footer of this message.  ?fitdistr says

   x: A numeric vector.

and setting the class does not make it a numeric vector (it is just a 
label).  And fitdistr early on does


if (missing(x) || length(x) == 0L || mode(x) != numeric)
stop('x' must be a non-empty numeric vector)

so you do have a non-empty numeric vector.

It is likely that your data contains NAs, in which case the quoted 
result is correct.  But despite the posting guide we have no details 
of your 'mydata' and have to guess.


On Sat, 2 Jan 2010, Saji Ren wrote:



Hi, R users:

I want to fit my data into a normal distribution by using the command
fitdistr in MASS.
I changed my data class from ts to numeric by


class(mydata)=numeric


but after using fitdistr, I got the result below


fitdistr(mydata,normal)

 meansd
 NA NA
(NA)   (NA)

the help doc of fitdistr does not mention anything about that, thus I need
your help.


The help page is not intended to be a very basic statistics/R textbook.


Thank you in advanced,
Saji from Shanghai
--
View this message in context: 
http://n4.nabble.com/Help-with-function-fitdistr-in-MASS-tp997609p997609.html
Sent from the R help mailing list archive at Nabble.com.

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



--
Brian D. Ripley,  rip...@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

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


Re: [R] Help with function fitdistr in MASS

2010-01-03 Thread Bernardo Rangel Tura
On Sat, 2010-01-02 at 23:20 -0800, Saji Ren wrote:
 Hi, R users:
 
 I want to fit my data into a normal distribution by using the command
 fitdistr in MASS.
 I changed my data class from ts to numeric by
 
 class(mydata)=numeric
 
 but after using fitdistr, I got the result below
 
 fitdistr(mydata,normal)
   meansd 
   NA NA  
  (NA)   (NA) 
 
 the help doc of fitdistr does not mention anything about that, thus I need
 your help.
 
 Thank you in advanced,
 Saji from Shanghai

Hi Sajj,

You hava NA in your data

try: fitdistr(na.exclude(mydata),normal)

-- 
Bernardo Rangel Tura, M.D,MPH,Ph.D
National Institute of Cardiology
Brazil

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


[R] Help with function fitdistr in MASS

2010-01-02 Thread Saji Ren

Hi, R users:

I want to fit my data into a normal distribution by using the command
fitdistr in MASS.
I changed my data class from ts to numeric by

class(mydata)=numeric

but after using fitdistr, I got the result below

fitdistr(mydata,normal)
  meansd 
  NA NA  
 (NA)   (NA) 

the help doc of fitdistr does not mention anything about that, thus I need
your help.

Thank you in advanced,
Saji from Shanghai
-- 
View this message in context: 
http://n4.nabble.com/Help-with-function-fitdistr-in-MASS-tp997609p997609.html
Sent from the R help mailing list archive at Nabble.com.

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


Re: [R] Help with function fitdistr in MASS

2010-01-02 Thread Saji Ren

I check my data again, and find that:
1. when the class of mydata is ts, I can't compute the sd of it. R returns
'NA'.
2. when I change the class from ts into numeric, R still can't compute the
sd of the data.

Any suggestion?

-- 
View this message in context: 
http://n4.nabble.com/Help-with-function-fitdistr-in-MASS-tp997609p997612.html
Sent from the R help mailing list archive at Nabble.com.

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


Re: [R] Help with function fitdistr in MASS

2010-01-02 Thread Saji Ren

And when I used the command below:

fitdistr(mydata, normal, na.rm=TRUE)

the result is still the same.
-- 
View this message in context: 
http://n4.nabble.com/Help-with-function-fitdistr-in-MASS-tp997609p997615.html
Sent from the R help mailing list archive at Nabble.com.

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


[R] Help with Function!

2009-03-13 Thread Lars Bishop
Dear All,

I need to write 'n' functions on 'm' variables. The functions should be
constructed according to the values of an (nxm) matrix of '1/0' values as
follows. For example,

if row1 is equal to ,say [1 0  ...0 0] then f1 - (1+x1)
if row 2 is equal to, say [1 1 1 0...0 1] then f2
-(1+x1)*(1+x2)*(1+x3)*(1+xm)
if row n is equal to [0 1 0 1 1 0 . 0] then fn -(1+x2)*(1+x4)*(1+x5)

Is there an efficient way to write these functions? Note that each f(x) is a
function of m variables, independently on whether the function explicitly
includes a given variable or not.

Many thanks for your help!

Regards,

Lars.

[[alternative HTML version deleted]]

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


Re: [R] Help with Function!

2009-03-13 Thread Paul Johnson
On Fri, Mar 13, 2009 at 6:28 PM, Lars Bishop lars...@gmail.com wrote:
 Dear All,

 I need to write 'n' functions on 'm' variables. The functions should be
 constructed according to the values of an (nxm) matrix of '1/0' values as
 follows. For example,

 if row1 is equal to ,say [1 0  ...0 0] then f1 - (1+x1)
 if row 2 is equal to, say [1 1 1 0...0 1] then f2
 -(1+x1)*(1+x2)*(1+x3)*(1+xm)
 if row n is equal to [0 1 0 1 1 0 . 0] then fn -(1+x2)*(1+x4)*(1+x5)

 Is there an efficient way to write these functions? Note that each f(x) is a
 function of m variables, independently on whether the function explicitly
 includes a given variable or not.

 Many thanks for your help!

 Regards,




indic - matrix(c(1,1,0,0,0,1,0,0,1,0), ncol=5)

X - data.frame(x1=3,x2=3,x3=4,x4=5,x5=6)

### Give this function 2 vectors, an indicator
### vector ind and a value vector Xin
myHomework - function(ind, Xin){
  prod(Xin[ , which(ind==1)])
}

myHomework(indic[1,], X[1,])

myHomework(indic[2,], X[1,])

Seems to give the right answer.  Let me know if we get an A.

PJ


-- 
Paul E. Johnson
Professor, Political Science
1541 Lilac Lane, Room 504
University of Kansas

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


[R] help with function filter

2008-06-09 Thread wilquin Minaya

 Hi everybody
 
   I need to create a program using the function filter with this vector.  
MATDINAMIC$VELOCIDADFIN[1:1000]
   That´s why I want to identify when use the mean and the median because I 
have problems.
 
   I want to know if I am using correct this:   
filter(MATDINAMIC$VELOCIDADFIN[1:1000],rep(1/3,3)) : ¿ is it for the mean or 
not?
 
and  filter(MATDINAMIC$VELOCIDADFIN[1:1000],rep(3,3)), is it for the median or 
not?
 
 How can I know or identify the right method?
 
  I hope to receive an answer as soon as possible.
 
   Wilquin
_


[[alternative HTML version deleted]]

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


Re: [R] help with function filter

2008-06-09 Thread jim holtman
I think you can get the median with:

filter(MATDINAMIC$VELOCIDADFIN[1:1000],c(0, 1, 0))

On Mon, Jun 9, 2008 at 5:08 PM, wilquin Minaya [EMAIL PROTECTED]
wrote:


  Hi everybody

   I need to create a program using the function filter with this vector.
  MATDINAMIC$VELOCIDADFIN[1:1000]
   That´s why I want to identify when use the mean and the median because I
 have problems.

   I want to know if I am using correct this:
 filter(MATDINAMIC$VELOCIDADFIN[1:1000],rep(1/3,3)) : ¿ is it for the mean or
 not?

 and  filter(MATDINAMIC$VELOCIDADFIN[1:1000],rep(3,3)), is it for the median
 or not?

 How can I know or identify the right method?

  I hope to receive an answer as soon as possible.

   Wilquin
 _


[[alternative HTML version deleted]]


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




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

[[alternative HTML version deleted]]

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