[R] detecting if a variable has changed

2016-06-05 Thread Neal H. Walfield
Hi,

I have a huge list.  Normally it is sorted, but I want to be able to
add elements to it without having to use any special interfaces and
then sort it on demand.  My idea is to use something like weak
references combined with attributes.  Consider:

  # Initialization.
  l = as.list(1:10)
  # Note that it is sorted.
  attr(l, 'sorted') = weakref(l)

  # Modify the list.
  l = append(l, 1:3)

  # Check if the list is still sorted.  (I use identical here, but it
  # probably too heavy weight: I just need to compare the addresses.)
  if (! identical(l, attr(l, 'sorted'))) {
l = sort(unlist(l))
attr(l, 'sorted') = weakref(l)
  }
  # Do operation that requires sorted list.
  ...

This is obviously a toy example.  I'm not actually sorting integers
and I may use a matrix instead of a list.

I've read:

  http://www.hep.by/gnu/r-patched/r-exts/R-exts_122.html
  http://homepage.stat.uiowa.edu/~luke/R/references/weakfinex.html

As far as I can tell, weakrefs are only available via the C API.  Is
there a way to do what I want in R without resorting to C code?  Is
what I want to do better achieved using something other than weakrefs?

Thanks!

:) Neal

__
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] detecting if a variable has changed

2016-06-05 Thread Neal H. Walfield
Hi,

This looks more or less what I'm looking for!  Thanks!

:) Neal

On Sun, 05 Jun 2016 18:47:11 +0200,
William Dunlap wrote:
> 
> [1  ]
> [2  ]
> I don't know what you mean by "without having to use any special
> interfaces", but "reference classes" will do what I think you want.
> E.g., the following makes a class called 'SortedNumeric' that only
> sorts the vector when you want to get its value, not when you append
> values. It stores the sorted vector so it does not get resorted each
> time you ask for it.
> 
> SortedNumeric <- setRefClass("sortedNumeric",
> fields = list(
> fData = "numeric",
> fIsUnsorted = "logical"),
> methods = list(
> initialize = function(Data = numeric(), isUnsorted = TRUE) {
> fData <<- Data
> stopifnot(is.logical(isUnsorted),
> length(isUnsorted)==1,
> !is.na(isUnsorted))
> fIsUnsorted <<- isUnsorted
> },
> getData = function() {
> if (isUnsorted) {
> fData <<- sort(fData)
> fIsUnsorted <<- FALSE
> }
> fData
> },
> appendData = function(newEntries) {
> fData <<- c(fData, newEntries)
> fIsUnsorted <<- TRUE
> }
> ))
> 
> Use it as:
> 
> 
> 
> > x <- SortedNumeric$new()
> 
> 
> > x$appendData(c(4,2,5))
> 
> 
> > x$appendData(c(1,8,9))
> 
> 
> > x
> 
> 
> Reference class object of class "sortedNumeric"
> 
> 
> Field "fData":
> 
> 
> [1] 4 2 5 1 8 9
> 
> 
> Field "fIsUnsorted":
> 
> 
> [1] TRUE
> 
> 
> > x$getData()
> 
> 
> [1] 1 2 4 5 8 9
> 
> 
> > x
>     
> 
> Reference class object of class "sortedNumeric"
> 
> 
> Field "fData":
> 
> 
> [1] 1 2 4 5 8 9
> 
> 
> Field "fIsUnsorted":
> 
> 
> [1] FALSE
> 
> Outside of base R, I think the R6 package gives another approach to
> this.
> 
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
> 
> On Sun, Jun 5, 2016 at 6:53 AM, Neal H. Walfield 
> wrote:
> 
> Hi,
> 
> I have a huge list. Normally it is sorted, but I want to be able
> to
> add elements to it without having to use any special interfaces
> and
> then sort it on demand. My idea is to use something like weak
> references combined with attributes. Consider:
> 
> # Initialization.
> l = as.list(1:10)
> # Note that it is sorted.
> attr(l, 'sorted') = weakref(l)
> 
> # Modify the list.
> l = append(l, 1:3)
> 
> # Check if the list is still sorted. (I use identical here, but it
> # probably too heavy weight: I just need to compare the
> addresses.)
> if (! identical(l, attr(l, 'sorted'))) {
> l = sort(unlist(l))
> attr(l, 'sorted') = weakref(l)
> }
> # Do operation that requires sorted list.
> ...
> 
> This is obviously a toy example. I'm not actually sorting integers
> and I may use a matrix instead of a list.
> 
> I've read:
> 
> http://www.hep.by/gnu/r-patched/r-exts/R-exts_122.html
> http://homepage.stat.uiowa.edu/~luke/R/references/weakfinex.html
> 
> As far as I can tell, weakrefs are only available via the C API.
> Is
> there a way to do what I want in R without resorting to C code? Is
> what I want to do better achieved using something other than
> weakrefs?
> 
> Thanks!
> 
> :) Neal
> 
> __
> 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] detecting if a variable has changed

2016-06-05 Thread Neal H. Walfield
On Sun, 05 Jun 2016 19:34:38 +0200,
Bert Gunter wrote:
> This help thread suggested a question to me:
> 
> Is there a function in some package that efficiently (I.e. O(log(n)) )
> inserts a single new element into the correct location in an
> already-sorted vector? My assumption here is that doing it via sort()
> is inefficient, but maybe that is incorrect. Please correct me if so.

I think data.table will do this if the the column is marked
appropriately.

> I realize that it would be straightforward to write such a function,
> but I just wondered if it already exists. My google & rseek searches
> did not succeed, but maybe I used the wrong keywords.
> 
> 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, Jun 5, 2016 at 9:47 AM, William Dunlap via R-help
>  wrote:
> > I don't know what you mean by "without having to use any special
> > interfaces", but "reference classes" will do what I think you want.  E.g.,
> > the following makes a class called 'SortedNumeric' that only sorts the
> > vector when you want to get its value, not when you append values.  It
> > stores the sorted vector so it does not get resorted each time you ask for
> > it.
> >
> > SortedNumeric <- setRefClass("sortedNumeric",
> > fields = list(
> > fData = "numeric",
> > fIsUnsorted = "logical"),
> > methods = list(
> > initialize = function(Data = numeric(), isUnsorted = TRUE) {
> > fData <<- Data
> > stopifnot(is.logical(isUnsorted),
> >   length(isUnsorted)==1,
> >   !is.na(isUnsorted))
> > fIsUnsorted <<- isUnsorted
> > },
> > getData = function() {
> > if (isUnsorted) {
> > fData <<- sort(fData)
> > fIsUnsorted <<- FALSE
> > }
> > fData
> > },
> > appendData = function(newEntries) {
> > fData <<- c(fData, newEntries)
> > fIsUnsorted <<- TRUE
> > }
> > ))
> >
> > Use it as:
> >
> >> x <- SortedNumeric$new()
> >> x$appendData(c(4,2,5))
> >> x$appendData(c(1,8,9))
> >> x
> > Reference class object of class "sortedNumeric"
> > Field "fData":
> > [1] 4 2 5 1 8 9
> > Field "fIsUnsorted":
> > [1] TRUE
> >> x$getData()
> > [1] 1 2 4 5 8 9
> >> x
> > Reference class object of class "sortedNumeric"
> > Field "fData":
> > [1] 1 2 4 5 8 9
> > Field "fIsUnsorted":
> > [1] FALSE
> >
> >
> > Outside of base R, I think the R6 package gives another approach to this.
> >
> >
> > Bill Dunlap
> > TIBCO Software
> > wdunlap tibco.com
> >
> > On Sun, Jun 5, 2016 at 6:53 AM, Neal H. Walfield  wrote:
> >
> >> Hi,
> >>
> >> I have a huge list.  Normally it is sorted, but I want to be able to
> >> add elements to it without having to use any special interfaces and
> >> then sort it on demand.  My idea is to use something like weak
> >> references combined with attributes.  Consider:
> >>
> >>   # Initialization.
> >>   l = as.list(1:10)
> >>   # Note that it is sorted.
> >>   attr(l, 'sorted') = weakref(l)
> >>
> >>   # Modify the list.
> >>   l = append(l, 1:3)
> >>
> >>   # Check if the list is still sorted.  (I use identical here, but it
> >>   # probably too heavy weight: I just need to compare the addresses.)
> >>   if (! identical(l, attr(l, 'sorted'))) {
> >> l = sort(unlist(l))
> >> attr(l, 'sorted') = weakref(l)
> >>   }
> >>   # Do operation that requires sorted list.
> >>   ...
> >>
> >> This is obviously a toy example.  I'm not actually sorting integers
> >> and I may use a matrix instead of a list.
> >>
> >> I've read:
> >>
> >>   http://www.hep.by/gnu/r-patched/r-exts/R-exts_122.html
> >> 

[R] %in% with matrix of lists

2016-07-30 Thread Neal H. Walfield
I have a matrix of lists.  Something along the lists of (but much
bigger than):

  x = array(dim=c(2, 2), data=list())
  x[1,1] = list(1:5)
  x[2,1] = list(6:9)
  x[1,2] = list(10:13)
  x[2,2] = list(14:16)

Each list contains a number of observations/ground truth for a
particular state.  That is, state <1,1> has observations <1,2,3,4,5>.
States may have a different number of observations (which is why I'm
not using an array).

I have another numeric matrix with the same dimensions and I want to
check if its values occur.  Something along the lines of:

 y = array(dim=c(2, 2), data=c(1, 10, 11, 20))

I want to do:

 y %in% x

But it doesn't work as hoped.  (I expect: c(T, F, T, F).)

I realize that I can do this with sapply, but I was hoping for a
faster / smarter solution.  Any ideas?

Thanks!

:) Neal

__
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] %in% with matrix of lists

2016-07-30 Thread Neal H. Walfield
On Sat, 30 Jul 2016 19:28:42 +0200,
Bert Gunter wrote:
> Bottom line: No, I dont see any vectorized way to do this.
> 
> However, the following may offer some slight improvement over your approach.
> 
> 1. Why do you need to store these as arrays, which are merely vectors
> with a "dim" attribute?
> 
> ## convert to vectors (a list is also a vector):
> 
> dim(x) <- NULL; dim(y) <- NULL

I apologize that my example was not minimal.  I have the data stored
in an array for other reasons.

> 2. ... and use mapply() instead of sapply() (not clear to me  *how*
> you mean to use sapply() anyway)

Sorry, I meant (and I'm using) mapply.

> > mapply("%in%",y,x)  ## might be slightly faster to use match() directly
> 
> [1]  TRUE FALSE  TRUE FALSE  ## NOT c(T,F,T,F)

I'm not sure what you mean by NOT here.  You get the same answer as I
do, as far as I can see.


Thanks for your help!

:) Neal

__
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] %in% with matrix of lists

2016-07-30 Thread Neal H. Walfield
On Sat, 30 Jul 2016 20:35:40 +0200,
Jeff Newmiller wrote:
> 
> >> [1]  TRUE FALSE  TRUE FALSE  ## NOT c(T,F,T,F)
> >
> >I'm not sure what you mean by NOT here.  You get the same answer as I
> >do, as far as I can see.
> >
> 
> # valid R
> T <- FALSE
> # invalid R
> TRUE <- FALSE
> 
> It is much much safer and clearer to use TRUE/FALSE than T/F. So
> 
> c( TRUE, FALSE,  TRUE, FALSE ) may not always be the same as c(T,F,T,F).

I see.  I was just trying to create a minimal example.  I'll try to be
more vigorous next time!

> I recommend reading the R Inferno to learn about this other such pitfalls. 

Thanks for the tip!

__
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] write.table produces a file that read.table can't read

2014-10-08 Thread Neal H. Walfield
Hi,

I'm using R version 3.1.1 on Debian via the CRAN repositories.

Consider the following MWE that writes a data.frame out to a file and
reads it back in (note that one of the strings contains a double
quote):

  > write.table(data.frame(a=1:3, b=c("a", "b\"b", "c")), '/tmp/a', 
row.names=FALSE, na="?", sep=",")
  > read.table('/tmp/a', header=TRUE, row.names=NULL, sep=',', na.strings='?', 
allowEscapes=T)
  [1] a b
  <0 rows> (or 0-length row.names)

/tmp/a contains the following:

  $ cat /tmp/a 
  "a","b"
  1,"a"
  2,"b\"b"
  3,"c"

Removing the double quote, it works:

  > write.table(data.frame(a=1:3, b=c("a", "bb", "c")), '/tmp/a', 
row.names=FALSE, na="?", sep=",")
  > read.table('/tmp/a', header=TRUE, row.names=NULL, sep=',', na.strings='?', 
allowEscapes=T)
a  b
  1 1  a
  2 2 bb
  3 3  c

Why does allowEscapes not work for double quotes?  Or, why does
write.table produce a file that read.table can't read!

Thanks for any advice!

Neal

__
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] vectorization & modifying globals in functions

2012-12-27 Thread Neal H. Walfield
At Thu, 27 Dec 2012 15:38:08 -0500,
Sam Steingold wrote:
> so,
> 1. is there a way for a function to modify a global variable?

Use <<- instead of <-.

> 2. how would you vectorize this loop?

This is hard.  Your function has a feedback loop: an iteration depends
on the previous iteration's result.  A for loop is about as good as
you can do in this case.  sapply might help a bit, but it is really
just a for loop in disguise.

Since sample.int is used to generate indexes, you might try to
generate a bunch of indexes, take as many as don't overlap (i.e.,
collect all orthogonal updates) and do all of those updates at once.
If you really need the entropy after every iteration, however, then
this won't work for you either.

Neal

__
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] parallel error message extraction (in mclapply)?

2012-12-29 Thread Neal H. Walfield
Hi, Ivo,

At Fri, 28 Dec 2012 16:34:03 -0800,
ivo welch wrote:
> so, it isn't obvious to me how I get to the try-error object
> list, or how to find out what triggered the abort.  

A try object is an object that captures the execution context at the
time of an error.  Consider the following code:

  > library(multicore)
  > result = mclapply(1:10, function (i) { if (i == 4) foo() else i })
  Warning message:
  In mclapply(1:10, function(i) { :
scheduled core 4 encountered error in user code, all values of the job will 
be affected

To investigate the error, we can use the try object that has been
saved in position 4:

  > print(result[[4]])
  [1] "Error in FUN(4L[[1L]], ...) : could not find function \"foo\"\n"
  attr(,"class")
  [1] "try-error"
  attr(,"condition")
   traceback(result[[4]])
  1: Error in FUN(4L[[1L]], ...) : could not find function "foo"

Neal

__
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] How to multiple the vector and variables from dataframe

2012-12-30 Thread Neal H. Walfield
At Sun, 30 Dec 2012 18:26:45 +0800 (CST),
meng wrote:
> 
> hi all:
> Here's a dataframe(dat) and a vector(z):
>  
> dat:
> x1 x2x3
> 0.2   1.2   2.5
> 0.5   2  5
> 0.8   3  6.2
>  
> > z
> [1]  10 100 100
>  
> I wanna do the following:
> 10*x1,100*x2,1000*x3
>  
> My solution is using the loop for z and dat(since the length of z is the same 
> as ncol  of dat),which is tedious.
> I wanna an efficient solution to do it .

You could convert the data frame to a matrix:

> dat=data.frame(x1=1:3, x2=11:13)
> dat
  x1 x2
1  1 11
2  2 12
3  3 13
> as.matrix(dat) * c(3, 2)
 x1 x2
[1,]  3 22
[2,]  4 36
[3,]  9 26

Neal

__
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] How to multiple the vector and variables from dataframe

2012-12-30 Thread Neal H. Walfield
At Sun, 30 Dec 2012 16:28:44 +,
Andrius Druzinis wrote:
> 
> Hi Neal,
> 
> Notice that c(2, 3) gets replicated into c(2, 3, 2, 3, 2, 3) and then
> multiplied by column. This is not the same as multiplying each column by
> the respective element in vector c(2, 3).

I think you mean multiplied by element.  

Here's a better solution using t to transpose the matrix:

dat=data.frame(x1=1:3, x2=11:13)
 as.matrix(dat)
 x1 x2
[1,]  1 11
[2,]  2 12
[3,]  3 13
t(as.matrix(dat)) * c(2, 3)
   [,1] [,2] [,3]
x1246
x2   33   36   39

> 
> Andrius
> 
> 
> 2012/12/30 Neal H. Walfield 
> 
> > At Sun, 30 Dec 2012 18:26:45 +0800 (CST),
> > meng wrote:
> > >
> > > hi all:
> > > Here's a dataframe(dat) and a vector(z):
> > >
> > > dat:
> > > x1 x2x3
> > > 0.2   1.2   2.5
> > > 0.5   2  5
> > > 0.8   3  6.2
> > >
> > > > z
> > > [1]  10 100 100
> > >
> > > I wanna do the following:
> > > 10*x1,100*x2,1000*x3
> > >
> > > My solution is using the loop for z and dat(since the length of z is the
> > same as ncol  of dat),which is tedious.
> > > I wanna an efficient solution to do it .
> >
> > You could convert the data frame to a matrix:
> >
> > > dat=data.frame(x1=1:3, x2=11:13)
> > > dat
> >   x1 x2
> > 1  1 11
> > 2  2 12
> > 3  3 13
> > > as.matrix(dat) * c(3, 2)
> >  x1 x2
> > [1,]  3 22
> > [2,]  4 36
> > [3,]  9 26
> >
> > Neal
> >
> > __
> > 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.


Re: [R] group values in classes

2012-12-31 Thread Neal H. Walfield
At Mon, 31 Dec 2012 12:13:43 +0200,
catalin roibu wrote:
> 
> Dear R users,
> I want to group numerical values in classes with different size and count
> the values for each classes.
> 
> My data is in this forma:
>  d  15  12,5  30,4  20,5  80,4  100,5  8,2  40,5  33  21  11
> And I want the group them in classes with 4 (5,etc) cm size like this:
>  class d  16  16  32  24  84  104  12  44  36  24  12
> and final to count values for each class:
> class d   n  12 2  16 2  24 2  32 1  36 1  44 1
> 84 1  104 1  Total 11

I rounded the sizes to the nearst whole cm.  It's not clear to me how
you want to group the elements from your description...

> data = c(15,  12.5,  30.4,  20.5,  80.4,  100.5,  8.2,  40.5,  33, 21,  11)
> aggregate(data, list(round(data)), length)
   Group.1 x
18 1
2   11 1
3   12 1
4   15 1
5   20 1
6   21 1
7   30 1
8   33 1
9   40 1
10  80 1
11 100 1

aggregate groups each element in the first argument according to the
specified class.  The third element is a function that is called on
each group.

Neal

__
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] Installing R-2.15.2 in Debian Wheezy/Testing

2012-12-31 Thread Neal H. Walfield
At Mon, 31 Dec 2012 15:38:10 -0500,
Stephen P. Molnar wrote:
> The current version of R that is available in Wheezy is 2.15.1. However, 
> version 2.15.2 is available at CRAN sites.
> ...
> My question is what should be the format of the line in the sources.lists?

Here's what I have:

  deb http://ftp5.gwdg.de/pub/misc/cran/bin/linux/debian squeeze-cran/

Neal

__
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] cut ()

2012-12-31 Thread Neal H. Walfield
At Mon, 31 Dec 2012 22:25:25 +,
Muhuri, Pradip (SAMHSA/CBHSQ) wrote:
> The issue is that, for Utah, I am getting an  instead of (42,48.7] in the 
> ob_mrj_cat column.

The problem is likely due to comparisons of floating point numbers.
Try moving your lower and upper bounds out a tiny bit.  When I add

  c(-1e-8, 0, 0, 0, 0, 1e8)

to the result of quantile, I don't get any NAs.

Neal

__
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] cut ()

2013-01-01 Thread Neal H. Walfield
At Tue, 1 Jan 2013 02:00:14 +,
Muhuri, Pradip (SAMHSA/CBHSQ) wrote:
> Although David's solution (putting the right parenthesis, which I had missed) 
> has resolved the issue, I would like to try yours as well.
> 
> Could you please clarify the six elements:  c(-1e-8, 0, 0, 0, 0, 1e8)?

It's a vector.  Floating point numbers can't always store an exact
representation.  So, for instance, R evaluates ".1 == .3 / 3" as
FALSE.  If instead you had done: "abs(.1 - .3 / 3) < 1e-8", the result
would be true.  The latter does not check for strict equality, but
whether the value is within some small delta of the target.  Read
chapter 1 of the R Inferno.

Note, the last element of the vector should be 1e-8, not 1e8.

Neal

__
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] Working with Matrix

2013-01-04 Thread Neal H. Walfield
At Fri, 4 Jan 2013 17:17:40 +0530,
Christofer Bogaso wrote:
> 
> Hello again,
> 
> Let say I have 2 matrices which equal number of columns but different
> number of rows like:
> 
> Mat1 <- matrix(1:20, 4, 5)
> Mat2 <- matrix(1:25, 5, 5)
> 
> Now for each column 1-to-5 I need to fetch the corresponding columns
> of these 2 matrices and add the corresponding elements (ignoring NA
> values if any). Therefore for the 1st column I need to do:
> 
> (1+1), (2+2),...,(4+4), (NA+5)
> 
> and so on
> 
> And the resulting numbers will be stored in some other matrix

This will redimension Mat1 and fill in NAs as you want:

Mat3 = matrix(c(t(Mat1), rep(NA, (nrow(Mat2) - nrow(Mat1)) * ncol(Mat1))),
byrow=TRUE, ncol=ncol(Mat1))

Then you can do Mat2 + Mat3.

> Also note that, here I gave the example of addition, however, this can
> be any user defined function.

You can use mapply for this:

  mapply(function (a, b) { a + b }, Mat2, Mat3)

Neal

__
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] Combinations

2013-03-15 Thread Neal H. Walfield
At Fri, 15 Mar 2013 09:22:15 -0400,
Amir wrote:
> I have two sets T1={c1,c2,..,cn} and T2={k1,k2,...,kn}.
> How can I find the sets as follow:
> 
> (c1,k1), (c1,k2) ...(c1,kn)  (c2,k1) (c2,k2)  (c2,kn) ... (cn,kn)

I think you are looking for expand.grid:

expand.grid(1:3, 10:13)
   Var1 Var2
1 1   10
2 2   10
3 3   10
4 1   11
...

Neal

__
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.