Re: [R] Merge and replace data

2023-09-07 Thread Richard O'Keefe
I'm a little confused, because the sample code does something that
none of the suggestions does.
x1 <- c(116,0,115,137,127,0,0)
x2 <- c(0,159,0,0,0,159,127)

[You] want : xx <- c(116,115,137,127,159, 127)
Assuming that there should have been two copies of 159 in xx, this is
xx <- c(x1[x1 != 0], x2[x2 != 0])
pmax or ifelse would give you
xx <- c(116,159,115,137,127,159,127)  # NOT the desired result

If there really was supposed to be one copy of 159,
you might want to call unique() somewhere.

You really need to set out clearly and unambiguously what
you actually want.


On Wed, 6 Sept 2023 at 01:42, roslinazairimah zakaria 
wrote:

> Thank you for the general code. Really appreciate it.
>
>
> On Tue, Sep 5, 2023 at 7:59 PM Eric Berger  wrote:
>
> > As Duncan points out, ifelse() provides a more general approach than
> > the specific pmax().
> >
> > Even more generally, you might want to consider the apply() function
> > (and its relatives sapply(), lapply(), ...)
> >
> > For example
> >
> > apply(cbind(x1,x2), MAR=1, max)
> >
> > In the above statement, x1 and x2 are combined into two columns, MAR=1
> > says apply the following function to each row,
> > and max specifies the function to be applied. The summary is that this
> > statement applies the max to each row in the object
> > specified by the first argument. It's very general because you can
> > replace max by an arbitrary function.
> >
> > Similarly, if you get more into R you might modify the above using
> > chained operations. The following rewrites the above
> > by piping cbind(x1,x2) into the apply command. Not a necessary feature
> > to know but eventually will assist you in writing code that does
> > not require a lot of intermediate variables.
> >
> > cbind(x1,x2) |> apply(MAR=1,max)
> >
> > On Tue, Sep 5, 2023 at 2:45 PM Duncan Murdoch 
> > wrote:
> > >
> > > On 05/09/2023 4:55 a.m., roslinazairimah zakaria wrote:
> > > > Hi all,
> > > >
> > > > I have these data
> > > >
> > > > x1 <- c(116,0,115,137,127,0,0)
> > > > x2 <- c(0,159,0,0,0,159,127)
> > > >
> > > > I want : xx <- c(116,115,137,127,159, 127)
> > > >
> > > > I would like to merge these data into one column. Whenever the data
> is
> > '0'
> > > > it will be replaced by the value in the column which is non zero..
> > > > I tried append and merge but fail to get what I want.
> > > >
> > >
> > > Others have pointed out pmax(x1, x2).  For the sample data, even x1+x2
> > > would give the same answer.
> > >
> > > The real question is what you want to do if both x1 and x2 have
> non-zero
> > > entries, or negative entries:
> > >
> > >pmax(x1, x2)
> > >
> > > will pick the larger one.  It might be zero if the other is negative,
> > > and that doesn't match your description.
> > >
> > >x1+x2
> > >
> > > will just give the sum, which doesn't match your description if both
> are
> > > non-zero.
> > >
> > > But maybe you want the x1 value unless it is zero, even if it is
> smaller
> > > than the x2 value:  then you would use
> > >
> > >ifelse(x1 != 0, x1, x2)
> > >
> > > Similarly
> > >
> > >ifelse(x2 != 0, x2, x1)
> > >
> > > would prefer the x2 value.
> > >
> > > You should think about what you would do in these other cases as well.
> > >
> > > Duncan Murdoch
> > >
> > > __
> > > 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.
> >
>
>
> --
> *Roslinazairimah Zakaria*
> *Tel: +609-5492370; Fax. No.+609-5492766*
>
> *Email: roslinazairi...@ump.edu.my ;
> roslina...@gmail.com *
> Faculty of Industrial Sciences & Technology
> University Malaysia Pahang
> Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia
>
> [[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.


Re: [R] Merge and replace data

2023-09-05 Thread roslinazairimah zakaria
Thank you for the general code. Really appreciate it.


On Tue, Sep 5, 2023 at 7:59 PM Eric Berger  wrote:

> As Duncan points out, ifelse() provides a more general approach than
> the specific pmax().
>
> Even more generally, you might want to consider the apply() function
> (and its relatives sapply(), lapply(), ...)
>
> For example
>
> apply(cbind(x1,x2), MAR=1, max)
>
> In the above statement, x1 and x2 are combined into two columns, MAR=1
> says apply the following function to each row,
> and max specifies the function to be applied. The summary is that this
> statement applies the max to each row in the object
> specified by the first argument. It's very general because you can
> replace max by an arbitrary function.
>
> Similarly, if you get more into R you might modify the above using
> chained operations. The following rewrites the above
> by piping cbind(x1,x2) into the apply command. Not a necessary feature
> to know but eventually will assist you in writing code that does
> not require a lot of intermediate variables.
>
> cbind(x1,x2) |> apply(MAR=1,max)
>
> On Tue, Sep 5, 2023 at 2:45 PM Duncan Murdoch 
> wrote:
> >
> > On 05/09/2023 4:55 a.m., roslinazairimah zakaria wrote:
> > > Hi all,
> > >
> > > I have these data
> > >
> > > x1 <- c(116,0,115,137,127,0,0)
> > > x2 <- c(0,159,0,0,0,159,127)
> > >
> > > I want : xx <- c(116,115,137,127,159, 127)
> > >
> > > I would like to merge these data into one column. Whenever the data is
> '0'
> > > it will be replaced by the value in the column which is non zero..
> > > I tried append and merge but fail to get what I want.
> > >
> >
> > Others have pointed out pmax(x1, x2).  For the sample data, even x1+x2
> > would give the same answer.
> >
> > The real question is what you want to do if both x1 and x2 have non-zero
> > entries, or negative entries:
> >
> >pmax(x1, x2)
> >
> > will pick the larger one.  It might be zero if the other is negative,
> > and that doesn't match your description.
> >
> >x1+x2
> >
> > will just give the sum, which doesn't match your description if both are
> > non-zero.
> >
> > But maybe you want the x1 value unless it is zero, even if it is smaller
> > than the x2 value:  then you would use
> >
> >ifelse(x1 != 0, x1, x2)
> >
> > Similarly
> >
> >ifelse(x2 != 0, x2, x1)
> >
> > would prefer the x2 value.
> >
> > You should think about what you would do in these other cases as well.
> >
> > Duncan Murdoch
> >
> > __
> > 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.
>


-- 
*Roslinazairimah Zakaria*
*Tel: +609-5492370; Fax. No.+609-5492766*

*Email: roslinazairi...@ump.edu.my ;
roslina...@gmail.com *
Faculty of Industrial Sciences & Technology
University Malaysia Pahang
Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia

[[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] Merge and replace data

2023-09-05 Thread roslinazairimah zakaria
Thank you for the general code. Really appreciate it.

On Tue, Sep 5, 2023 at 7:45 PM Duncan Murdoch 
wrote:

> On 05/09/2023 4:55 a.m., roslinazairimah zakaria wrote:
> > Hi all,
> >
> > I have these data
> >
> > x1 <- c(116,0,115,137,127,0,0)
> > x2 <- c(0,159,0,0,0,159,127)
> >
> > I want : xx <- c(116,115,137,127,159, 127)
> >
> > I would like to merge these data into one column. Whenever the data is
> '0'
> > it will be replaced by the value in the column which is non zero..
> > I tried append and merge but fail to get what I want.
> >
>
> Others have pointed out pmax(x1, x2).  For the sample data, even x1+x2
> would give the same answer.
>
> The real question is what you want to do if both x1 and x2 have non-zero
> entries, or negative entries:
>
>pmax(x1, x2)
>
> will pick the larger one.  It might be zero if the other is negative,
> and that doesn't match your description.
>
>x1+x2
>
> will just give the sum, which doesn't match your description if both are
> non-zero.
>
> But maybe you want the x1 value unless it is zero, even if it is smaller
> than the x2 value:  then you would use
>
>ifelse(x1 != 0, x1, x2)
>
> Similarly
>
>ifelse(x2 != 0, x2, x1)
>
> would prefer the x2 value.
>
> You should think about what you would do in these other cases as well.
>
> Duncan Murdoch
>


-- 
*Roslinazairimah Zakaria*
*Tel: +609-5492370; Fax. No.+609-5492766*

*Email: roslinazairi...@ump.edu.my ;
roslina...@gmail.com *
Faculty of Industrial Sciences & Technology
University Malaysia Pahang
Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia

[[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] Merge and replace data

2023-09-05 Thread Eric Berger
As Duncan points out, ifelse() provides a more general approach than
the specific pmax().

Even more generally, you might want to consider the apply() function
(and its relatives sapply(), lapply(), ...)

For example

apply(cbind(x1,x2), MAR=1, max)

In the above statement, x1 and x2 are combined into two columns, MAR=1
says apply the following function to each row,
and max specifies the function to be applied. The summary is that this
statement applies the max to each row in the object
specified by the first argument. It's very general because you can
replace max by an arbitrary function.

Similarly, if you get more into R you might modify the above using
chained operations. The following rewrites the above
by piping cbind(x1,x2) into the apply command. Not a necessary feature
to know but eventually will assist you in writing code that does
not require a lot of intermediate variables.

cbind(x1,x2) |> apply(MAR=1,max)

On Tue, Sep 5, 2023 at 2:45 PM Duncan Murdoch  wrote:
>
> On 05/09/2023 4:55 a.m., roslinazairimah zakaria wrote:
> > Hi all,
> >
> > I have these data
> >
> > x1 <- c(116,0,115,137,127,0,0)
> > x2 <- c(0,159,0,0,0,159,127)
> >
> > I want : xx <- c(116,115,137,127,159, 127)
> >
> > I would like to merge these data into one column. Whenever the data is '0'
> > it will be replaced by the value in the column which is non zero..
> > I tried append and merge but fail to get what I want.
> >
>
> Others have pointed out pmax(x1, x2).  For the sample data, even x1+x2
> would give the same answer.
>
> The real question is what you want to do if both x1 and x2 have non-zero
> entries, or negative entries:
>
>pmax(x1, x2)
>
> will pick the larger one.  It might be zero if the other is negative,
> and that doesn't match your description.
>
>x1+x2
>
> will just give the sum, which doesn't match your description if both are
> non-zero.
>
> But maybe you want the x1 value unless it is zero, even if it is smaller
> than the x2 value:  then you would use
>
>ifelse(x1 != 0, x1, x2)
>
> Similarly
>
>ifelse(x2 != 0, x2, x1)
>
> would prefer the x2 value.
>
> You should think about what you would do in these other cases as well.
>
> Duncan Murdoch
>
> __
> 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] Merge and replace data

2023-09-05 Thread Duncan Murdoch

On 05/09/2023 4:55 a.m., roslinazairimah zakaria wrote:

Hi all,

I have these data

x1 <- c(116,0,115,137,127,0,0)
x2 <- c(0,159,0,0,0,159,127)

I want : xx <- c(116,115,137,127,159, 127)

I would like to merge these data into one column. Whenever the data is '0'
it will be replaced by the value in the column which is non zero..
I tried append and merge but fail to get what I want.



Others have pointed out pmax(x1, x2).  For the sample data, even x1+x2 
would give the same answer.


The real question is what you want to do if both x1 and x2 have non-zero 
entries, or negative entries:


  pmax(x1, x2)

will pick the larger one.  It might be zero if the other is negative, 
and that doesn't match your description.


  x1+x2

will just give the sum, which doesn't match your description if both are 
non-zero.


But maybe you want the x1 value unless it is zero, even if it is smaller 
than the x2 value:  then you would use


  ifelse(x1 != 0, x1, x2)

Similarly

  ifelse(x2 != 0, x2, x1)

would prefer the x2 value.

You should think about what you would do in these other cases as well.

Duncan Murdoch

__
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] Merge and replace data

2023-09-05 Thread roslinazairimah zakaria
Thank you very much for your help.

On Tue, Sep 5, 2023 at 6:39 PM Rui Barradas  wrote:

> Às 09:55 de 05/09/2023, roslinazairimah zakaria escreveu:
> > Hi all,
> >
> > I have these data
> >
> > x1 <- c(116,0,115,137,127,0,0)
> > x2 <- c(0,159,0,0,0,159,127)
> >
> > I want : xx <- c(116,115,137,127,159, 127)
> >
> > I would like to merge these data into one column. Whenever the data is
> '0'
> > it will be replaced by the value in the column which is non zero..
> > I tried append and merge but fail to get what I want.
> >
> Hello,
>
> That's a case for ?pmax:
>
>
> x1 <- c(116,0,115,137,127,0,0)
> x2 <- c(0,159,0,0,0,159,127)
> pmax(x1, x2)
> #> [1] 116 159 115 137 127 159 127
>
>
> Hope this helps,
>
> Rui Barradas
>


-- 
*Roslinazairimah Zakaria*
*Tel: +609-5492370; Fax. No.+609-5492766*

*Email: roslinazairi...@ump.edu.my ;
roslina...@gmail.com *
Faculty of Industrial Sciences & Technology
University Malaysia Pahang
Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia

[[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] Merge and replace data

2023-09-05 Thread roslinazairimah zakaria
Thank you very much for your help.

On Tue, Sep 5, 2023 at 6:12 PM Eric Berger  wrote:

> xx <- pmax(x1,x2)
>
> On Tue, Sep 5, 2023 at 11:56 AM roslinazairimah zakaria
>  wrote:
> >
> > Hi all,
> >
> > I have these data
> >
> > x1 <- c(116,0,115,137,127,0,0)
> > x2 <- c(0,159,0,0,0,159,127)
> >
> > I want : xx <- c(116,115,137,127,159, 127)
> >
> > I would like to merge these data into one column. Whenever the data is
> '0'
> > it will be replaced by the value in the column which is non zero..
> > I tried append and merge but fail to get what I want.
> >
> > --
> > *Roslina Zakaria*
> >
> > [[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.
>


-- 
*Roslinazairimah Zakaria*
*Tel: +609-5492370; Fax. No.+609-5492766*

*Email: roslinazairi...@ump.edu.my ;
roslina...@gmail.com *
Faculty of Industrial Sciences & Technology
University Malaysia Pahang
Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia

[[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] Merge and replace data

2023-09-05 Thread Rui Barradas

Às 09:55 de 05/09/2023, roslinazairimah zakaria escreveu:

Hi all,

I have these data

x1 <- c(116,0,115,137,127,0,0)
x2 <- c(0,159,0,0,0,159,127)

I want : xx <- c(116,115,137,127,159, 127)

I would like to merge these data into one column. Whenever the data is '0'
it will be replaced by the value in the column which is non zero..
I tried append and merge but fail to get what I want.


Hello,

That's a case for ?pmax:


x1 <- c(116,0,115,137,127,0,0)
x2 <- c(0,159,0,0,0,159,127)
pmax(x1, x2)
#> [1] 116 159 115 137 127 159 127


Hope this helps,

Rui Barradas

__
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] Merge and replace data

2023-09-05 Thread Eric Berger
xx <- pmax(x1,x2)

On Tue, Sep 5, 2023 at 11:56 AM roslinazairimah zakaria
 wrote:
>
> Hi all,
>
> I have these data
>
> x1 <- c(116,0,115,137,127,0,0)
> x2 <- c(0,159,0,0,0,159,127)
>
> I want : xx <- c(116,115,137,127,159, 127)
>
> I would like to merge these data into one column. Whenever the data is '0'
> it will be replaced by the value in the column which is non zero..
> I tried append and merge but fail to get what I want.
>
> --
> *Roslina Zakaria*
>
> [[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] Merge with closest (not equal) time stamps

2023-08-24 Thread Naresh Gurbuxani
Thanks for your suggestion. 

I have just returned from a vacation and started catching up on my emails. 

Rolling join is an elegant and most suitable solution for my tasks.  I invested 
some time in learning data.table package.  The vignette on secondary indices 
and auto indexing refers to another vignette on joins and rolling joins.  But 
the vignette on joins is missing.  There is already some discussion on the 
internet regarding this missing vignette.  

Here is one more request for this joins vignette, even if in a draft form.  

Naresh  

> On Aug 9, 2023, at 8:39 AM, Hadley Wickham  wrote:
> 
> It sounds like you might want a rolling join, e.g.
> https://dplyr.tidyverse.org/reference/join_by.html#rolling-joins.
> 
> (And data.table has similar functionality which inspired dplyr)
> 
> Hadley
> 
> On Mon, Aug 7, 2023 at 9:32 PM Naresh Gurbuxani
>  wrote:
>> 
>> 
>> I have two dataframes, each with a column for timestamp.  I want to
>> merge the two dataframes such that each row from first dataframe
>> is matched with the row in the second dataframe with most recent but
>> preceding timestamp. Here is an example.
>> 
>> option.trades <- data.frame(timestamp = as.POSIXct(c("2023-08-07 10:23:22", 
>> "2023-08-07 10:25:33", "2023-08-07 10:28:41")), option.price = c(2.5, 2.7, 
>> 1.8))
>> 
>> stock.trades <- data.frame(timestamp = as.POSIXct(c("2023-08-07 10:23:21", 
>> "2023-08-07 10:23:34", "2023-08-07 10:24:57", "2023-08-07 10:28:37", 
>> "2023-08-07 10:29:01")), stock.price = c(102.2, 102.9, 103.1, 101.8, 101.7))
>> 
>> stock.trades <- stock.trades[order(stock.trades$timestamp),]
>> 
>> library(plyr)
>> mystock.prices <- ldply(option.trades$timestamp, function(tstamp) 
>> tail(subset(stock.trades, timestamp <= tstamp), 1))
>> names(mystock.prices)[1] <- "stock.timestamp"
>> myres <- cbind(option.trades, mystock.prices)
>> 
>> This method works. But for large dataframes, it is very slow.  Is there
>> a way to speed up the merge?
>> 
>> Thanks,
>> Naresh
>> 
>> __
>> 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.
> 
> 
> 
> -- 
> http://hadley.nz

__
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] Merge with closest (not equal) time stamps

2023-08-09 Thread Hadley Wickham
It sounds like you might want a rolling join, e.g.
https://dplyr.tidyverse.org/reference/join_by.html#rolling-joins.

(And data.table has similar functionality which inspired dplyr)

Hadley

On Mon, Aug 7, 2023 at 9:32 PM Naresh Gurbuxani
 wrote:
>
>
> I have two dataframes, each with a column for timestamp.  I want to
> merge the two dataframes such that each row from first dataframe
> is matched with the row in the second dataframe with most recent but
> preceding timestamp. Here is an example.
>
> option.trades <- data.frame(timestamp = as.POSIXct(c("2023-08-07 10:23:22", 
> "2023-08-07 10:25:33", "2023-08-07 10:28:41")), option.price = c(2.5, 2.7, 
> 1.8))
>
> stock.trades <- data.frame(timestamp = as.POSIXct(c("2023-08-07 10:23:21", 
> "2023-08-07 10:23:34", "2023-08-07 10:24:57", "2023-08-07 10:28:37", 
> "2023-08-07 10:29:01")), stock.price = c(102.2, 102.9, 103.1, 101.8, 101.7))
>
> stock.trades <- stock.trades[order(stock.trades$timestamp),]
>
> library(plyr)
> mystock.prices <- ldply(option.trades$timestamp, function(tstamp) 
> tail(subset(stock.trades, timestamp <= tstamp), 1))
> names(mystock.prices)[1] <- "stock.timestamp"
> myres <- cbind(option.trades, mystock.prices)
>
> This method works. But for large dataframes, it is very slow.  Is there
> a way to speed up the merge?
>
> Thanks,
> Naresh
>
> __
> 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.



-- 
http://hadley.nz

__
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] Merge with closest (not equal) time stamps

2023-08-08 Thread Enrico Schumann
On Mon, 07 Aug 2023, Naresh Gurbuxani writes:

> I have two dataframes, each with a column for timestamp.  I want to
> merge the two dataframes such that each row from first dataframe
> is matched with the row in the second dataframe with most recent but
> preceding timestamp. Here is an example.
>
> option.trades <- data.frame(timestamp = as.POSIXct(c("2023-08-07 10:23:22", 
> "2023-08-07 10:25:33", "2023-08-07 10:28:41")), option.price = c(2.5, 2.7, 
> 1.8))
>
> stock.trades <- data.frame(timestamp =
> as.POSIXct(c("2023-08-07 10:23:21", "2023-08-07
> 10:23:34", "2023-08-07 10:24:57", "2023-08-07
> 10:28:37", "2023-08-07 10:29:01")), stock.price =
> c(102.2, 102.9, 103.1, 101.8, 101.7))
>
> stock.trades <- stock.trades[order(stock.trades$timestamp),]
>
> library(plyr)
> mystock.prices <- ldply(option.trades$timestamp, function(tstamp) 
> tail(subset(stock.trades, timestamp <= tstamp), 1))
> names(mystock.prices)[1] <- "stock.timestamp"
> myres <- cbind(option.trades, mystock.prices)
>
> This method works. But for large dataframes, it is very slow.  Is there
> a way to speed up the merge?
>
> Thanks,
> Naresh
>

If the timestamps are sorted (or you can sort them),
function ?findInterval might be helpful:

i <- findInterval(option.trades$timestamp, stock.trades$timestamp)
cbind(option.trades, stock.trades[i, ])
## timestamp option.price   timestamp stock.price
## 1 2023-08-07 10:23:22  2.5 2023-08-07 10:23:21   102.2
## 3 2023-08-07 10:25:33  2.7 2023-08-07 10:24:57   103.1
## 4 2023-08-07 10:28:41  1.8 2023-08-07 10:28:37   101.8



-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

__
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] Merge with closest (not equal) time stamps

2023-08-08 Thread Naresh Gurbuxani
I was able to adapt your solution using packages with which I am more familiar. 

myres2 <- merge(option.trades, stock.trades, by = "timestamp", all =
TRUE)

myres2[,"stock.timestamp"] <- ifelse(is.na(myres2$stock.price), NA,
myres2$timestamp)

myres2$stock.timestamp <- as.POSIXct(myres2$stock.timestamp, origin =
"1970-01-01")

library(zoo)
myres2$stock.price <- na.locf(myres2$stock.price)
myres2$stock.timestamp <- na.locf(myres2$stock.timestamp)
myres2 <- myres2[!is.na(myres2$option.price),]
row.names(myres2) <- NULL

all.equal(myres, myres2[,c(1, 2, 4, 3)]) # TRUE

This calculation is indeed faster.  

Thanks for your help,
Naresh

> On Aug 8, 2023, at 5:39 AM, Eric Berger  wrote:
> 
> Hi Naresh,
> Perhaps the below is faster than your approach
> 
> library(dplyr)
> library(tidyr)
> merge(option.trades, stock.trades, by="timestamp", all=TRUE) |>
>  dplyr::arrange(timestamp) |>
>  dplyr::mutate(stock.timestamp =
> as.POSIXct(ifelse(is.na(option.price), timestamp, NA))) |>
>  tidyr::fill(stock.price, stock.timestamp) |>
>  dplyr::filter(!is.na(option.price)) |>
>  dplyr::select(1,2,4,3) ->
>  myres2
> 
> identical(myres, myres2)  ## TRUE
> 
> 
> On Tue, Aug 8, 2023 at 5:32 AM Naresh Gurbuxani
>  wrote:
>> 
>> 
>> I have two dataframes, each with a column for timestamp.  I want to
>> merge the two dataframes such that each row from first dataframe
>> is matched with the row in the second dataframe with most recent but
>> preceding timestamp. Here is an example.
>> 
>> option.trades <- data.frame(timestamp = as.POSIXct(c("2023-08-07 10:23:22", 
>> "2023-08-07 10:25:33", "2023-08-07 10:28:41")), option.price = c(2.5, 2.7, 
>> 1.8))
>> 
>> stock.trades <- data.frame(timestamp = as.POSIXct(c("2023-08-07 10:23:21", 
>> "2023-08-07 10:23:34", "2023-08-07 10:24:57", "2023-08-07 10:28:37", 
>> "2023-08-07 10:29:01")), stock.price = c(102.2, 102.9, 103.1, 101.8, 101.7))
>> 
>> stock.trades <- stock.trades[order(stock.trades$timestamp),]
>> 
>> library(plyr)
>> mystock.prices <- ldply(option.trades$timestamp, function(tstamp) 
>> tail(subset(stock.trades, timestamp <= tstamp), 1))
>> names(mystock.prices)[1] <- "stock.timestamp"
>> myres <- cbind(option.trades, mystock.prices)
>> 
>> This method works. But for large dataframes, it is very slow.  Is there
>> a way to speed up the merge?
>> 
>> Thanks,
>> Naresh
>> 
>> __
>> 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] Merge with closest (not equal) time stamps

2023-08-08 Thread Eric Berger
Hi Naresh,
Perhaps the below is faster than your approach

library(dplyr)
library(tidyr)
merge(option.trades, stock.trades, by="timestamp", all=TRUE) |>
  dplyr::arrange(timestamp) |>
  dplyr::mutate(stock.timestamp =
as.POSIXct(ifelse(is.na(option.price), timestamp, NA))) |>
  tidyr::fill(stock.price, stock.timestamp) |>
  dplyr::filter(!is.na(option.price)) |>
  dplyr::select(1,2,4,3) ->
  myres2

identical(myres, myres2)  ## TRUE


On Tue, Aug 8, 2023 at 5:32 AM Naresh Gurbuxani
 wrote:
>
>
> I have two dataframes, each with a column for timestamp.  I want to
> merge the two dataframes such that each row from first dataframe
> is matched with the row in the second dataframe with most recent but
> preceding timestamp. Here is an example.
>
> option.trades <- data.frame(timestamp = as.POSIXct(c("2023-08-07 10:23:22", 
> "2023-08-07 10:25:33", "2023-08-07 10:28:41")), option.price = c(2.5, 2.7, 
> 1.8))
>
> stock.trades <- data.frame(timestamp = as.POSIXct(c("2023-08-07 10:23:21", 
> "2023-08-07 10:23:34", "2023-08-07 10:24:57", "2023-08-07 10:28:37", 
> "2023-08-07 10:29:01")), stock.price = c(102.2, 102.9, 103.1, 101.8, 101.7))
>
> stock.trades <- stock.trades[order(stock.trades$timestamp),]
>
> library(plyr)
> mystock.prices <- ldply(option.trades$timestamp, function(tstamp) 
> tail(subset(stock.trades, timestamp <= tstamp), 1))
> names(mystock.prices)[1] <- "stock.timestamp"
> myres <- cbind(option.trades, mystock.prices)
>
> This method works. But for large dataframes, it is very slow.  Is there
> a way to speed up the merge?
>
> Thanks,
> Naresh
>
> __
> 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] Merge column with characters

2021-11-19 Thread ROSLINAZAIRIMAH BINTI ZAKARIA .
Hi Jim and all,

All functions worked beautifully. I really appreciate your help.

*Thank you and best regards.*




On Fri, Nov 19, 2021 at 4:26 AM Jim Lemon  wrote:

> Hi RosalinaZakaria,
> Talk about using a sledgehammer to crack a nut. In your example the
> two objects are character vectors. How about:
>
> dt_comb1gd <-paste0(dtpaigd,dtpmgd)
>
> Jim
>
> On Fri, Nov 19, 2021 at 2:15 AM ROSLINAZAIRIMAH BINTI ZAKARIA .
>  wrote:
> >
> > Dear all,
> >
> > I try to merge two columns consisting of characters using the 'coalesce'
> > function from dplyr package. However, two data still have not merged,
> data
> > no. 124 1nd 143. Any help is very much appreciated. I provide the data as
> > follows.
> >
> > > dput(dtpaigd)
> > c("C+", "B+", "C+", "B+", "C+", "A-", "A ", "B+", "A-", "C ",
> > "B+", "A-", "B+", "B ", "B ", "B ", "D ", "B+", "B ", "B ", "C+",
> > "B ", "A ", "A-", "B+", "A-", "A ", "B ", "A ", "C ", "B ", "A+",
> > "C ", "B ", "B+", "C+", "B ", "B ", "C+", "B ", "B+", "B ", "A-",
> > "B+", "B ", "A-", "C+", "A ", "A-", "C+", "C+", "B ", "B+", "A-",
> > "A-", "B ", "E ", "A-", "B+", "B ", "A ", "B+", "A-", "A-", "A ",
> > "B+", "A-", "A-", "A-", "B+", "C ", "A-", "A ", "A-", "A ", "A-",
> > "A-", "A ", "A ", "B+", "B ", "A+", "B ", "B ", "B+", "A-", "B+",
> > "B+", "A-", "A-", "B+", "A-", "A-", "A-", "A-", "A-", "A ", "B+",
> > "B ", "A-", "A-", "A-", "A ", "A-", "A-", "B+", "A-", "A ", "B+",
> > "B+", "A-", "B+", "A-", "A ", "B+", "A ", "D ", "A+", "B ", "B+",
> > "A+", "B+", "B ", "", "A ", "A-", "B ", "A ", "B+", "E ", "B ",
> > "B ", "A ", "A-", "B ", "A ", "C+", "B ", "A ", "B+", "B+", "A ",
> > "", "B ")
> > > dput(dtpmgd)
> > c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> > "", "", "", "", "", "", "", "", "", "", "", "", "A-", "", "",
> > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> > "A ", "")
> >
> > dt_comb1gd <- coalesce(dtpaigd, dtpmgd)
> > > dput(dt_comb1gd)
> > c("C+", "B+", "C+", "B+", "C+", "A-", "A ", "B+", "A-", "C ",
> > "B+", "A-", "B+", "B ", "B ", "B ", "D ", "B+", "B ", "B ", "C+",
> > "B ", "A ", "A-", "B+", "A-", "A ", "B ", "A ", "C ", "B ", "A+",
> > "C ", "B ", "B+", "C+", "B ", "B ", "C+", "B ", "B+", "B ", "A-",
> > "B+", "B ", "A-", "C+", "A ", "A-", "C+", "C+", "B ", "B+", "A-",
> > "A-", "B ", "E ", "A-", "B+", "B ", "A ", "B+", "A-", "A-", "A ",
> > "B+", "A-", "A-", "A-", "B+", "C ", "A-", "A ", "A-", "A ", "A-",
> > "A-", "A ", "A ", "B+", "B ", "A+", "B ", "B ", "B+", "A-", "B+",
> > "B+", "A-", "A-", "B+", "A-", "A-", "A-", "A-", "A-", "A ", "B+",
> > "B ", "A-", "A-", "A-", "A ", "A-", "A-", "B+", "A-", "A ", "B+",
> > "B+", "A-", "B+", "A-", "A ", "B+", "A ", "D ", "A+", "B ", "B+",
> > "A+", "B+", "B ", "", "A ", "A-", "B ", "A ", "B+", "E ", "B ",
> > "B ", "A ", "A-", "B ", "A ", "C+", "B ", "A ", "B+", "B+", "A ",
> > "", "B ")
> >
> > *Thank you and best regards.*
> >
> > *RoslinaZakaria*
> >
> > --
> > _*
> > *_
> > _**_*
> > *__
> > _*"Think Green. Keep it on the screen.
> > If printing is
> > necessary, please print it on both sides."
> > *_
> >
> > *The information contained
> > in this e-mail message and any accompanying files is or may be
> > confidential. If you are not the intended recipient, any use,
> > dissemination, reliance, forwarding, printing or copying of this e-mail
> or
> > any attached files is unauthorized. This e-mail is subject to copyright.
> No
> > part of it should be reproduced, adapted or communicated without the
> > written consent of the copyright owner. If you have received this e-mail
> in
> > error please advise the sender immediately by return e-mail or telephone
> > and delete all copies. UMP does not guarantee the accuracy or
> completeness
> > of any information contained in this e-mail or attached files. Internet
> > communications are not secure, therefore UMP does not accept legal
> > responsibility for the contents of this message or attached files.*
> > *Universiti Malaysia Pahang *
> >
> > [[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.
>

-- 
_*
*_
_**_*
*__
_*"Think Green. Keep it on the screen.
If printing is 
necessary, please print it on both sides."
*_

*The information contained 
in this e-mail message and any accompanying 

Re: [R] Merge column with characters

2021-11-18 Thread Jim Lemon
Hi RosalinaZakaria,
Talk about using a sledgehammer to crack a nut. In your example the
two objects are character vectors. How about:

dt_comb1gd <-paste0(dtpaigd,dtpmgd)

Jim

On Fri, Nov 19, 2021 at 2:15 AM ROSLINAZAIRIMAH BINTI ZAKARIA .
 wrote:
>
> Dear all,
>
> I try to merge two columns consisting of characters using the 'coalesce'
> function from dplyr package. However, two data still have not merged, data
> no. 124 1nd 143. Any help is very much appreciated. I provide the data as
> follows.
>
> > dput(dtpaigd)
> c("C+", "B+", "C+", "B+", "C+", "A-", "A ", "B+", "A-", "C ",
> "B+", "A-", "B+", "B ", "B ", "B ", "D ", "B+", "B ", "B ", "C+",
> "B ", "A ", "A-", "B+", "A-", "A ", "B ", "A ", "C ", "B ", "A+",
> "C ", "B ", "B+", "C+", "B ", "B ", "C+", "B ", "B+", "B ", "A-",
> "B+", "B ", "A-", "C+", "A ", "A-", "C+", "C+", "B ", "B+", "A-",
> "A-", "B ", "E ", "A-", "B+", "B ", "A ", "B+", "A-", "A-", "A ",
> "B+", "A-", "A-", "A-", "B+", "C ", "A-", "A ", "A-", "A ", "A-",
> "A-", "A ", "A ", "B+", "B ", "A+", "B ", "B ", "B+", "A-", "B+",
> "B+", "A-", "A-", "B+", "A-", "A-", "A-", "A-", "A-", "A ", "B+",
> "B ", "A-", "A-", "A-", "A ", "A-", "A-", "B+", "A-", "A ", "B+",
> "B+", "A-", "B+", "A-", "A ", "B+", "A ", "D ", "A+", "B ", "B+",
> "A+", "B+", "B ", "", "A ", "A-", "B ", "A ", "B+", "E ", "B ",
> "B ", "A ", "A-", "B ", "A ", "C+", "B ", "A ", "B+", "B+", "A ",
> "", "B ")
> > dput(dtpmgd)
> c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> "", "", "", "", "", "", "", "", "", "", "", "", "A-", "", "",
> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
> "A ", "")
>
> dt_comb1gd <- coalesce(dtpaigd, dtpmgd)
> > dput(dt_comb1gd)
> c("C+", "B+", "C+", "B+", "C+", "A-", "A ", "B+", "A-", "C ",
> "B+", "A-", "B+", "B ", "B ", "B ", "D ", "B+", "B ", "B ", "C+",
> "B ", "A ", "A-", "B+", "A-", "A ", "B ", "A ", "C ", "B ", "A+",
> "C ", "B ", "B+", "C+", "B ", "B ", "C+", "B ", "B+", "B ", "A-",
> "B+", "B ", "A-", "C+", "A ", "A-", "C+", "C+", "B ", "B+", "A-",
> "A-", "B ", "E ", "A-", "B+", "B ", "A ", "B+", "A-", "A-", "A ",
> "B+", "A-", "A-", "A-", "B+", "C ", "A-", "A ", "A-", "A ", "A-",
> "A-", "A ", "A ", "B+", "B ", "A+", "B ", "B ", "B+", "A-", "B+",
> "B+", "A-", "A-", "B+", "A-", "A-", "A-", "A-", "A-", "A ", "B+",
> "B ", "A-", "A-", "A-", "A ", "A-", "A-", "B+", "A-", "A ", "B+",
> "B+", "A-", "B+", "A-", "A ", "B+", "A ", "D ", "A+", "B ", "B+",
> "A+", "B+", "B ", "", "A ", "A-", "B ", "A ", "B+", "E ", "B ",
> "B ", "A ", "A-", "B ", "A ", "C+", "B ", "A ", "B+", "B+", "A ",
> "", "B ")
>
> *Thank you and best regards.*
>
> *RoslinaZakaria*
>
> --
> _*
> *_
> _**_*
> *__
> _*"Think Green. Keep it on the screen.
> If printing is
> necessary, please print it on both sides."
> *_
>
> *The information contained
> in this e-mail message and any accompanying files is or may be
> confidential. If you are not the intended recipient, any use,
> dissemination, reliance, forwarding, printing or copying of this e-mail or
> any attached files is unauthorized. This e-mail is subject to copyright. No
> part of it should be reproduced, adapted or communicated without the
> written consent of the copyright owner. If you have received this e-mail in
> error please advise the sender immediately by return e-mail or telephone
> and delete all copies. UMP does not guarantee the accuracy or completeness
> of any information contained in this e-mail or attached files. Internet
> communications are not secure, therefore UMP does not accept legal
> responsibility for the contents of this message or attached files.*
> *Universiti Malaysia Pahang *
>
> [[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] Merge column with characters

2021-11-18 Thread Rui Barradas

Hello,

Use an index giving the "" positions.


i <- nchar(dtpmgd) == 0L
dtpmgd[i] <- dtpaigd[i]



Or, in one line,


dtpmgd[nchar(dtpmgd) == 0L] <- dtpaigd[nchar(dtpmgd) == 0L]



Hope this helps,

Rui Barradas


Às 07:02 de 18/11/21, ROSLINAZAIRIMAH BINTI ZAKARIA . escreveu:

Dear all,

I try to merge two columns consisting of characters using the 'coalesce'
function from dplyr package. However, two data still have not merged, data
no. 124 1nd 143. Any help is very much appreciated. I provide the data as
follows.


dput(dtpaigd)

c("C+", "B+", "C+", "B+", "C+", "A-", "A ", "B+", "A-", "C ",
"B+", "A-", "B+", "B ", "B ", "B ", "D ", "B+", "B ", "B ", "C+",
"B ", "A ", "A-", "B+", "A-", "A ", "B ", "A ", "C ", "B ", "A+",
"C ", "B ", "B+", "C+", "B ", "B ", "C+", "B ", "B+", "B ", "A-",
"B+", "B ", "A-", "C+", "A ", "A-", "C+", "C+", "B ", "B+", "A-",
"A-", "B ", "E ", "A-", "B+", "B ", "A ", "B+", "A-", "A-", "A ",
"B+", "A-", "A-", "A-", "B+", "C ", "A-", "A ", "A-", "A ", "A-",
"A-", "A ", "A ", "B+", "B ", "A+", "B ", "B ", "B+", "A-", "B+",
"B+", "A-", "A-", "B+", "A-", "A-", "A-", "A-", "A-", "A ", "B+",
"B ", "A-", "A-", "A-", "A ", "A-", "A-", "B+", "A-", "A ", "B+",
"B+", "A-", "B+", "A-", "A ", "B+", "A ", "D ", "A+", "B ", "B+",
"A+", "B+", "B ", "", "A ", "A-", "B ", "A ", "B+", "E ", "B ",
"B ", "A ", "A-", "B ", "A ", "C+", "B ", "A ", "B+", "B+", "A ",
"", "B ")

dput(dtpmgd)

c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "A-", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"A ", "")

dt_comb1gd <- coalesce(dtpaigd, dtpmgd)

dput(dt_comb1gd)

c("C+", "B+", "C+", "B+", "C+", "A-", "A ", "B+", "A-", "C ",
"B+", "A-", "B+", "B ", "B ", "B ", "D ", "B+", "B ", "B ", "C+",
"B ", "A ", "A-", "B+", "A-", "A ", "B ", "A ", "C ", "B ", "A+",
"C ", "B ", "B+", "C+", "B ", "B ", "C+", "B ", "B+", "B ", "A-",
"B+", "B ", "A-", "C+", "A ", "A-", "C+", "C+", "B ", "B+", "A-",
"A-", "B ", "E ", "A-", "B+", "B ", "A ", "B+", "A-", "A-", "A ",
"B+", "A-", "A-", "A-", "B+", "C ", "A-", "A ", "A-", "A ", "A-",
"A-", "A ", "A ", "B+", "B ", "A+", "B ", "B ", "B+", "A-", "B+",
"B+", "A-", "A-", "B+", "A-", "A-", "A-", "A-", "A-", "A ", "B+",
"B ", "A-", "A-", "A-", "A ", "A-", "A-", "B+", "A-", "A ", "B+",
"B+", "A-", "B+", "A-", "A ", "B+", "A ", "D ", "A+", "B ", "B+",
"A+", "B+", "B ", "", "A ", "A-", "B ", "A ", "B+", "E ", "B ",
"B ", "A ", "A-", "B ", "A ", "C+", "B ", "A ", "B+", "B+", "A ",
"", "B ")

*Thank you and best regards.*

*RoslinaZakaria*



__
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] Merge column with characters

2021-11-18 Thread Micha Silver



On 18/11/2021 09:02, ROSLINAZAIRIMAH BINTI ZAKARIA . wrote:

Dear all,

I try to merge two columns consisting of characters using the 'coalesce'
function from dplyr package. However, two data still have not merged, data
no. 124 1nd 143. Any help is very much appreciated. I provide the data as
follows.



Those two indicies are "" in the dtpaigd list. So they're skipped. 
That's what coalesce does.



If you want to push the values from dtpmgd into dtpaigd, wherever 
dtpaigd is an empty string, maybe this will help:



dt_combined <- ifelse(dtpaigd == "", dtpmgd, dtpaigd)



dput(dtpaigd)

c("C+", "B+", "C+", "B+", "C+", "A-", "A ", "B+", "A-", "C ",
"B+", "A-", "B+", "B ", "B ", "B ", "D ", "B+", "B ", "B ", "C+",
"B ", "A ", "A-", "B+", "A-", "A ", "B ", "A ", "C ", "B ", "A+",
"C ", "B ", "B+", "C+", "B ", "B ", "C+", "B ", "B+", "B ", "A-",
"B+", "B ", "A-", "C+", "A ", "A-", "C+", "C+", "B ", "B+", "A-",
"A-", "B ", "E ", "A-", "B+", "B ", "A ", "B+", "A-", "A-", "A ",
"B+", "A-", "A-", "A-", "B+", "C ", "A-", "A ", "A-", "A ", "A-",
"A-", "A ", "A ", "B+", "B ", "A+", "B ", "B ", "B+", "A-", "B+",
"B+", "A-", "A-", "B+", "A-", "A-", "A-", "A-", "A-", "A ", "B+",
"B ", "A-", "A-", "A-", "A ", "A-", "A-", "B+", "A-", "A ", "B+",
"B+", "A-", "B+", "A-", "A ", "B+", "A ", "D ", "A+", "B ", "B+",
"A+", "B+", "B ", "", "A ", "A-", "B ", "A ", "B+", "E ", "B ",
"B ", "A ", "A-", "B ", "A ", "C+", "B ", "A ", "B+", "B+", "A ",
"", "B ")

dput(dtpmgd)

c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "A-", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"A ", "")

dt_comb1gd <- coalesce(dtpaigd, dtpmgd)

dput(dt_comb1gd)

c("C+", "B+", "C+", "B+", "C+", "A-", "A ", "B+", "A-", "C ",
"B+", "A-", "B+", "B ", "B ", "B ", "D ", "B+", "B ", "B ", "C+",
"B ", "A ", "A-", "B+", "A-", "A ", "B ", "A ", "C ", "B ", "A+",
"C ", "B ", "B+", "C+", "B ", "B ", "C+", "B ", "B+", "B ", "A-",
"B+", "B ", "A-", "C+", "A ", "A-", "C+", "C+", "B ", "B+", "A-",
"A-", "B ", "E ", "A-", "B+", "B ", "A ", "B+", "A-", "A-", "A ",
"B+", "A-", "A-", "A-", "B+", "C ", "A-", "A ", "A-", "A ", "A-",
"A-", "A ", "A ", "B+", "B ", "A+", "B ", "B ", "B+", "A-", "B+",
"B+", "A-", "A-", "B+", "A-", "A-", "A-", "A-", "A-", "A ", "B+",
"B ", "A-", "A-", "A-", "A ", "A-", "A-", "B+", "A-", "A ", "B+",
"B+", "A-", "B+", "A-", "A ", "B+", "A ", "D ", "A+", "B ", "B+",
"A+", "B+", "B ", "", "A ", "A-", "B ", "A ", "B+", "E ", "B ",
"B ", "A ", "A-", "B ", "A ", "C+", "B ", "A ", "B+", "B+", "A ",
"", "B ")

*Thank you and best regards.*

*RoslinaZakaria*


--
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918

__
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] Merge the data from multiple text files

2019-01-07 Thread David L Carlson
Thank you. I couldn't have said it better myself. It would probably be simpler 
if you process the lines first to remove duplicates and break compound 
statements into simple statements. Even then it will be a challenge to not end 
up with statements that are internally contradictory, e.g. (A and B) and not(B).

David C

-Original Message-
From: Jeff Newmiller [mailto:jdnew...@dcn.davis.ca.us] 
Sent: Monday, January 7, 2019 11:04 AM
To: Priya Arasu ; Priya Arasu via R-help 
; David L Carlson ; David Winsemius 
; r-help@r-project.org
Subject: Re: [R] Merge the data from multiple text files

I think it is rather presumptuous of you to think that anyone is going to write 
an expression optimizer for some unspecified language on the R-help mailing 
list. I am sure that such tasks can be handled in R, but it is non-trivial and 
the background needed would be very off-topic here.

On January 7, 2019 4:49:04 AM PST, Priya Arasu via R-help 
 wrote:
>Thank you David Winsemius and David L Carlson. 
>@David L Carlson, Thank you for the code. I have one more issue, while
>merging the files. Please advice.For example
>In text file 1:
>A = not(B or C)B = A and CC = D
>In text file 2:
>A = not(C or D) and (D and E)
>
>So when I merge using your code, it merges A = not(B or C) and (D and
>E). How do I merge A as A= not(B or C or D) and (D and E) ?  I also
>have duplicates like A= not(B or C) and not (C or D) instead as A=
>not(B or C or D) ThanksPriya 
>
>On Sunday, 6 January 2019 4:39 AM, David L Carlson 
>wrote:
> 
>
>To expand on David W's answer, here is an approach to your example. If
>you have many text files, you would want to process them together
>rather than individually. You gave us two examples so I'll use those
>and read them from the console using readLines(), but you would use the
>same function to open the files on your computer:
>
>> TF1 <- readLines(n=3)
>A = not(B or C)
>B = A and C
>C = D
>> 
>> TF2 <- readLines(n=2)
>A = D and E
>B = not(D)
>> 
>> TF <- sort(c(TF1, TF2))
>> TF
>[1] "A = D and E"    "A = not(B or C)" "B = A and C"    "B = not(D)"
>[5] "C = D"
>
>Now we have combined the files into a single character vector called TF
>and sorted them. Next we need to parse them into the left and right
>hand sides. We will replace " = " with "\t" (tab) to do that:
>
>> TF.delim <- gsub(" = ", "\t", TF)
>> TF.data <- read.delim(text=TF.delim, header=FALSE, as.is=TRUE)
>> colnames(TF.data) <- c("LHS", "RHS")
>> print(TF.data, right=FALSE)
>  LHS RHS
>1 A  D and E
>2 A  not(B or C)
>3 B  A and C
>4 B  not(D)
>5 C  D
>
>TF.data is a data frame with two columns. The tricky part is to add
>surrounding parentheses to rows 1 and 3 to get your example output:
>
>> paren1 <- grepl("and", TF.data$RHS)
>> paren2 <- !grepl("\\(*\\)", TF.data$RHS)
>> paren <- apply(cbind(paren1, paren2), 1, all)
>> TF.data$RHS[paren] <- paste0("(", TF.data$RHS[paren], ")")
>> print(TF.data, right=FALSE)
>  LHS RHS
>1 A  (D and E)
>2 A  not(B or C)
>3 B  (A and C)
>4 B  not(D)
>5 C  D
>
>The first three lines identify the rows that have the word "and" but do
>not already have parentheses. The fourth line adds the surrounding
>parentheses. Finally we will combine the rows that belong to the same
>LHS value with split and create a list:
>
>> TF.list <- split(TF.data$RHS, TF.data$LHS)
>> TF.list
>$`A`
>[1] "(D and E)"  "not(B or C)"
>
>$B
>[1] "(A and C)" "not(D)"  
>
>$C
>[1] "D"
>
>> TF.and <- lapply(TF.list, paste, collapse=" and ")
>> TF.final <- lapply(names(TF.and), function(x) paste(x, "=",
>TF.and[[x]]))
>> TF.final <- do.call(rbind, TF.final)
>> TF.final
>    [,1]                          
>[1,] "A = (D and E) and not(B or C)"
>[2,] "B = (A and C) and not(D)"
>[3,] "C = D"
>> write(TF.final, file="TF.output.txt")
>
>The text file "TF.output.txt" contains the three lines.
>
>--
>David L. Carlson
>Department of Anthropology
>Texas A University
>
>-Original Message-
>From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of David
>Winsemius
>Sent: Saturday, January 5, 2019 1:12 PM
>
>Subject: Re: [R] Merge the data from multiple text files
>
>
>On 1/5/19 7:28 AM, Priya Arasu via R-help wrote:
>> I have multiple text files, where each file has Boolean

Re: [R] Merge the data from multiple text files

2019-01-07 Thread Jeff Newmiller
I think it is rather presumptuous of you to think that anyone is going to write 
an expression optimizer for some unspecified language on the R-help mailing 
list. I am sure that such tasks can be handled in R, but it is non-trivial and 
the background needed would be very off-topic here.

On January 7, 2019 4:49:04 AM PST, Priya Arasu via R-help 
 wrote:
>Thank you David Winsemius and David L Carlson. 
>@David L Carlson, Thank you for the code. I have one more issue, while
>merging the files. Please advice.For example
>In text file 1:
>A = not(B or C)B = A and CC = D
>In text file 2:
>A = not(C or D) and (D and E)
>
>So when I merge using your code, it merges A = not(B or C) and (D and
>E). How do I merge A as A= not(B or C or D) and (D and E) ?  I also
>have duplicates like A= not(B or C) and not (C or D) instead as A=
>not(B or C or D) ThanksPriya 
>
>On Sunday, 6 January 2019 4:39 AM, David L Carlson 
>wrote:
> 
>
>To expand on David W's answer, here is an approach to your example. If
>you have many text files, you would want to process them together
>rather than individually. You gave us two examples so I'll use those
>and read them from the console using readLines(), but you would use the
>same function to open the files on your computer:
>
>> TF1 <- readLines(n=3)
>A = not(B or C)
>B = A and C
>C = D
>> 
>> TF2 <- readLines(n=2)
>A = D and E
>B = not(D)
>> 
>> TF <- sort(c(TF1, TF2))
>> TF
>[1] "A = D and E"    "A = not(B or C)" "B = A and C"    "B = not(D)"
>[5] "C = D"
>
>Now we have combined the files into a single character vector called TF
>and sorted them. Next we need to parse them into the left and right
>hand sides. We will replace " = " with "\t" (tab) to do that:
>
>> TF.delim <- gsub(" = ", "\t", TF)
>> TF.data <- read.delim(text=TF.delim, header=FALSE, as.is=TRUE)
>> colnames(TF.data) <- c("LHS", "RHS")
>> print(TF.data, right=FALSE)
>  LHS RHS
>1 A  D and E
>2 A  not(B or C)
>3 B  A and C
>4 B  not(D)
>5 C  D
>
>TF.data is a data frame with two columns. The tricky part is to add
>surrounding parentheses to rows 1 and 3 to get your example output:
>
>> paren1 <- grepl("and", TF.data$RHS)
>> paren2 <- !grepl("\\(*\\)", TF.data$RHS)
>> paren <- apply(cbind(paren1, paren2), 1, all)
>> TF.data$RHS[paren] <- paste0("(", TF.data$RHS[paren], ")")
>> print(TF.data, right=FALSE)
>  LHS RHS
>1 A  (D and E)
>2 A  not(B or C)
>3 B  (A and C)
>4 B  not(D)
>5 C  D
>
>The first three lines identify the rows that have the word "and" but do
>not already have parentheses. The fourth line adds the surrounding
>parentheses. Finally we will combine the rows that belong to the same
>LHS value with split and create a list:
>
>> TF.list <- split(TF.data$RHS, TF.data$LHS)
>> TF.list
>$`A`
>[1] "(D and E)"  "not(B or C)"
>
>$B
>[1] "(A and C)" "not(D)"  
>
>$C
>[1] "D"
>
>> TF.and <- lapply(TF.list, paste, collapse=" and ")
>> TF.final <- lapply(names(TF.and), function(x) paste(x, "=",
>TF.and[[x]]))
>> TF.final <- do.call(rbind, TF.final)
>> TF.final
>    [,1]                          
>[1,] "A = (D and E) and not(B or C)"
>[2,] "B = (A and C) and not(D)"
>[3,] "C = D"
>> write(TF.final, file="TF.output.txt")
>
>The text file "TF.output.txt" contains the three lines.
>
>--
>David L. Carlson
>Department of Anthropology
>Texas A University
>
>-Original Message-
>From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of David
>Winsemius
>Sent: Saturday, January 5, 2019 1:12 PM
>
>Subject: Re: [R] Merge the data from multiple text files
>
>
>On 1/5/19 7:28 AM, Priya Arasu via R-help wrote:
>> I have multiple text files, where each file has Boolean rules.
>> Example of my text file 1 and 2
>> Text file 1:
>> A = not(B or C)
>> B = A and C
>> C = D
>> Text file 2:
>> A = D and E
>> B = not(D)
>>
>> I want to merge the contents in text file as follows
>> A = not(B or C) and (D and E)
>> B = not(D) and (A and C)
>> C = D
>> Is there a code in R to merge the data from multiple text files?
>
>
>There is a `merge` function. For this use case you would need to first 
>parse your expressions so that the LHS was in one character column and 
>the RHS was in another character column in each of 2 dataframes. Then 
>merge on the LHS columns and `paste` matching values from the two 
>columns. You will probably need to learn how to use `ifelse` and
>`is.na`.
>
>> Thank you
>> Priya
>>
>>     [[alternative HTML version deleted]]
>
>
>You also need to learn that R is a plain text mailing list and that
>each 
>mail client has its own method for building mail in plain text.

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

__
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] Merge the data from multiple text files

2019-01-07 Thread Priya Arasu via R-help
Thank you David Winsemius and David L Carlson. 
@David L Carlson, Thank you for the code. I have one more issue, while merging 
the files. Please advice.For example
In text file 1:
A = not(B or C)B = A and CC = D
In text file 2:
A = not(C or D) and (D and E)

So when I merge using your code, it merges A = not(B or C) and (D and E). How 
do I merge A as A= not(B or C or D) and (D and E) ?  I also have duplicates 
like A= not(B or C) and not (C or D) instead as A= not(B or C or D) ThanksPriya 

On Sunday, 6 January 2019 4:39 AM, David L Carlson  
wrote:
 

 To expand on David W's answer, here is an approach to your example. If you 
have many text files, you would want to process them together rather than 
individually. You gave us two examples so I'll use those and read them from the 
console using readLines(), but you would use the same function to open the 
files on your computer:

> TF1 <- readLines(n=3)
A = not(B or C)
B = A and C
C = D
> 
> TF2 <- readLines(n=2)
A = D and E
B = not(D)
> 
> TF <- sort(c(TF1, TF2))
> TF
[1] "A = D and E"    "A = not(B or C)" "B = A and C"    "B = not(D)"
[5] "C = D"

Now we have combined the files into a single character vector called TF and 
sorted them. Next we need to parse them into the left and right hand sides. We 
will replace " = " with "\t" (tab) to do that:

> TF.delim <- gsub(" = ", "\t", TF)
> TF.data <- read.delim(text=TF.delim, header=FALSE, as.is=TRUE)
> colnames(TF.data) <- c("LHS", "RHS")
> print(TF.data, right=FALSE)
  LHS RHS
1 A  D and E
2 A  not(B or C)
3 B  A and C
4 B  not(D)
5 C  D

TF.data is a data frame with two columns. The tricky part is to add surrounding 
parentheses to rows 1 and 3 to get your example output:

> paren1 <- grepl("and", TF.data$RHS)
> paren2 <- !grepl("\\(*\\)", TF.data$RHS)
> paren <- apply(cbind(paren1, paren2), 1, all)
> TF.data$RHS[paren] <- paste0("(", TF.data$RHS[paren], ")")
> print(TF.data, right=FALSE)
  LHS RHS
1 A  (D and E)
2 A  not(B or C)
3 B  (A and C)
4 B  not(D)
5 C  D

The first three lines identify the rows that have the word "and" but do not 
already have parentheses. The fourth line adds the surrounding parentheses. 
Finally we will combine the rows that belong to the same LHS value with split 
and create a list:

> TF.list <- split(TF.data$RHS, TF.data$LHS)
> TF.list
$`A`
[1] "(D and E)"  "not(B or C)"

$B
[1] "(A and C)" "not(D)"  

$C
[1] "D"

> TF.and <- lapply(TF.list, paste, collapse=" and ")
> TF.final <- lapply(names(TF.and), function(x) paste(x, "=", TF.and[[x]]))
> TF.final <- do.call(rbind, TF.final)
> TF.final
    [,1]                          
[1,] "A = (D and E) and not(B or C)"
[2,] "B = (A and C) and not(D)"
[3,] "C = D"
> write(TF.final, file="TF.output.txt")

The text file "TF.output.txt" contains the three lines.

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

-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of David Winsemius
Sent: Saturday, January 5, 2019 1:12 PM

Subject: Re: [R] Merge the data from multiple text files


On 1/5/19 7:28 AM, Priya Arasu via R-help wrote:
> I have multiple text files, where each file has Boolean rules.
> Example of my text file 1 and 2
> Text file 1:
> A = not(B or C)
> B = A and C
> C = D
> Text file 2:
> A = D and E
> B = not(D)
>
> I want to merge the contents in text file as follows
> A = not(B or C) and (D and E)
> B = not(D) and (A and C)
> C = D
> Is there a code in R to merge the data from multiple text files?


There is a `merge` function. For this use case you would need to first 
parse your expressions so that the LHS was in one character column and 
the RHS was in another character column in each of 2 dataframes. Then 
merge on the LHS columns and `paste` matching values from the two 
columns. You will probably need to learn how to use `ifelse` and `is.na`.

> Thank you
> Priya
>
>     [[alternative HTML version deleted]]


You also need to learn that R is a plain text mailing list and that each 
mail client has its own method for building mail in plain text.


-- 

David.

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

Re: [R] Merge the data from multiple text files

2019-01-05 Thread David L Carlson
To expand on David W's answer, here is an approach to your example. If you have 
many text files, you would want to process them together rather than 
individually. You gave us two examples so I'll use those and read them from the 
console using readLines(), but you would use the same function to open the 
files on your computer:

> TF1 <- readLines(n=3)
A = not(B or C)
B = A and C
C = D
> 
> TF2 <- readLines(n=2)
A = D and E
B = not(D)
> 
> TF <- sort(c(TF1, TF2))
> TF
[1] "A = D and E" "A = not(B or C)" "B = A and C" "B = not(D)"
[5] "C = D"

Now we have combined the files into a single character vector called TF and 
sorted them. Next we need to parse them into the left and right hand sides. We 
will replace " = " with "\t" (tab) to do that:

> TF.delim <- gsub(" = ", "\t", TF)
> TF.data <- read.delim(text=TF.delim, header=FALSE, as.is=TRUE)
> colnames(TF.data) <- c("LHS", "RHS")
> print(TF.data, right=FALSE)
  LHS RHS
1 A   D and E
2 A   not(B or C)
3 B   A and C
4 B   not(D)
5 C   D

TF.data is a data frame with two columns. The tricky part is to add surrounding 
parentheses to rows 1 and 3 to get your example output:

> paren1 <- grepl("and", TF.data$RHS)
> paren2 <- !grepl("\\(*\\)", TF.data$RHS)
> paren <- apply(cbind(paren1, paren2), 1, all)
> TF.data$RHS[paren] <- paste0("(", TF.data$RHS[paren], ")")
> print(TF.data, right=FALSE)
  LHS RHS
1 A   (D and E)
2 A   not(B or C)
3 B   (A and C)
4 B   not(D)
5 C   D

The first three lines identify the rows that have the word "and" but do not 
already have parentheses. The fourth line adds the surrounding parentheses. 
Finally we will combine the rows that belong to the same LHS value with split 
and create a list:

> TF.list <- split(TF.data$RHS, TF.data$LHS)
> TF.list
$`A`
[1] "(D and E)"   "not(B or C)"

$B
[1] "(A and C)" "not(D)"   

$C
[1] "D"

> TF.and <- lapply(TF.list, paste, collapse=" and ")
> TF.final <- lapply(names(TF.and), function(x) paste(x, "=", TF.and[[x]]))
> TF.final <- do.call(rbind, TF.final)
> TF.final
 [,1]   
[1,] "A = (D and E) and not(B or C)"
[2,] "B = (A and C) and not(D)"
[3,] "C = D"
> write(TF.final, file="TF.output.txt")

The text file "TF.output.txt" contains the three lines.

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

-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of David Winsemius
Sent: Saturday, January 5, 2019 1:12 PM
To: Priya Arasu ; r-help@r-project.org
Subject: Re: [R] Merge the data from multiple text files


On 1/5/19 7:28 AM, Priya Arasu via R-help wrote:
> I have multiple text files, where each file has Boolean rules.
> Example of my text file 1 and 2
> Text file 1:
> A = not(B or C)
> B = A and C
> C = D
> Text file 2:
> A = D and E
> B = not(D)
>
> I want to merge the contents in text file as follows
> A = not(B or C) and (D and E)
> B = not(D) and (A and C)
> C = D
> Is there a code in R to merge the data from multiple text files?


There is a `merge` function. For this use case you would need to first 
parse your expressions so that the LHS was in one character column and 
the RHS was in another character column in each of 2 dataframes. Then 
merge on the LHS columns and `paste` matching values from the two 
columns. You will probably need to learn how to use `ifelse` and `is.na`.

> Thank you
> Priya
>
>   [[alternative HTML version deleted]]


You also need to learn that R is a plain text mailing list and that each 
mail client has its own method for building mail in plain text.


-- 

David.

>
> __
> 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@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] Merge the data from multiple text files

2019-01-05 Thread David Winsemius



On 1/5/19 7:28 AM, Priya Arasu via R-help wrote:

I have multiple text files, where each file has Boolean rules.
Example of my text file 1 and 2
Text file 1:
A = not(B or C)
B = A and C
C = D
Text file 2:
A = D and E
B = not(D)

I want to merge the contents in text file as follows
A = not(B or C) and (D and E)
B = not(D) and (A and C)
C = D
Is there a code in R to merge the data from multiple text files?



There is a `merge` function. For this use case you would need to first 
parse your expressions so that the LHS was in one character column and 
the RHS was in another character column in each of 2 dataframes. Then 
merge on the LHS columns and `paste` matching values from the two 
columns. You will probably need to learn how to use `ifelse` and `is.na`.



Thank you
Priya

[[alternative HTML version deleted]]



You also need to learn that R is a plain text mailing list and that each 
mail client has its own method for building mail in plain text.



--

David.



__
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] merge two data frame based on equal and unequal comparisons

2018-04-18 Thread Ding, Yuan Chun
HI All,

I found that other R users experienced the same problem when using sqldf 
package. An expert provided solution, adding "method="raw" at the end as below, 
one or more of the columns in my data.frame are of class "AsIs" ( I don't know 
what it is) .  When sqldf retrieves the result from SQLite it will be numeric 
or character and since it was originally of class AsIs,  it tries to convert it 
back but R provides no as.AsIs,  so it fails; use method = "raw", so sqldf will 
not try to convert  the results from the database.

DMRlog2pbde47DMS <- sqldf("select * from DMR_log2pbde47 as a left join 
DMS_log2pbde47 as b 
   on a.chrom = b.chromo and a.start <= b.MAPINFO and 
a.end >= b.MAPINFO", method = "raw" )



-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Ding, Yuan Chun
Sent: Wednesday, April 18, 2018 10:38 AM
To: r-help@r-project.org
Subject: [R] merge two data frame based on equal and unequal comparisons

[Attention: This email came from an external source. Do not open attachments or 
click on links from unknown senders or unexpected emails.]





Dear R users,

I need to merge two data frames based on both equal and unequal comparisons.  
The "sqldf" package used to work well , but today, I cannot resolve the 
following error by reinstallation of the sqldf package.   Can anyone suggest a 
different way to perform this kind of merge function?

Thank you,

Ding

> DMRlog2pbde47DMS <- sqldf("select * from DMR_log2pbde47 as a left join 
> DMS_log2pbde47 as b
+on a.chrom = b.chromosome and a.start <= 
+ b.MAPINFO and a.end >= b.MAPINFO" )
Error in get(as.character(FUN), mode = "function", envir = envir) :
  object 'as.AsIs' of mode 'function' was not found



-
-SECURITY/CONFIDENTIALITY WARNING-
This message (and any attachments) are intended solely f...{{dropped:9}}

__
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] Merge by Range in R

2017-09-04 Thread jim holtman
Have you tried 'foverlaps' in the data.table package?


Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Mon, Sep 4, 2017 at 8:31 AM, Mohammad Tanvir Ahamed via R-help <
r-help@r-project.org> wrote:

> Hi,
> I have two big data set.
>
> data _1 :
> > dim(data_1)
> [1] 15820 5
>
> > head(data_1)
>Chromosome  StartEndFeature GroupA_3
> 1:   chr1 521369  75 chr1-00010.170
> 2:   chr1 750001  80 chr1-0002   -0.086
> 3:   chr1 81  85 chr1-00030.006
> 4:   chr1 850001  90 chr1-00040.050
> 5:   chr1 91  95 chr1-00050.062
> 6:   chr1 950001 100chr1-0006   -0.016
>
> data_2:
> > dim(data_2)
> [1] 470870 5
>
> > head(data_2)
>Chromosome Start   EndFeature GroupA_3
> 1:   chr1 15864 15865 cg138693410.207
> 2:   chr1 18826 18827 cg14008030   -0.288
> 3:   chr1 29406 29407 cg12045430   -0.331
> 4:   chr1 29424 29425 cg20826792   -0.074
> 5:   chr1 29434 29435 cg003816040.141
> 6:   chr1 68848 68849 cg20253340   -0.458
>
>
> What I want to do :
> Based on column name "Chromosome", "Start" and "End" of two data set ,   I
> want to find which row (preciously "Feature") of data_2 is in every range (
> between "Start" and "End") of data_1 ? Also "Chromosome" column element
> should be match between two data set.
>
> I have tried "GenomicRanges" packages describe in the post
> https://stackoverflow.com/questions/11892241/merge-by-
> range-in-r-applying-loops
> But i was not successful. Can any one please help me to do this fast, as
> the data is very big ?
> Thanks in advance.
>
>
> Regards.
> Tanvir Ahamed Stockholm, Sweden |  mashra...@yahoo.com
>
> [[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.


Re: [R] Merge list element by name

2017-04-13 Thread David Winsemius

> On Apr 13, 2017, at 7:56 AM, Mohammad Tanvir Ahamed via R-help 
>  wrote:
> 
> Hi, 
> I have a list like 
> kk<- list (a = 1:5, b = 6:10, c = 4:11)
> 
> Now i want to merger (Union) the list element "a" and "c" by name .
> 
> My expected outcome is 
> kk1<- list(a_c = 1:11, b = 6:10)
> 
> 
> I can do it with several lines of code. But can any one have idea to do 
> efficiently/ quickly on a big data with less code.

Given that you used the term in your problem specification, it's hard to 
understand how you missed finding the `union` function :

kk1 <- with( kk, list( a_c <- union(a,c), b=b)  )
 kk1
#-
[[1]]
 [1]  1  2  3  4  5 11 10  9  8  7  6

$b
[1] 10  9  8  7  6
#-

(It's not a `merge`.)

I thought this would remove any factor-stored information, since the code 
(before bytecode compilation) is:

function (x, y) 
unique(c(as.vector(x), as.vector(y)))

But 'as.vector' does the equivalent of 'as.character' on factor vectors, so you 
would be "safe" from that concern.


-- 
David.

> Thanks in advance. 
> 
> Tanvir Ahamed 
> Göteborg, Sweden  |  mashra...@yahoo.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.

David Winsemius
Alameda, CA, USA

__
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] Merge selected list element by name

2017-04-13 Thread Bert Gunter
So do you mean like this:

> kk<- list (a = 1:5, b = 6:10, c = 4:11)
> kk1 <- list(union(kk$a,kk$c),kk$b)
> names(kk1)<- c(paste(names(kk)[c(1,3)],collapse="_"),names(kk)[2])
> kk1
$a_c
 [1]  1  2  3  4  5  6  7  8  9 10 11

$b
[1]  6  7  8  9 10

??

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 Thu, Apr 13, 2017 at 10:33 AM, Mohammad Tanvir Ahamed via R-help
<r-help@r-project.org> wrote:
> Thanks for your code.
> But this is not the way i am expecting .
>
> I want to merge (Union) "a" and "c" and my expected output is
>
>> kk1
> $a_c
>  [1]  1  2  3  4  5  6  7  8  9 10 11
>
> $b
> [1]  6  7  8  9 10
>
>
> Note : I worte the code kk1<- list(a_c = 1:11, b = 6:10) just to show by 
> expected outcome. But i am expecting the resulting code will naming "a_c" by 
> itself also.
> Hope i can make clear about problem.
>
>
>
>
> Tanvir Ahamed
> Göteborg, Sweden  |  mashra...@yahoo.com
>
>
>
> 
> From: Rui Barradas <ruipbarra...@sapo.pt>
>
> l...@r-project.org>
> Sent: Thursday, 13 April 2017, 18:37
> Subject: Re: [R] Merge selected list element by name
>
>
>
> Hello,
>
> There's no need to send the same question twice, we've got it at the
> first try.
> Maybe I don't understand but is this it?
>
> kk1 <- list(a_c = union(kk$a, kk$c), b = kk$b)
> kk1
> $a_c
>   [1]  1  2  3  4  5  6  7  8  9 10 11
>
> $b
> [1]  6  7  8  9 10
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> Em 13-04-2017 15:59, Mohammad Tanvir Ahamed via R-help escreveu:
>>
>> Hi,
>>
>> I have a list like
>>
>> kk<- list (a = 1:5, b = 6:10, c = 4:11)
>>
>>
>> Now i want to merger (Union) the list element "a" and "c" by name .
>>
>>
>> My expected outcome is
>>
>> kk1<- list(a_c = 1:11, b = 6:10)
>>
>>
>>
>> I can do it with several lines of code. But can any one have idea to do 
>> efficiently/ quickly on a big data with less code.
>>
>> Thanks in advance.
>>
>>
>> Tanvir Ahamed
>>
>
>>
>> __
>> 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@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] Merge selected list element by name

2017-04-13 Thread Mohammad Tanvir Ahamed via R-help
Thanks for your code. 
But this is not the way i am expecting . 

I want to merge (Union) "a" and "c" and my expected output is 

> kk1
$a_c
 [1]  1  2  3  4  5  6  7  8  9 10 11

$b
[1]  6  7  8  9 10


Note : I worte the code kk1<- list(a_c = 1:11, b = 6:10) just to show by 
expected outcome. But i am expecting the resulting code will naming "a_c" by 
itself also.
Hope i can make clear about problem. 

 

 
Tanvir Ahamed 
Göteborg, Sweden  |  mashra...@yahoo.com 




From: Rui Barradas <ruipbarra...@sapo.pt>

l...@r-project.org> 
Sent: Thursday, 13 April 2017, 18:37
Subject: Re: [R] Merge selected list element by name



Hello,

There's no need to send the same question twice, we've got it at the 
first try.
Maybe I don't understand but is this it?

kk1 <- list(a_c = union(kk$a, kk$c), b = kk$b)
kk1
$a_c
  [1]  1  2  3  4  5  6  7  8  9 10 11

$b
[1]  6  7  8  9 10


Hope this helps,

Rui Barradas


Em 13-04-2017 15:59, Mohammad Tanvir Ahamed via R-help escreveu:
>
> Hi,
>
> I have a list like
>
> kk<- list (a = 1:5, b = 6:10, c = 4:11)
>
>
> Now i want to merger (Union) the list element "a" and "c" by name .
>
>
> My expected outcome is
>
> kk1<- list(a_c = 1:11, b = 6:10)
>
>
>
> I can do it with several lines of code. But can any one have idea to do 
> efficiently/ quickly on a big data with less code.
>
> Thanks in advance.
>
>
> Tanvir Ahamed
>

>
> __
> 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] Merge selected list element by name

2017-04-13 Thread Rui Barradas

Hello,

There's no need to send the same question twice, we've got it at the 
first try.

Maybe I don't understand but is this it?

kk1 <- list(a_c = union(kk$a, kk$c), b = kk$b)
kk1
$a_c
 [1]  1  2  3  4  5  6  7  8  9 10 11

$b
[1]  6  7  8  9 10


Hope this helps,

Rui Barradas

Em 13-04-2017 15:59, Mohammad Tanvir Ahamed via R-help escreveu:


Hi,

I have a list like

kk<- list (a = 1:5, b = 6:10, c = 4:11)


Now i want to merger (Union) the list element "a" and "c" by name .


My expected outcome is

kk1<- list(a_c = 1:11, b = 6:10)



I can do it with several lines of code. But can any one have idea to do 
efficiently/ quickly on a big data with less code.

Thanks in advance.


Tanvir Ahamed

Göteborg, Sweden  |  mashra...@yahoo.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@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] Merge data by coordinates

2016-10-18 Thread Michal Kubista
Dear Milu,
If your objective is to match the places from one table to the nearest
place in the second table, you can generally use knn algorithm for 1
nearest neighbourhood.
But please, check what David suggests first.

Best regards,
Michal

2016-10-16 19:24 GMT+02:00 David Winsemius :

>
> > On Oct 16, 2016, at 6:32 AM, Miluji Sb  wrote:
> >
> > Dear all,
> >
> > I have two dataframe 1 by latitude and longitude but they always do not
> > match. Is it possible to merge them (e.g. nearest distance)?
> >
> > # Dataframe 1
> > structure(list(lat = c(54L, 55L, 51L, 54L, 53L, 50L, 47L, 51L,
> > 49L, 54L), lon = c(14L, 8L, 15L, 7L, 6L, 5L, 13L, 5L, 13L, 11L
> > ), PPP2000_40 = c(4606, 6575, 6593, 7431, 9393, 10773, 11716,
> > 12226, 13544, 14526)), .Names = c("lat", "lon", "PPP2000_40"), row.names
> =
> > c(6764L,
> > 8796L, 8901L, 9611L, 11649L, 12819L, 13763L, 14389L, 15641L,
> > 16571L), class = "data.frame")
> >
> > # Dataframe 2
> > structure(list(lat = c(47, 47, 47, 47, 47, 47, 48, 48, 48, 48
> > ), lon = c(7, 8, 9, 10, 11, 12, 7, 8, 9, 10), GDP = c(19.09982,
> > 13.31977, 14.95925, 6.8575635, 23.334565, 6.485748, 24.01197,
> > 14.30393075, 21.33759675, 9.71803675)), .Names = c("lat", "lon",
> > "GDP"), row.names = c(NA, 10L), class = "data.frame")
>
> I think you should first do this:
>
> plot(d1$lat,d1$lon)
> points(d2$lat,d2$lon, col="red")
>
> And then respond to my suggestion that this is not a well-posed computing
> problem. Explain why the red dots should have a 1-1 relationship with the
> black dots.
>
>
> --
> David.
>
> >
> > Thank you so much!
> >
> > Sincerely,
> >
> > Milu
> >
> >   [[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.
>
> David Winsemius
> Alameda, CA, USA
>
> __
> 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.


Re: [R] Merge data by coordinates

2016-10-16 Thread David Winsemius

> On Oct 16, 2016, at 6:32 AM, Miluji Sb  wrote:
> 
> Dear all,
> 
> I have two dataframe 1 by latitude and longitude but they always do not
> match. Is it possible to merge them (e.g. nearest distance)?
> 
> # Dataframe 1
> structure(list(lat = c(54L, 55L, 51L, 54L, 53L, 50L, 47L, 51L,
> 49L, 54L), lon = c(14L, 8L, 15L, 7L, 6L, 5L, 13L, 5L, 13L, 11L
> ), PPP2000_40 = c(4606, 6575, 6593, 7431, 9393, 10773, 11716,
> 12226, 13544, 14526)), .Names = c("lat", "lon", "PPP2000_40"), row.names =
> c(6764L,
> 8796L, 8901L, 9611L, 11649L, 12819L, 13763L, 14389L, 15641L,
> 16571L), class = "data.frame")
> 
> # Dataframe 2
> structure(list(lat = c(47, 47, 47, 47, 47, 47, 48, 48, 48, 48
> ), lon = c(7, 8, 9, 10, 11, 12, 7, 8, 9, 10), GDP = c(19.09982,
> 13.31977, 14.95925, 6.8575635, 23.334565, 6.485748, 24.01197,
> 14.30393075, 21.33759675, 9.71803675)), .Names = c("lat", "lon",
> "GDP"), row.names = c(NA, 10L), class = "data.frame")

I think you should first do this:

plot(d1$lat,d1$lon)
points(d2$lat,d2$lon, col="red")

And then respond to my suggestion that this is not a well-posed computing 
problem. Explain why the red dots should have a 1-1 relationship with the black 
dots.


-- 
David.

> 
> Thank you so much!
> 
> Sincerely,
> 
> Milu
> 
>   [[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.

David Winsemius
Alameda, CA, USA

__
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] Merge several datasets into one

2016-07-01 Thread Lida Zeighami
Hi Lily,

I think below codes can work:

 f<- list.files("D:/output/test/your folde
rname",full.names=TRUE,recursive=TRUE)

files<- grep(".csv", f)

files_merge<- data.frame()
for (i in 1:length(f[files])){
  data<- read.csv(file=f[files][i],header=TRUE, sep=",")
  files_merge<-  rbind(files_merge,data)
}

Best,
Lida

On Thu, Jun 30, 2016 at 2:26 PM, lily li  wrote:

> Hi R users,
>
> I'd like to ask that how to merge several datasets into one in R? I put
> these csv files in one folder, and use the lapply function, but it says
> that cannot open file 'xx.csv'. These files have different names, but end
> with .csv extension, and the files have the same header. Thanks for your
> help.
>
> [[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.


Re: [R] Merge several datasets into one

2016-07-01 Thread ruipbarradas
Hello,

Maybe something like this.

fls <- list.files(pattern = "*.csv")
dat.list <- lapply(fls, read.csv)
dat <- do.call(rbind, dat.list)

Hope this helps,

Rui Barradas
 

Citando lily li :

> Hi R users,
>
> I'd like to ask that how to merge several datasets into one in R? I put
> these csv files in one folder, and use the lapply function, but it says
> that cannot open file 'xx.csv'. These files have different names, but end
> with .csv extension, and the files have the same header. Thanks for your
> help.
>
>         [[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.htmland 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.

Re: [R] Merge several datasets into one

2016-06-30 Thread Lists
Lily Li  wrote :

I think you are going to have to give us some more detail. What commands did 
you execute? what are the names of the .csv files in your directory? Can you 
read one of them as asingle read.csv?

> Hi R users,
> 
> I'd like to ask that how to merge several datasets into one in R? I put
> these csv files in one folder, and use the lapply function, but it says
> that cannot open file 'xx.csv'. These files have different names, but end
> with .csv extension, and the files have the same header. Thanks for your
> help.
> 
>   [[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] Merge several datasets into one

2016-06-30 Thread Bert Gunter
Lily:

If you mean that you have several csv files in a directory/folder on
your computer and you are using lapply() to do something with them,
then you do not have a clue about how R works and you need to go
through some tutorials to learn. There are many good ones on the web.
Some recommendations can be found here:
https://www.rstudio.com/online-learning/#R

We do expect posters here to have made some minimal effort to learn R
basics before posting.

If I have misunderstood, then you will either have to wait for a reply
from someone with greater insight or explain yourself more clearly.

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 Thu, Jun 30, 2016 at 12:26 PM, lily li  wrote:
> Hi R users,
>
> I'd like to ask that how to merge several datasets into one in R? I put
> these csv files in one folder, and use the lapply function, but it says
> that cannot open file 'xx.csv'. These files have different names, but end
> with .csv extension, and the files have the same header. Thanks for your
> help.
>
> [[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] Merge sort

2016-04-20 Thread Duncan Murdoch

On 20/04/2016 7:38 AM, Gaston wrote:

I indeed used is.na() to check length, as I was not sure weather
lenght() was a simple query or would go through the whole vector to
count the elements.


length() is a simple query, and is very fast.  The other problem in your 
approach (which may not be a problem with your current data) is that NA 
is commonly used as an element of a vector to represent a missing value.




So to sum up, function calls are expensive, therefore recursion should
be avoided, and growing the size of a vector (which is probably
reassigning and copying?) is also expensive.


"Avoided" may be too strong:  speed isn't always a concern, sometimes 
clarity is more important.  Growing vectors is definitely expensive.


Duncan Murdoch



Thank you for your help!



On 04/19/2016 11:51 PM, Duncan Murdoch wrote:

On 19/04/2016 3:39 PM, Gaston wrote:

Hello everyone,

I am learning R since recently, and as a small exercise I wanted to
write a recursive mergesort. I was extremely surprised to discover that
my sorting, although operational, is deeply inefficient in time. Here is
my code :


merge <- function(x,y){
if (is.na(x[1])) return(y)
else if (is.na(y[1])) return(x)
else if (x[1] 0 && length(x2) > 0) {
 # compare the first values
 if (x1[1] < x2[1]) {
   result[i + 1] <- x1[1]
   x1 <- x1[-1]
 } else {
   result[i + 1] <- x2[1]
   x2 <- x2[-1]
 }
 i <- i + 1
   }

   # put the smaller one into the result
   # delete it from whichever vector it came from
   # repeat until one of x1 or x2 is empty
   # copy both vectors (one is empty!) onto the end of the results
   result <- c(result, x1, x2)
   result
}

If I were going for speed, I wouldn't modify the x1 and x2 vectors,
and I'd pre-allocate result to the appropriate length, rather than
growing it in the while loop.  But that was a different class!

Duncan Murdoch




__
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] Merge sort

2016-04-20 Thread Gaston
I indeed used is.na() to check length, as I was not sure weather 
lenght() was a simple query or would go through the whole vector to 
count the elements.


So to sum up, function calls are expensive, therefore recursion should 
be avoided, and growing the size of a vector (which is probably 
reassigning and copying?) is also expensive.


Thank you for your help!



On 04/19/2016 11:51 PM, Duncan Murdoch wrote:

On 19/04/2016 3:39 PM, Gaston wrote:

Hello everyone,

I am learning R since recently, and as a small exercise I wanted to
write a recursive mergesort. I was extremely surprised to discover that
my sorting, although operational, is deeply inefficient in time. Here is
my code :


merge <- function(x,y){
   if (is.na(x[1])) return(y)
   else if (is.na(y[1])) return(x)
   else if (x[1] 0 && length(x2) > 0) {
# compare the first values
if (x1[1] < x2[1]) {
  result[i + 1] <- x1[1]
  x1 <- x1[-1]
} else {
  result[i + 1] <- x2[1]
  x2 <- x2[-1]
}
i <- i + 1
  }

  # put the smaller one into the result
  # delete it from whichever vector it came from
  # repeat until one of x1 or x2 is empty
  # copy both vectors (one is empty!) onto the end of the results
  result <- c(result, x1, x2)
  result
}

If I were going for speed, I wouldn't modify the x1 and x2 vectors, 
and I'd pre-allocate result to the appropriate length, rather than 
growing it in the while loop.  But that was a different class!


Duncan Murdoch


__
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] Merge sort

2016-04-19 Thread Duncan Murdoch

On 19/04/2016 3:39 PM, Gaston wrote:

Hello everyone,

I am learning R since recently, and as a small exercise I wanted to
write a recursive mergesort. I was extremely surprised to discover that
my sorting, although operational, is deeply inefficient in time. Here is
my code :


merge <- function(x,y){
   if (is.na(x[1])) return(y)
   else if (is.na(y[1])) return(x)
   else if (x[1] 0 && length(x2) > 0) {
# compare the first values
if (x1[1] < x2[1]) {
  result[i + 1] <- x1[1]
  x1 <- x1[-1]
} else {
  result[i + 1] <- x2[1]
  x2 <- x2[-1]
}
i <- i + 1
  }

  # put the smaller one into the result
  # delete it from whichever vector it came from
  # repeat until one of x1 or x2 is empty
  # copy both vectors (one is empty!) onto the end of the results
  result <- c(result, x1, x2)
  result
}

If I were going for speed, I wouldn't modify the x1 and x2 vectors, and 
I'd pre-allocate result to the appropriate length, rather than growing 
it in the while loop.  But that was a different class!


Duncan Murdoch

__
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] merge small clusters in R

2016-03-19 Thread Boris Steipe
This is not a well defined question, until your notions of "small" and 
"nearest" are defined. In your specific example

   rect.hclust(hc, k = 3, border = 2:5)

... will do what you are asking for. This is not likely to work in the general 
case - imagine that your cluster of size two only meets the others at the root: 
in that case you would be distorting the result significantly if you were to 
merge it in with another cluster, simply based on membership size. That said, 
perhaps the package dynamicTreeCut will help you find cuts in a dendrogram that 
more closely match your intuition.

B.


On Mar 16, 2016, at 11:38 AM, Sheila the angel  wrote:

> In R, I have cut a dendrogram into clusters. However some of the clusters
> have only few samples. How can I merge the small clusters with nearest big
> cuter.
> 
> hc <- hclust(dist(USArrests))
> plot(hc, cex = 0.6)
> rect.hclust(hc, k = 4, border = 2:5)
> 
> It gives one cluster with only 2 samples. How can I merge it with nearest
> cluster?
> 
> Thanks
> S.
> 
>   [[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] merge xts objects with different data types ?

2015-09-03 Thread Jeff Newmiller
The root of your problems lie in your assumption that xts variables act like 
data frames. Instead they are matrices with an index attribute. All values in a 
matrix must be of the same storage mode. 

You might want to investigate the data.table package. It is not a time series 
object but you can still handle time series operations and you can have any 
type of data.
---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On September 3, 2015 5:40:36 PM PDT, ce  wrote:
>
>Hello
>
>Let's say some questions about merging  xts variables :
>
>a<- xts("abc", Sys.Date())
>b <- xts("def", Sys.Date())
>c <- xts(1, Sys.Date())
>
>> merge(a,b)
>   a b
>2015-09-03 "abc" "def"
>> merge(a,b,c)
>a  b c
>2015-09-03 NA NA 1
>Warning messages:
>1: In merge.xts(a, b, c) : NAs introduced by coercion
>2: In merge.xts(a, b, c) : NAs introduced by coercion
>3: In merge.xts(a, b, c) : NAs introduced by coercion
>4: In merge.xts(a, b, c) : NAs introduced by coercion
>
>How I can merge  a, b ,c correctly ? Another example is with Binary
>variables :
>
>> e<- xts(TRUE, Sys.Date())
>> e
>   [,1]
>2015-09-03 TRUE
>> merge(e,b)
>   e  b
>2015-09-03 1 NA
>Warning message:
>In merge.xts(e, b) : NAs introduced by coercion
>
>
>My second question is how I can convert an xts object to factor :
>
>> d <- merge(a,b)
>> d
>   a b
>2015-09-03 "abc" "def"
>> factor(d, levels = c("abc","def"))
>  a   b 
>abc def 
>Levels: abc def
>
>Date disappears here?
>
>Thanks for your help
>ce
>
>__
>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] merge xts objects with different data types ?

2015-09-03 Thread Joshua Ulrich
On Thu, Sep 3, 2015 at 7:40 PM, ce  wrote:
>
> Hello
>
> Let's say some questions about merging  xts variables :
>
> a<- xts("abc", Sys.Date())
> b <- xts("def", Sys.Date())
> c <- xts(1, Sys.Date())
>
>> merge(a,b)
>a b
> 2015-09-03 "abc" "def"
>> merge(a,b,c)
> a  b c
> 2015-09-03 NA NA 1
> Warning messages:
> 1: In merge.xts(a, b, c) : NAs introduced by coercion
> 2: In merge.xts(a, b, c) : NAs introduced by coercion
> 3: In merge.xts(a, b, c) : NAs introduced by coercion
> 4: In merge.xts(a, b, c) : NAs introduced by coercion
>
> How I can merge  a, b ,c correctly ? Another example is with Binary variables 
> :
>
>> e<- xts(TRUE, Sys.Date())
>> e
>[,1]
> 2015-09-03 TRUE
>> merge(e,b)
>e  b
> 2015-09-03 1 NA
> Warning message:
> In merge.xts(e, b) : NAs introduced by coercion
>
xts objects are a matrix with an index attribute, and you can't mix
types in a matrix.  So all the objects you merge need to be the same
type.  For objects a, b, and c: you need to convert c to character:
storage.mode(c) <- "character"

Also, merge.xts currently only supports n-way merges integer, numeric,
and logical types (see https://github.com/joshuaulrich/xts/issues/44).
So you need to merge a and b first, then merge that result with c.
You can do that by calling merge.xts many times, or via Reduce:
merge(merge(a,b),c)
Reduce(merge, list(a,b,c))

>
> My second question is how I can convert an xts object to factor :
>
>> d <- merge(a,b)
>> d
>a b
> 2015-09-03 "abc" "def"
>> factor(d, levels = c("abc","def"))
>   a   b
> abc def
> Levels: abc def
>
> Date disappears here?
>
I'm not sure what you expected; factors don't have dates.  It's not
clear what you're trying to do here.

> Thanks for your help
> ce
>
> __
> 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.



-- 
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.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.


Re: [R] merge xts objects with different data types ?

2015-09-03 Thread ce

Thanks a lot Jeff and Joshua,
I can see why data.table can be used with dates ( still more work to make it 
work :)  )

For factors, I was hoping to get a time series of a factor ( values can be only 
"abc" or "def" category ) :

2015-09-04 "abc"
2015-09-05 "abc"
2015-09-06 "def"
2015-09-07 "abc"


-Original Message-
From: "Joshua Ulrich" [josh.m.ulr...@gmail.com]
Date: 09/03/2015 09:43 PM
To: "ce" <zadi...@excite.com>
CC: "R-Help" <r-help@r-project.org>
Subject: Re: [R] merge xts objects with different data types ?

On Thu, Sep 3, 2015 at 7:40 PM, ce <zadi...@excite.com> wrote:
>
> Hello
>
> Let's say some questions about merging  xts variables :
>
> a<- xts("abc", Sys.Date())
> b <- xts("def", Sys.Date())
> c <- xts(1, Sys.Date())
>
>> merge(a,b)
>a b
> 2015-09-03 "abc" "def"
>> merge(a,b,c)
> a  b c
> 2015-09-03 NA NA 1
> Warning messages:
> 1: In merge.xts(a, b, c) : NAs introduced by coercion
> 2: In merge.xts(a, b, c) : NAs introduced by coercion
> 3: In merge.xts(a, b, c) : NAs introduced by coercion
> 4: In merge.xts(a, b, c) : NAs introduced by coercion
>
> How I can merge  a, b ,c correctly ? Another example is with Binary variables 
> :
>
>> e<- xts(TRUE, Sys.Date())
>> e
>[,1]
> 2015-09-03 TRUE
>> merge(e,b)
>e  b
> 2015-09-03 1 NA
> Warning message:
> In merge.xts(e, b) : NAs introduced by coercion
>
xts objects are a matrix with an index attribute, and you can't mix
types in a matrix.  So all the objects you merge need to be the same
type.  For objects a, b, and c: you need to convert c to character:
storage.mode(c) <- "character"

Also, merge.xts currently only supports n-way merges integer, numeric,
and logical types (see https://github.com/joshuaulrich/xts/issues/44).
So you need to merge a and b first, then merge that result with c.
You can do that by calling merge.xts many times, or via Reduce:
merge(merge(a,b),c)
Reduce(merge, list(a,b,c))

>
> My second question is how I can convert an xts object to factor :
>
>> d <- merge(a,b)
>> d
>a b
> 2015-09-03 "abc" "def"
>> factor(d, levels = c("abc","def"))
>   a   b
> abc def
> Levels: abc def
>
> Date disappears here?
>
I'm not sure what you expected; factors don't have dates.  It's not
clear what you're trying to do here.

> Thanks for your help
> ce
>
> __
> 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.



-- 
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.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.


Re: [R] merge: right set overwrite left set

2015-07-13 Thread aldi
Thank you Ista,
Your solution is smart, by sub-setting from x.HHu.map data only HHid, 
position as indices (because they are unique) for the merge, and any 
extra columns in x.HHu.map that are not present in y.HHo,map, thus when 
the merge is done with option all=T, will work among the two sets of 
data, without creating .x and .y when the variables are in common in two 
sets.

With best wishes,
Aldi

  ## find indv columns in x.HHu.map that don't exist in y.HHo.map
  x.HHu.map - x.HHu.map[
+ c(HHid,
+   position,
+   names(x.HHu.map)[
+!names(x.HHu.map)
+%in% names(y.HHo.map)]
+   )]
  ## merge, adding extra column from x.HHu.map
  zzz - merge(y.HHo.map, x.HHu.map, by=c('HHid', 'position'), all=T)
  ## order by HHid
  zzz - zzz[order(zzz$HHid),]
  zzz
HHid position indv1 indv2 ind3
1   HH1   10 2 00
2  HH10  101NA 20
3  HH11  111NANA   NA
4   HH2   20 0 20
5   HH3   30 0 10
6   HH4   42NANA0
7   HH5   55 2 20
8   HH6   66NANA   NA
9   HH7   75NANA   NA
10  HH8   81NANA   NA
11  HH9   92NANA   NA


On 7/12/2015 12:06 PM, Ista Zahn wrote:
 I think this does what you want:

 ## find idiv coloumns in x.HHu.map that don't exist in y.HHo.map
 x.HHu.map - x.HHu.map[
  c(HHid,
position,
names(x.HHu.map)[
 !names(x.HHu.map)
 %in% names(y.HHo.map)]
)]
 ## merge, adding extra column from x.HHu.map
 zzz - merge(y.HHo.map, x.HHu.map, by=c('HHid', 'position'), all=T)
 ## order by HHid
 zzz - zzz[order(zzz$HHid),]

 Best,
 Ista

 On Sun, Jul 12, 2015 at 10:45 AM, aldi a...@dsgmail.wustl.edu wrote:
 Hi,
 I have two sets of data x.HHu and y.HHo, rows are IDs and columns are
 individuals. I do not know in advance indv or HHid, both of them will be
 captured from the data. As the y.HHo set updates, y.HHo set has better
 information then x.HHu set. Thus I want a merge where right set
 overwrites left set info based on HHid, i.e. to overwrite x.HHu set with
 y.HHo set but keep any extra info from the x.HHu set that is not present
 in y.HHo set.
 HHids will be complete based on z.map, with the corresponding positions.
 I am having trouble with the part after this line: ###
 +++
 I am thinking that I am creating new columns position indv1 and
 indv2, but R is interpreting them as row information.
 See the expected final table at the end. HHid is common, indv3 is from
 x.HHu, and the rest position and indv1 and indv2 are from y.HHo
 Any suggestions are appreciated.
 Thank you in advance,
 Aldi

 x.HHu- data.frame(
  HHid = c( 'HH1', 'HH2', 'HH3', 'HH4', 'HH5', 'HH10')
, indv1 = c( 2, 0, 2 , 0, 2, 0)
, indv2 = c( 0, NA, 2, 2, 2, 2)
, ind3 = c( 0, 0, 0, 0, 0, 0)
)
 ### the HHo data will be the top set to overwrite any HHu data, when
 they exist, thinking that HHo are better than HHu results
 ### when they are available

 y.HHo-data.frame(HHid=c('HH1', 'HH2','HH5', 'HH3', 'HH10')
, indv1 = c(2, 0, 2, 0, NA)
, indv2 = c(0, 2, 2, 1, 2)
)

 z.map-data.frame(HHid = c('HH1', 'HH2', 'HH3', 'HH4', 'HH5',
 'HH6','HH8', 'HH7', 'HH9', 'HH10', 'HH11')
   , position= c(10,20,30,42,55,66,81,75,92,101,111)
   )
 ### see objects
 x.HHu
 y.HHo
 z.map
 ### now sort the map by position, this sorted map will be used to sort
 finally all data
 z.map-z.map[with(z.map, order(position)), ]
 z.map

 ### First I introduce position to both sets so I can sort them in
 advance by position.
 x.HHu.map -merge( z.map, x.HHu, by='HHid', all=T)
 x.HHu.map-x.HHu.map[with(x.HHu.map, order(position)), ]
 x.HHu.map

 y.HHo.map -merge( z.map, y.HHo, by='HHid', all= T)
 y.HHo.map-y.HHo.map[with(y.HHo.map, order(position)), ]
 y.HHo.map

 ### now merge HHu  and HHo  with the hope to overwrite the HHu set with
 HHo wherever they overlap by column names.
 zzz - merge(x.HHu.map, y.HHo.map, by='HHid', all=T)
 zzz
 ### find common variable names in two sets

 commonNames - names(x.impu.map)[which(colnames(x.impu.map) %in%
 colnames(y.geno.map))]

 ## remove HHid wich is common for x and y, but work with the rest of columns
 commonNames-commonNames[-c(1)]

 ### +++
 for(i in 1:length(commonNames)){

 print(commonNames[i])
 zzz$commonNames[i] - NA

 print(paste(zzz,$,commonNames[i],.y,sep=))

 zzz$commonNames[i] - zzz[,paste(commonNames[i],.y,sep=)]

 ### paste(zzz$commonNames[i],.x,sep='') - NULL;
 ### paste(zzz$commonNames[i],.y,sep='') - NULL;

 }
 zzz

 The final expected set has to be: HHid is common, indv3 is from x.HHu,
 and the rest position and indv1 and indv2 are from y.HHo
  HHid position ind3  indv1 indv2
 1   HH1 10

Re: [R] merge: right set overwrite left set

2015-07-13 Thread aldi
Thank you Jeff,
Your solutions have two great aspects: a) you provide a different 
approach by using reshape2 syntax / tidyr, and b) the concern that it is 
better to update x.HHu.map with y.HHo.map, without overwriting x.HHu.map 
with NA from y.HHo.map, thus keeping intact the old value(s). That is 
the ideal situation, but I do not know exactly if the old value is 
correct, which if it is not correct then it may create strata in the 
data. Therefore I prefer better to overwrite any old column with the new 
column when it exits, even with NA. In addition, you introduce the 
safety stringAsFactors=F. The map is only for getting position based 
on HHid, and at the end I plan sorting final set based on position.

While the reshape2 worked with no problems from the start, the other 
solution with
# tidyr/dplyr solution
library(tidyr)
library(dplyr)
did not work correct in the start (it stopped R working and OS says R 
have to close) in windows 8.1 with R 3.1.2, quite possible because 
called libraries were compiled under R 3.1.3.
When installed the new version of R: 3.2.1, the second solution worked 
also with no problem.
For whoever has written the libraries tidyr and dplyr, they produced 
warnings for function name conflicts with base and stats:
Attaching package: �dplyr�
The following objects are masked from �package:stats�:
 filter, lag
The following objects are masked from �package:base�:
 intersect, setdiff, setequal, union

Great solutions!
Thank you,
Aldi

  x.HHu - data.frame(
+ HHid = c( 'HH1', 'HH2', 'HH3', 'HH4', 'HH5', 'HH10' )
+   , indv1 = c( 2, 0, 2 , 0, 2, 0 )
+   , indv2 = c( 0, NA, 2, 2, 2, 2 )
+   , ind3 = c( 0, 0, 0, 0, 0, 0 )
+   , stringsAsFactors = FALSE # avoid creating HHid as a factor
+ )
  y.HHo - data.frame(
+ HHid=c( 'HH1', 'HH2','HH5', 'HH3', 'HH10' )
+   , indv1 = c( 2, 0, 2, 0, NA )
+   , indv2 = c( 0, 2, 2, 1, 2 )
+   , stringsAsFactors = FALSE
+ )
  z.map - data.frame(
+ HHid = c( 'HH1', 'HH2', 'HH3', 'HH4', 'HH5'
+ , 'HH6','HH8', 'HH7', 'HH9', 'HH10', 'HH11' )
+   , position= c( 10, 20, 30, 42, 55, 66, 81, 75, 92, 101, 111 )
+   , stringsAsFactors = FALSE
+ )
  # reshape2 solution
  library(reshape2)
 
  x.HHu.long - melt( x.HHu, HHid, variable.name = indv )
  x.HHu.long$indv - as.character( x.HHu.long$indv )
  y.HHo.long - melt( y.HHo, HHid, variable.name = indv )
  y.HHo.long$indv - as.character( y.HHo.long$indv )
  xy.HH.long - merge( x.HHu.long
+, y.HHo.long
+, by = c( HHid, indv )
+, all = TRUE )
  xy.HH.long$value - with( xy.HH.long
+ , ifelse( is.na( value.y )
+ , value.x
+ , value.y ) )
  xy.HH0 - dcast( xy.HH.long, HHid ~ indv )
  xy.HH - merge( xy.HH0, z.map, all=TRUE )
  xy.HH - xy.HH[ order( xy.HH$HHid ), ]
  # compare xy.HH with zzz ... I think there is an error in zzz for # 
HH10/indv1, because NA should not be considered more informative
  # than 0...
### solution with reshape2
  xy.HH
HHid ind3 indv1 indv2 position
1   HH10 2 0   10
2  HH100 0 2  101
3  HH11   NANANA  111
4   HH20 0 2   20
5   HH30 0 1   30
6   HH40 0 2   42
7   HH50 2 2   55
8   HH6   NANANA   66
9   HH7   NANANA   75
10  HH8   NANANA   81
11  HH9   NANANA   92

### Solution with tidyr:
  xy.HH.d
HHid ind3 indv1 indv2 position
1   HH10 2 0   10
2  HH100 0 2  101
3  HH11   NANANA  111
4   HH20 0 2   20
5   HH30 0 1   30
6   HH40 0 2   42
7   HH50 2 2   55
8   HH6   NANANA   66
9   HH7   NANANA   75
10  HH8   NANANA   81
11  HH9   NANANA   92

On 7/12/2015 1:35 PM, Jeff Newmiller wrote:
 I get confused by your use of the position map table. If I follow your 
 description toward your desired result, I take a different route that 
 makes sense to me. Perhaps it will make sense to you as well. The key 
 idea is to make individual comparisons of the values for each 
 combination of HHid and indv, regardless of where they are in the 
 original data frames.

 Below are two different syntactic representations of my understanding 
 of your problem. They differ only in the approach taken to strip away 
 syntax clutter. I start by making the HHid identifiers character 
 values in the original data frames, because their respective factor 
 levels in the two data frames would not necessarily correspond.

 x.HHu - data.frame(
 HHid = c( 'HH1', 'HH2', 'HH3', 'HH4', 'HH5', 'HH10' )
   , indv1 = c( 2, 0, 2 , 0, 2, 0 )
   , indv2 = c( 0, NA, 2, 2, 2, 2 )
   , ind3 = c( 0, 0, 0, 0, 0, 0 )
   , stringsAsFactors = FALSE # avoid creating HHid as a factor
 )
 y.HHo - data.frame(
 HHid=c( 'HH1', 'HH2','HH5', 'HH3', 

Re: [R] merge: right set overwrite left set

2015-07-12 Thread Jeff Newmiller
I get confused by your use of the position map table. If I follow your 
description toward your desired result, I take a different route that 
makes sense to me. Perhaps it will make sense to you as well. The key idea 
is to make individual comparisons of the values for each combination of 
HHid and indv, regardless of where they are in the original data frames.


Below are two different syntactic representations of my understanding of 
your problem. They differ only in the approach taken to strip away syntax 
clutter. I start by making the HHid identifiers character values in the 
original data frames, because their respective factor levels in the two 
data frames would not necessarily correspond.


x.HHu - data.frame(
HHid = c( 'HH1', 'HH2', 'HH3', 'HH4', 'HH5', 'HH10' )
  , indv1 = c( 2, 0, 2 , 0, 2, 0 )
  , indv2 = c( 0, NA, 2, 2, 2, 2 )
  , ind3 = c( 0, 0, 0, 0, 0, 0 )
  , stringsAsFactors = FALSE # avoid creating HHid as a factor
)
y.HHo - data.frame(
HHid=c( 'HH1', 'HH2','HH5', 'HH3', 'HH10' )
  , indv1 = c( 2, 0, 2, 0, NA )
  , indv2 = c( 0, 2, 2, 1, 2 )
  , stringsAsFactors = FALSE
)
z.map - data.frame(
HHid = c( 'HH1', 'HH2', 'HH3', 'HH4', 'HH5'
, 'HH6','HH8', 'HH7', 'HH9', 'HH10', 'HH11' )
  , position= c( 10, 20, 30, 42, 55, 66, 81, 75, 92, 101, 111 )
  , stringsAsFactors = FALSE
)


# reshape2 solution
library(reshape2)

x.HHu.long - melt( x.HHu, HHid, variable.name = indv )
x.HHu.long$indv - as.character( x.HHu.long$indv )
y.HHo.long - melt( y.HHo, HHid, variable.name = indv )
y.HHo.long$indv - as.character( y.HHo.long$indv )
xy.HH.long - merge( x.HHu.long
   , y.HHo.long
   , by = c( HHid, indv )
   , all = TRUE )
xy.HH.long$value - with( xy.HH.long
, ifelse( is.na( value.y )
, value.x
, value.y ) )
xy.HH0 - dcast( xy.HH.long, HHid ~ indv )
xy.HH - merge( xy.HH0, z.map, all=TRUE )
xy.HH - xy.HH[ order( xy.HH$HHid ), ]
# compare xy.HH with zzz ... I think there is an error in zzz for 
# HH10/indv1, because NA should not be considered more informative

# than 0...

# tidyr/dplyr solution
library(tidyr)
library(dplyr)

# define a common processing sequence
lengthen - (   . # period is placeholder for data frame
%% gather( indv, value, -HHid )
%% mutate( indv = as.character( indv ) )
)
x.HHu.dlong - x.HHu %% lengthen
y.HHo.dlong - y.HHo %% lengthen
xy.HH.d - (   x.HHu.dlong
   %% full_join( y.HHo.dlong, by= c( HHid, indv ) )
   %% transmute( HHid = HHid
, indv = indv
, value = ifelse( is.na( value.y )
, value.x
, value.y )
)
   %% spread( indv, value )
   %% full_join( z.map, by=HHid )
   %% arrange( HHid )
   %% as.data.frame
   )

On Sun, 12 Jul 2015, aldi wrote:


Hi,
I have two sets of data x.HHu and y.HHo, rows are IDs and columns are
individuals. I do not know in advance indv or HHid, both of them will be
captured from the data. As the y.HHo set updates, y.HHo set has better
information then x.HHu set. Thus I want a merge where right set
overwrites left set info based on HHid, i.e. to overwrite x.HHu set with
y.HHo set but keep any extra info from the x.HHu set that is not present
in y.HHo set.
HHids will be complete based on z.map, with the corresponding positions.
I am having trouble with the part after this line: ###
+++
I am thinking that I am creating new columns position indv1 and
indv2, but R is interpreting them as row information.
See the expected final table at the end. HHid is common, indv3 is from
x.HHu, and the rest position and indv1 and indv2 are from y.HHo
Any suggestions are appreciated.
Thank you in advance,
Aldi

x.HHu- data.frame(
   HHid = c( 'HH1', 'HH2', 'HH3', 'HH4', 'HH5', 'HH10')
 , indv1 = c( 2, 0, 2 , 0, 2, 0)
 , indv2 = c( 0, NA, 2, 2, 2, 2)
 , ind3 = c( 0, 0, 0, 0, 0, 0)
 )
### the HHo data will be the top set to overwrite any HHu data, when
they exist, thinking that HHo are better than HHu results
### when they are available

y.HHo-data.frame(HHid=c('HH1', 'HH2','HH5', 'HH3', 'HH10')
 , indv1 = c(2, 0, 2, 0, NA)
 , indv2 = c(0, 2, 2, 1, 2)
 )

z.map-data.frame(HHid = c('HH1', 'HH2', 'HH3', 'HH4', 'HH5',
'HH6','HH8', 'HH7', 'HH9', 'HH10', 'HH11')
, position= c(10,20,30,42,55,66,81,75,92,101,111)
)
### see objects
x.HHu
y.HHo
z.map
### now sort the map by position, this sorted map will be used to sort
finally all data
z.map-z.map[with(z.map, order(position)), ]
z.map

### First I introduce position to both sets so I can sort them in
advance by position.
x.HHu.map -merge( z.map, x.HHu, by='HHid', all=T)

Re: [R] merge: right set overwrite left set

2015-07-12 Thread Ista Zahn
I think this does what you want:

## find idiv coloumns in x.HHu.map that don't exist in y.HHo.map
x.HHu.map - x.HHu.map[
c(HHid,
  position,
  names(x.HHu.map)[
   !names(x.HHu.map)
   %in% names(y.HHo.map)]
  )]
## merge, adding extra column from x.HHu.map
zzz - merge(y.HHo.map, x.HHu.map, by=c('HHid', 'position'), all=T)
## order by HHid
zzz - zzz[order(zzz$HHid),]

Best,
Ista

On Sun, Jul 12, 2015 at 10:45 AM, aldi a...@dsgmail.wustl.edu wrote:
 Hi,
 I have two sets of data x.HHu and y.HHo, rows are IDs and columns are
 individuals. I do not know in advance indv or HHid, both of them will be
 captured from the data. As the y.HHo set updates, y.HHo set has better
 information then x.HHu set. Thus I want a merge where right set
 overwrites left set info based on HHid, i.e. to overwrite x.HHu set with
 y.HHo set but keep any extra info from the x.HHu set that is not present
 in y.HHo set.
 HHids will be complete based on z.map, with the corresponding positions.
 I am having trouble with the part after this line: ###
 +++
 I am thinking that I am creating new columns position indv1 and
 indv2, but R is interpreting them as row information.
 See the expected final table at the end. HHid is common, indv3 is from
 x.HHu, and the rest position and indv1 and indv2 are from y.HHo
 Any suggestions are appreciated.
 Thank you in advance,
 Aldi

 x.HHu- data.frame(
 HHid = c( 'HH1', 'HH2', 'HH3', 'HH4', 'HH5', 'HH10')
   , indv1 = c( 2, 0, 2 , 0, 2, 0)
   , indv2 = c( 0, NA, 2, 2, 2, 2)
   , ind3 = c( 0, 0, 0, 0, 0, 0)
   )
 ### the HHo data will be the top set to overwrite any HHu data, when
 they exist, thinking that HHo are better than HHu results
 ### when they are available

 y.HHo-data.frame(HHid=c('HH1', 'HH2','HH5', 'HH3', 'HH10')
   , indv1 = c(2, 0, 2, 0, NA)
   , indv2 = c(0, 2, 2, 1, 2)
   )

 z.map-data.frame(HHid = c('HH1', 'HH2', 'HH3', 'HH4', 'HH5',
 'HH6','HH8', 'HH7', 'HH9', 'HH10', 'HH11')
  , position= c(10,20,30,42,55,66,81,75,92,101,111)
  )
 ### see objects
 x.HHu
 y.HHo
 z.map
 ### now sort the map by position, this sorted map will be used to sort
 finally all data
 z.map-z.map[with(z.map, order(position)), ]
 z.map

 ### First I introduce position to both sets so I can sort them in
 advance by position.
 x.HHu.map -merge( z.map, x.HHu, by='HHid', all=T)
 x.HHu.map-x.HHu.map[with(x.HHu.map, order(position)), ]
 x.HHu.map

 y.HHo.map -merge( z.map, y.HHo, by='HHid', all= T)
 y.HHo.map-y.HHo.map[with(y.HHo.map, order(position)), ]
 y.HHo.map

 ### now merge HHu  and HHo  with the hope to overwrite the HHu set with
 HHo wherever they overlap by column names.
 zzz - merge(x.HHu.map, y.HHo.map, by='HHid', all=T)
 zzz
 ### find common variable names in two sets

 commonNames - names(x.impu.map)[which(colnames(x.impu.map) %in%
 colnames(y.geno.map))]

 ## remove HHid wich is common for x and y, but work with the rest of columns
 commonNames-commonNames[-c(1)]

 ### +++
 for(i in 1:length(commonNames)){

 print(commonNames[i])
 zzz$commonNames[i] - NA

 print(paste(zzz,$,commonNames[i],.y,sep=))

 zzz$commonNames[i] - zzz[,paste(commonNames[i],.y,sep=)]

 ### paste(zzz$commonNames[i],.x,sep='') - NULL;
 ### paste(zzz$commonNames[i],.y,sep='') - NULL;

 }
 zzz

 The final expected set has to be: HHid is common, indv3 is from x.HHu,
 and the rest position and indv1 and indv2 are from y.HHo
 HHid position ind3  indv1 indv2
 1   HH1 10  0 2   0
 2  HH10101  0NA   2
 3  HH11111 NANA  NA
 4   HH2 20  0 0   2
 5   HH3 30  0 0   1
 6   HH4 42  0NA  NA
 7   HH5 55  0 2   2
 8   HH6 66 NANA  NA
 9   HH7 75 NANA  NA
 10  HH8 81 NANA  NA
 11  HH9 92 NANA  NA

 --


 [[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] merge function

2015-06-01 Thread Bert Gunter
You do not appear to understand what merge() does. Go through the worked
examples in ?merge so that you do.

FWIW, I would agree that the Help file is cryptic and difficult to
understand. Perhaps going through a tutorial on database join operations
might help.

Cheers,
Bert

Bert Gunter

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

On Mon, Jun 1, 2015 at 7:47 AM, carol white via R-help r-help@r-project.org
 wrote:

 I understood that by would take the intersection of names(x) and names(y),
 names(x) being the column names of x and names(y), column names of y.
 if x has 5 col and the col names of x are col1, col2... col5 and y has 3
 col and their names are col1, col2, col3, I thought that the merged data
 set will have 3 col, namely col1, col2, col3 but all 5 col, i.e. col1,
 col2... col5 are taken if nothing is specified for the by arg.
 Cheers,



  On Monday, June 1, 2015 4:32 PM, Michael Dewey 
 li...@dewey.myzen.co.uk wrote:




 On 01/06/2015 14:46, carol white via R-help wrote:
  Hi,By default the merge function should take the intersection of column
 names

   (if this is understood from by = intersect(names(x), names(y)),

 Dear Carol
 The by parameter specifies which columns are used to merge by. Did you
 understand it to be which columns are retained in the result?

 Just a hunch, and if not then you need to give us a toy example.



   but it takes all columns. How to specify the intersection of column
 names?
   Thanks
  Carol
 
  [[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.
 

 --
 Michael
 http://www.dewey.myzen.co.uk/home.html



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


Re: [R] merge function

2015-06-01 Thread John Kane
Let me try this again. Here are the links I forgot. My apologies.
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
 and http://adv-r.had.co.nz/Reproducibility.html

John Kane
Kingston ON Canada


 -Original Message-
 From: jrkrid...@inbox.com
 Sent: Mon, 1 Jun 2015 06:29:41 -0800
 To: wht_...@yahoo.com, r-help@r-project.org
 Subject: RE: [R] merge function
 
 As Burt says it is not exactly clear what you want but is something like
 this what you are looking for?
 
 dat1  -  data.frame(aa = c(a, b, c), bb = 1:3)
 dat2  -  data.frame(xx = c(b, c, d), yy = 3:1)
 merge(dat1, dat2, by.x = aa, by.y = xx)
 
 For further reference here are some suggestions about asking questions on
 the R-help list.  In particular it is very helpful if data is supplied in
 dput() form (See ?dput for details)
 
 John Kane
 Kingston ON Canada
 
 
 -Original Message-
 From: r-help@r-project.org
 Sent: Mon, 1 Jun 2015 13:46:15 + (UTC)
 To: r-help@r-project.org
 Subject: [R] merge function
 
 Hi,By default the merge function should take the intersection of column
 names (if this is understood from by = intersect(names(x), names(y)),
 but
 it takes all columns. How to specify the intersection of column names?
  Thanks
 Carol
 
  [[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.
 
 
 Can't remember your password? Do you need a strong and secure password?
 Use Password manager! It stores your passwords  protects your account.
 Check it out at http://mysecurelogon.com/manager


FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!

__
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] merge function

2015-06-01 Thread carol white via R-help
I understood that by would take the intersection of names(x) and names(y), 
names(x) being the column names of x and names(y), column names of y.
if x has 5 col and the col names of x are col1, col2... col5 and y has 3 col 
and their names are col1, col2, col3, I thought that the merged data set will 
have 3 col, namely col1, col2, col3 but all 5 col, i.e. col1, col2... col5 are 
taken if nothing is specified for the by arg.
Cheers,
 


 On Monday, June 1, 2015 4:32 PM, Michael Dewey li...@dewey.myzen.co.uk 
wrote:
   

 

On 01/06/2015 14:46, carol white via R-help wrote:
 Hi,By default the merge function should take the intersection of column names

  (if this is understood from by = intersect(names(x), names(y)),

Dear Carol
The by parameter specifies which columns are used to merge by. Did you 
understand it to be which columns are retained in the result?

Just a hunch, and if not then you need to give us a toy example.



  but it takes all columns. How to specify the intersection of column names?
  Thanks
 Carol

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


-- 
Michael
http://www.dewey.myzen.co.uk/home.html


  
[[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] merge function

2015-06-01 Thread Bert Gunter
1. Please read and follow the posting guide.

2. Reproducible example? (... at least I don't understand what you mean)

3. Plain text, not HTML.

Cheers,
Bert

Bert Gunter

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

On Mon, Jun 1, 2015 at 6:46 AM, carol white via R-help r-help@r-project.org
 wrote:

 Hi,By default the merge function should take the intersection of column
 names (if this is understood from by = intersect(names(x), names(y)), but
 it takes all columns. How to specify the intersection of column names?
  Thanks
 Carol

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


Re: [R] merge function

2015-06-01 Thread Michael Dewey



On 01/06/2015 14:46, carol white via R-help wrote:

Hi,By default the merge function should take the intersection of column names


 (if this is understood from by = intersect(names(x), names(y)),

Dear Carol
The by parameter specifies which columns are used to merge by. Did you 
understand it to be which columns are retained in the result?


Just a hunch, and if not then you need to give us a toy example.



 but it takes all columns. How to specify the intersection of column names?

  Thanks
Carol

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



--
Michael
http://www.dewey.myzen.co.uk/home.html

__
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] merge function

2015-06-01 Thread John Kane
As Burt says it is not exactly clear what you want but is something like this 
what you are looking for?

dat1  -  data.frame(aa = c(a, b, c), bb = 1:3)
dat2  -  data.frame(xx = c(b, c, d), yy = 3:1)
merge(dat1, dat2, by.x = aa, by.y = xx)

For further reference here are some suggestions about asking questions on the 
R-help list.  In particular it is very helpful if data is supplied in dput() 
form (See ?dput for details)

John Kane
Kingston ON Canada


 -Original Message-
 From: r-help@r-project.org
 Sent: Mon, 1 Jun 2015 13:46:15 + (UTC)
 To: r-help@r-project.org
 Subject: [R] merge function
 
 Hi,By default the merge function should take the intersection of column
 names (if this is understood from by = intersect(names(x), names(y)), but
 it takes all columns. How to specify the intersection of column names?
  Thanks
 Carol
 
   [[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.


Can't remember your password? Do you need a strong and secure password?
Use Password manager! It stores your passwords  protects your account.

__
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] merge function

2015-06-01 Thread John Kane
Exactly what I thought too the first time I read ?merge. R sometimes has its 
own approach.

John Kane
Kingston ON Canada


 -Original Message-
 From: r-help@r-project.org
 Sent: Mon, 1 Jun 2015 14:47:07 + (UTC)
 To: li...@dewey.myzen.co.uk, r-help@r-project.org
 Subject: Re: [R] merge function
 
 I understood that by would take the intersection of names(x) and
 names(y), names(x) being the column names of x and names(y), column names
 of y.
 if x has 5 col and the col names of x are col1, col2... col5 and y has 3
 col and their names are col1, col2, col3, I thought that the merged data
 set will have 3 col, namely col1, col2, col3 but all 5 col, i.e. col1,
 col2... col5 are taken if nothing is specified for the by arg.
 Cheers,
 
 
 
  On Monday, June 1, 2015 4:32 PM, Michael Dewey
 li...@dewey.myzen.co.uk wrote:
 
 
 
 
 On 01/06/2015 14:46, carol white via R-help wrote:
 Hi,By default the merge function should take the intersection of column
 names
 
   (if this is understood from by = intersect(names(x), names(y)),
 
 Dear Carol
 The by parameter specifies which columns are used to merge by. Did you
 understand it to be which columns are retained in the result?
 
 Just a hunch, and if not then you need to give us a toy example.
 
 
 
   but it takes all columns. How to specify the intersection of column
 names?
   Thanks
 Carol
 
     [[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.
 
 
 --
 Michael
 http://www.dewey.myzen.co.uk/home.html
 
 
 
   [[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.


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

__
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] Merge List element by name

2015-03-07 Thread Jeff Newmiller
This would be a perfect time for you to use best practices to convey what you 
have to us [1]... most specifically, posting using plain text will keep your 
code from getting munged up, and using dput to provide an unambiguous form we 
can put into our R sessions.

[1] 
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
---
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.

On March 7, 2015 2:01:57 PM PST, Mohammad Tanvir Ahamed via R-help 
r-help@r-project.org wrote:
Hi, I have a list like below : 
 gg
[[1]]       assembly1 GCA_000257985
[[2]]       assembly1 GCA_17125
[[3]]       assembly1 GCA_16805
[[4]]       assembly1 GCA_000144955
[[5]]       assembly                    isolation.source1 GCA_000507725
         missing
[[6]]       assembly isolation.source1
GCA_000507705           missing
[[7]]       assembly1 GCA_000237265
[[8]]       assembly location   date         host      
              isolation.source1 GCA_000237125   Taiwan Jul-02
Homo sapiens    
pus/wound##now
i want merge the list element and want output as below as data frame
Expected output : 
assembly                  location  date   host                    
  isolation.sourceGCA_000257985    NA          NA    NA      
 NA
GCA_17125    NA          NA    NA        NA
GCA_16805    NA          NA    NA        NA
GCA_000144955    NA          NA    NA        NA
GCA_000507725    NA          NA    NA      
 missing
GCA_000507705NA          NA    NA      
 missingGCA_000237265    NA          NA    NA      
 NA    
GCA_000237125    Taiwan    Jul-02    Homo sapiens      pus/wound
Any help will be very much appreciated . Thanks 
Best regards

... Tanvir AhamedGöteborg, Sweden
   [[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] merge 2 data.frames in dependence of 2 values

2014-11-13 Thread Rui Barradas

Hello,

See ?merge, in particular the argument 'all'.

dat1 - read.table(text = 
NAMEMONTH BONUS
1  Andy 2014-10   100
2  Pete 2014-10200
3  Marc2014-10300
4  Andy2014-11400
, header = TRUE, stringsAsFactors = FALSE)


dat2 - read.table(text = 
  NAME  MONTHBONUS_2
1Andy2014-10   150
2Pete 2014-11   180
3Jason   2014-10   190
4Paul 2014-10   210
5Andy2014-11   30
, header = TRUE, stringsAsFactors = FALSE)

merge(dat1, dat2, all = TRUE)


Hope this helps,

Rui Barradas

Em 13-11-2014 14:02, Matthias Weber escreveu:

Hello togehter,

i have a little problem. Maybe anyone can help me.

I have 2 data.frames, which look like as follows:
First:

 NAMEMONTH BONUS
1  Andy 2014-10   100
2  Pete 2014-10200
3  Marc2014-10300
4  Andy2014-11400

Second:

   NAME  MONTHBONUS_2
1Andy2014-10   150
2Pete 2014-11   180
3Jason   2014-10   190
4Paul 2014-10   210
5Andy2014-11   30

How can I merge this 2 data.frames, if I want the following result:

 NAMEMONTH BONUSBONUS_2
1 Andy 2014-10   100150
2 Pete 2014-11 180
3 Marc2014-10300
4 Andy2014-11   400 30
5 Pete 2014-10   200
6 Jason  2014-10 190
7 Paul 2014-10 210

The important thing is, that for every accordance in the Columns NAME and 
MONTH I get a new line.

Thanks for your help.

Best regards.

Mat


This e-mail may contain trade secrets, privileged, undisclosed or otherwise 
confidential information. If you have received this e-mail in error, you are 
hereby notified that any review, copying or distribution of it is strictly 
prohibited. Please inform us immediately and destroy the original transmittal. 
Thank you for your cooperation.

Diese E-Mail kann Betriebs- oder Geschaeftsgeheimnisse oder sonstige 
vertrauliche Informationen enthalten. Sollten Sie diese E-Mail irrtuemlich 
erhalten haben, ist Ihnen eine Kenntnisnahme des Inhalts, eine 
Vervielfaeltigung oder Weitergabe der E-Mail ausdruecklich untersagt. Bitte 
benachrichtigen Sie uns und vernichten Sie die empfangene E-Mail. Vielen Dank.

[[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@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] merge 2 data.frames in dependence of 2 values

2014-11-13 Thread William Dunlap
merge(df1, df2, all=TRUE)


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Nov 13, 2014 at 6:02 AM, Matthias Weber 
matthias.we...@fntsoftware.com wrote:

 Hello togehter,

 i have a little problem. Maybe anyone can help me.

 I have 2 data.frames, which look like as follows:
 First:

 NAMEMONTH BONUS
 1  Andy 2014-10   100
 2  Pete 2014-10200
 3  Marc2014-10300
 4  Andy2014-11400

 Second:

   NAME  MONTHBONUS_2
 1Andy2014-10   150
 2Pete 2014-11   180
 3Jason   2014-10   190
 4Paul 2014-10   210
 5Andy2014-11   30

 How can I merge this 2 data.frames, if I want the following result:

 NAMEMONTH BONUSBONUS_2
 1 Andy 2014-10   100150
 2 Pete 2014-11 180
 3 Marc2014-10300
 4 Andy2014-11   400 30
 5 Pete 2014-10   200
 6 Jason  2014-10 190
 7 Paul 2014-10 210

 The important thing is, that for every accordance in the Columns NAME
 and MONTH I get a new line.

 Thanks for your help.

 Best regards.

 Mat

 
 This e-mail may contain trade secrets, privileged, undisclosed or
 otherwise confidential information. If you have received this e-mail in
 error, you are hereby notified that any review, copying or distribution of
 it is strictly prohibited. Please inform us immediately and destroy the
 original transmittal. Thank you for your cooperation.

 Diese E-Mail kann Betriebs- oder Geschaeftsgeheimnisse oder sonstige
 vertrauliche Informationen enthalten. Sollten Sie diese E-Mail irrtuemlich
 erhalten haben, ist Ihnen eine Kenntnisnahme des Inhalts, eine
 Vervielfaeltigung oder Weitergabe der E-Mail ausdruecklich untersagt. Bitte
 benachrichtigen Sie uns und vernichten Sie die empfangene E-Mail. Vielen
 Dank.

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


[[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] merge 2 data.frames in dependence of 2 values

2014-11-13 Thread Michael Dewey



On 13/11/2014 14:02, Matthias Weber wrote:

Hello togehter,

i have a little problem. Maybe anyone can help me.


I think you might find
?merge
enlightening

Indeed given that the word merge occurs in your subject line and your 
text it is surprising you have not already found it.




I have 2 data.frames, which look like as follows:
First:

 NAMEMONTH BONUS
1  Andy 2014-10   100
2  Pete 2014-10200
3  Marc2014-10300
4  Andy2014-11400

Second:

   NAME  MONTHBONUS_2
1Andy2014-10   150
2Pete 2014-11   180
3Jason   2014-10   190
4Paul 2014-10   210
5Andy2014-11   30

How can I merge this 2 data.frames, if I want the following result:

 NAMEMONTH BONUSBONUS_2
1 Andy 2014-10   100150
2 Pete 2014-11 180
3 Marc2014-10300
4 Andy2014-11   400 30
5 Pete 2014-10   200
6 Jason  2014-10 190
7 Paul 2014-10 210

The important thing is, that for every accordance in the Columns NAME and 
MONTH I get a new line.

Thanks for your help.

Best regards.

Mat


This e-mail may contain trade secrets, privileged, undisclosed or otherwise 
confidential information. If you have received this e-mail in error, you are 
hereby notified that any review, copying or distribution of it is strictly 
prohibited. Please inform us immediately and destroy the original transmittal. 
Thank you for your cooperation.

Diese E-Mail kann Betriebs- oder Geschaeftsgeheimnisse oder sonstige 
vertrauliche Informationen enthalten. Sollten Sie diese E-Mail irrtuemlich 
erhalten haben, ist Ihnen eine Kenntnisnahme des Inhalts, eine 
Vervielfaeltigung oder Weitergabe der E-Mail ausdruecklich untersagt. Bitte 
benachrichtigen Sie uns und vernichten Sie die empfangene E-Mail. Vielen Dank.

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


-
No virus found in this message.
Checked by AVG - www.avg.com
Version: 2015.0.5557 / Virus Database: 4213/8566 - Release Date: 11/13/14




--
Michael
http://www.dewey.myzen.co.uk

__
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] merge coefficients from a glmlist of models

2014-10-28 Thread John Fox
Hi Michael,

How about this?


coef.glmlist - function(object, result=c(list, matrix, data.frame),
...){
result - match.arg(result)
coefs - lapply(object, coef)
if (result == list) return(coefs)
coef.names - unique(unlist(lapply(coefs, names)))
n.mods - length(object)
coef.matrix - matrix(NA, length(coef.names), n.mods)
rownames(coef.matrix) - coef.names
colnames(coef.matrix) - names(object)
for (i in 1:n.mods){
coef - coef(object[[i]])
coef.matrix[names(coef), i] - coef
}
if (result == matrix) return(coef.matrix)
as.data.frame(coef.matrix)
}

 coef(mods, result=data.frame)
  donner.mod1 donner.mod2 donner.mod3 donner.mod4
(Intercept)1.59915455  1.85514867   0.8792031   0.7621901
age   -0.03379836 -0.04565225  NA  NA
sexMale   -1.20678665 -1.62177307  -1.3745016  -1.0995718
age:sexMaleNA  0.01957257  NA  NA
poly(age, 2)1  NA  NA  -7.9366059 -26.9688970
poly(age, 2)2  NA  NA  -6.6929413 -30.5626032
poly(age, 2)1:sexMale  NA  NA  NA  22.7210591
poly(age, 2)2:sexMale  NA  NA  NA  28.8975876

Best,
 John

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Michael Friendly
 Sent: Tuesday, October 28, 2014 11:47 AM
 To: R-help
 Subject: [R] merge coefficients from a glmlist of models
 
 In the vcdExtra package, I have a function glmlist to collect a set of
 glm() models as a glmlist object,
 and other functions that generate  fit such a collection of models.
 
 This is my working example, fitting a set of models to the Donner data
 
 # install.packages(vcdExtra, repos=http://R-Forge.R-project.org;)#
 needs devel version
 data(Donner, package=vcdExtra)
 # make survived a factor
 Donner$survived - factor(Donner$survived, labels=c(no, yes))
 Donner$family.size - ave(as.numeric(Donner$family), Donner$family,
 FUN=length)
 # collapse small families into Other
 fam - Donner$family
 levels(fam)[c(3,4,6,7,9)] - Other
 # reorder, putting Other last
 fam = factor(fam,levels(fam)[c(1, 2, 4:6, 3)])
 Donner$family - fam
 
 # fit models
 donner.mod0 - glm(survived ~ age, data=Donner, family=binomial)
 donner.mod1 - glm(survived ~ age + sex, data=Donner, family=binomial)
 donner.mod2 - glm(survived ~ age * sex , data=Donner, family=binomial)
 donner.mod3 - glm(survived ~ poly(age,2) + sex, data=Donner,
 family=binomial)
 donner.mod4 - glm(survived ~ poly(age,2) * sex, data=Donner,
 family=binomial)
 mods - glmlist(donner.mod1, donner.mod2, donner.mod3, donner.mod4)
 
 I'd like to write other methods for handling a glmlist, similar to the
 way stats::anova.glmlist works, e.g.,
 
   library(vcdExtra)
   mods - glmlist(donner.mod1, donner.mod2, donner.mod3, donner.mod4)
  
   anova(mods, test=Chisq)
 Analysis of Deviance Table
 
 Model 1: survived ~ age + sex
 Model 2: survived ~ age * sex
 Model 3: survived ~ poly(age, 2) + sex
 Model 4: survived ~ poly(age, 2) * sex
 Resid. Df Resid. Dev Df Deviance Pr(Chi)
 1 87 111.128
 2 86 110.727 1 0.4003 0.52692
 3 86 106.731 0 3.9958
 4 84 97.799 2 8.9321 0.01149 *
 ---
 Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
 
 # gives same result as anova() with an explicit list of models:
 
   anova(donner.mod1, donner.mod2, donner.mod3, donner.mod4,
 test=Chisq)
 Analysis of Deviance Table
 
 Model 1: survived ~ age + sex
 Model 2: survived ~ age * sex
 Model 3: survived ~ poly(age, 2) + sex
 Model 4: survived ~ poly(age, 2) * sex
 Resid. Df Resid. Dev Df Deviance Pr(Chi)
 1 87 111.128
 2 86 110.727 1 0.4003 0.52692
 3 86 106.731 0 3.9958
 4 84 97.799 2 8.9321 0.01149 *
 ---
 Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
 
 Then, using the function vcdExtra::Summarise, I can define a
 Summarise.glmlist method that is essentially
 
 sumry - lapply(mods, Summarise)
 do.call(rbind, sumry)
 
   Summarise(mods)# not yet added to the package
 Likelihood summary table:
 AIC BIC LR Chisq Df Pr(Chisq)
 donner.mod1 117.13 124.63 111.128 87 0.04159 *
 donner.mod2 118.73 128.73 110.727 86 0.03755 *
 donner.mod3 114.73 124.73 106.731 86 0.06439 .
 donner.mod4 109.80 124.80 97.799 84 0.14408
 ---
 Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
 
 Similarly, I can define a residuals.glmlist method, using using cbind()
 to collect the residuals from all
 models.
 But I'm stumped on a coef() method, because the coefficients fitted in
 various models
 differ.
 
   coefs - lapply(mods, coef)
   coefs
 $donner.mod1
 (Intercept) age sexMale
 1.59915455 -0.03379836 -1.20678665
 
 $donner.mod2
 (Intercept) age sexMale age:sexMale
 1.85514867 -0.04565225 -1.62177307 0.01957257
 
 $donner.mod3
 (Intercept) poly(age, 2)1 poly(age, 2)2 sexMale
 0.8792031 -7.9366059 -6.6929413 -1.3745016
 
 $donner.mod4
 (Intercept) poly(age, 2)1 poly(age, 

Re: [R] merge coefficients from a glmlist of models

2014-10-28 Thread Michael Friendly

On 10/28/2014 12:13 PM, John Fox wrote:

Hi Michael,

How about this?
That's perfect! And more general than I had hoped for.  You probably 
used mindreader() :-)

-Michael




coef.glmlist - function(object, result=c(list, matrix, data.frame),
...){
 result - match.arg(result)
 coefs - lapply(object, coef)
 if (result == list) return(coefs)
 coef.names - unique(unlist(lapply(coefs, names)))
 n.mods - length(object)
 coef.matrix - matrix(NA, length(coef.names), n.mods)
 rownames(coef.matrix) - coef.names
 colnames(coef.matrix) - names(object)
 for (i in 1:n.mods){
 coef - coef(object[[i]])
 coef.matrix[names(coef), i] - coef
 }
 if (result == matrix) return(coef.matrix)
 as.data.frame(coef.matrix)
}


coef(mods, result=data.frame)

   donner.mod1 donner.mod2 donner.mod3 donner.mod4
(Intercept)1.59915455  1.85514867   0.8792031   0.7621901
age   -0.03379836 -0.04565225  NA  NA
sexMale   -1.20678665 -1.62177307  -1.3745016  -1.0995718
age:sexMaleNA  0.01957257  NA  NA
poly(age, 2)1  NA  NA  -7.9366059 -26.9688970
poly(age, 2)2  NA  NA  -6.6929413 -30.5626032
poly(age, 2)1:sexMale  NA  NA  NA  22.7210591
poly(age, 2)2:sexMale  NA  NA  NA  28.8975876

Best,
  John


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
project.org] On Behalf Of Michael Friendly
Sent: Tuesday, October 28, 2014 11:47 AM
To: R-help
Subject: [R] merge coefficients from a glmlist of models

In the vcdExtra package, I have a function glmlist to collect a set of
glm() models as a glmlist object,
and other functions that generate  fit such a collection of models.

This is my working example, fitting a set of models to the Donner data

# install.packages(vcdExtra, repos=http://R-Forge.R-project.org;)#
needs devel version
data(Donner, package=vcdExtra)
# make survived a factor
Donner$survived - factor(Donner$survived, labels=c(no, yes))
Donner$family.size - ave(as.numeric(Donner$family), Donner$family,
FUN=length)
# collapse small families into Other
fam - Donner$family
levels(fam)[c(3,4,6,7,9)] - Other
# reorder, putting Other last
fam = factor(fam,levels(fam)[c(1, 2, 4:6, 3)])
Donner$family - fam

# fit models
donner.mod0 - glm(survived ~ age, data=Donner, family=binomial)
donner.mod1 - glm(survived ~ age + sex, data=Donner, family=binomial)
donner.mod2 - glm(survived ~ age * sex , data=Donner, family=binomial)
donner.mod3 - glm(survived ~ poly(age,2) + sex, data=Donner,
family=binomial)
donner.mod4 - glm(survived ~ poly(age,2) * sex, data=Donner,
family=binomial)
mods - glmlist(donner.mod1, donner.mod2, donner.mod3, donner.mod4)

I'd like to write other methods for handling a glmlist, similar to the
way stats::anova.glmlist works, e.g.,

   library(vcdExtra)
   mods - glmlist(donner.mod1, donner.mod2, donner.mod3, donner.mod4)
  
   anova(mods, test=Chisq)
Analysis of Deviance Table

Model 1: survived ~ age + sex
Model 2: survived ~ age * sex
Model 3: survived ~ poly(age, 2) + sex
Model 4: survived ~ poly(age, 2) * sex
Resid. Df Resid. Dev Df Deviance Pr(Chi)
1 87 111.128
2 86 110.727 1 0.4003 0.52692
3 86 106.731 0 3.9958
4 84 97.799 2 8.9321 0.01149 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

# gives same result as anova() with an explicit list of models:

   anova(donner.mod1, donner.mod2, donner.mod3, donner.mod4,
test=Chisq)
Analysis of Deviance Table

Model 1: survived ~ age + sex
Model 2: survived ~ age * sex
Model 3: survived ~ poly(age, 2) + sex
Model 4: survived ~ poly(age, 2) * sex
Resid. Df Resid. Dev Df Deviance Pr(Chi)
1 87 111.128
2 86 110.727 1 0.4003 0.52692
3 86 106.731 0 3.9958
4 84 97.799 2 8.9321 0.01149 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Then, using the function vcdExtra::Summarise, I can define a
Summarise.glmlist method that is essentially

sumry - lapply(mods, Summarise)
do.call(rbind, sumry)

   Summarise(mods)# not yet added to the package
Likelihood summary table:
AIC BIC LR Chisq Df Pr(Chisq)
donner.mod1 117.13 124.63 111.128 87 0.04159 *
donner.mod2 118.73 128.73 110.727 86 0.03755 *
donner.mod3 114.73 124.73 106.731 86 0.06439 .
donner.mod4 109.80 124.80 97.799 84 0.14408
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Similarly, I can define a residuals.glmlist method, using using cbind()
to collect the residuals from all
models.
But I'm stumped on a coef() method, because the coefficients fitted in
various models
differ.

   coefs - lapply(mods, coef)
   coefs
$donner.mod1
(Intercept) age sexMale
1.59915455 -0.03379836 -1.20678665

$donner.mod2
(Intercept) age sexMale age:sexMale
1.85514867 -0.04565225 -1.62177307 0.01957257

$donner.mod3
(Intercept) poly(age, 2)1 poly(age, 2)2 sexMale
0.8792031 -7.9366059 -6.6929413 

Re: [R] Merge of the rows by finding the sum of the rows

2014-10-23 Thread Rui Barradas

Hello,

Try

aggregate(Rain ~ Year + Month, data = dat, FUN = sum)


Hope this helps,

Rui Barradas

Em 23-10-2014 01:29, Hafizuddin Arshad escreveu:

Dear R users,

Can someone help me on this?  I would like to find the sum of the Rain if
the Month appears more than once. For example in row 3 and 4, October
appear more than once, so I want to find the sum of the two rows and
replace it so that the Month just appear once. It some sort of merge but by
finding the sum for the third column. This is my data:

structure(list(Year = c(1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
1971L, 1971L, 1971L, 1972L, 1972L, 1972L, 1972L, 1972L, 1972L,
1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 1973L,
1973L, 1974L, 1974L, 1974L, 1974L, 1974L, 1974L, 1974L, 1974L,
1974L, 1975L, 1975L, 1975L, 1975L, 1975L, 1975L, 1975L, 1976L,
1976L, 1976L, 1976L, 1976L, 1976L, 1976L, 1976L, 1976L), Month = c(3L,
6L, 10L, 10L, 11L, 11L, 11L, 11L, 12L, 2L, 9L, 12L, 12L, 12L,
12L, 3L, 9L, 10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 8L, 9L,
11L, 11L, 11L, 11L, 11L, 12L, 12L, 1L, 2L, 4L, 10L, 11L, 12L,
12L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L), Rain = c(196,
88.8, 96, 70.6, 104.9, 80, 102.8, 161.5, 123.4, 70.8, 99, 77.7,
130.8, 134.1, 86.3, 213.3, 169.9, 89.4, 78.7, 81.5, 100.3, 107.1,
93.2, 83.8, 253.2, 75.4, 134.5, 84.5, 82.5, 82.5, 119.5, 119.5,
134.5, 83.5, 372.5, 79.5, 112, 80.5, 129.5, 120.5, 126, 73, 93.5,
86.5, 140.5, 76, 180.5, 75, 130.5, 130.5)), .Names = c(Year,
Month, Rain), row.names = c(NA, 50L), class = data.frame)

Thank you so much for the help.


Arshad

[[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@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] merge by time, certain value if 5 min before and after an event

2014-10-04 Thread Dagmar
Thank you Jean, Petr, Terry, William and everyone else who thought about 
my problem.
It is sooo good that this mailing list exists!

I solved my problem using Petr's suggestion, that didn't seem so 
complicated and worked fine for me.
Thanks again and have a great weekend,
Dagmar



Am 02.10.2014 um 23:38 schrieb Adams, Jean:
 Thanks, Dagmar.

 So, shouldn't row 3 with a time of 09:51:01 be low and not high?

 Jean

 On Thu, Oct 2, 2014 at 4:25 PM, Dagmar ramga...@gmx.net 
 mailto:ramga...@gmx.net wrote:

 Dear Jean and all,

 I want all lines to be low, but during times 9:55 - 10:05 a.m
 (i.e. a
 timespan of 10 min) I want them to be high.
 In my real data low and high refer to lowtide and hightide in
 the waddensea and I want to assign the location of my animal at
 the time
 it was taken to the tide (that means, there was water not only exactly
 at 10:00 (as taken from official data) but also 5 min before and
 after).

 I hope that is more understandable, if not ask again. Thanks for
 trying
 to help,
 Dagmar

 Am 02.10.2014 um 23:01 schrieb Adams, Jean:
  Dagmar,
 
  Can you explain more fully why rows 1, 2, and 5 in your result are
  low and rows 3 and 4 are high?  It is not clear to me from the
  information you have provided.
 
   result[c(1, 2, 5), ]
  Timestamp location Event
  1 24.09.2012 09:05:011 low
  2 24.09.2012 09:49:502 low
  5 24.09.2012 10:05:105 low
 
   result[3:4, ]
  Timestamp location Event
  3 24.09.2012 09:51:013  high
  4 24.09.2012 10:04:501  high
 
  Jean
 
 
  On Thu, Oct 2, 2014 at 8:13 AM, Dagmar ramga...@gmx.net
 mailto:ramga...@gmx.net
  mailto:ramga...@gmx.net mailto:ramga...@gmx.net wrote:
 
  Hello! I hope someone can help me. It would save me days of
 work.
  Thanks in
  advance!
  I have two dataframes which look like these:
 
 
  myframe - data.frame (Timestamp=c(24.09.2012 09:00:00,
 24.09.2012
  10:00:00,
  24.09.2012 11:00:00), Event=c(low,high,low) )
  myframe
 
 
  mydata - data.frame ( Timestamp=c(24.09.2012 09:05:01,
 24.09.2012
  09:49:50, 24.09.2012 09:51:01, 24.09.2012 10:04:50,
 24.09.2012
  10:05:10)
  , location=c(1,2,3,1,5) )
  mydata
 
 
  # I want to merge them by time so I have a dataframe which looks
  like this
  in the end (i.e. Low  during 5 min before and after high )
 
  result - data.frame ( Timestamp=c(24.09.2012 09:05:01,
 24.09.2012
  09:49:50, 24.09.2012 09:51:01, 24.09.2012 10:04:50,
 24.09.2012
  10:05:10)
  , location=c(1,2,3,1,5) ,
  Event=c(low, low,high,high,low))
  result
 
  Anyone knows how do merge them?
  Best regards,
  Dagmar
 
  __
  R-help@r-project.org mailto:R-help@r-project.org
 mailto:R-help@r-project.org mailto: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 mailto: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] merge by time, certain value if 5 min before and after an event

2014-10-03 Thread PIKAL Petr
Hi

If Jean's guess is correct, after simple changing Timestamp to real date

see ?strptime and ?as.POSIXct

you can use

result - merge(mydata, myframe, all=TRUE)

use function ?na.locf from zoo package to fill NAs in Event column and get rid 
of all rows with NA in location e.g. by

?complete.cases

Regards

Petr


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Adams, Jean
 Sent: Thursday, October 02, 2014 11:38 PM
 To: Dagmar
 Cc: R help
 Subject: Re: [R] merge by time, certain value if 5 min before and after
 an event

 Thanks, Dagmar.

 So, shouldn't row 3 with a time of 09:51:01 be low and not high?

 Jean

 On Thu, Oct 2, 2014 at 4:25 PM, Dagmar ramga...@gmx.net wrote:

  Dear Jean and all,
 
  I want all lines to be low, but during times 9:55 - 10:05 a.m (i.e.
  a timespan of 10 min) I want them to be high.
  In my real data low and high refer to lowtide and hightide in
  the waddensea and I want to assign the location of my animal at the
  time it was taken to the tide (that means, there was water not only
  exactly at 10:00 (as taken from official data) but also 5 min before
 and after).
 
  I hope that is more understandable, if not ask again. Thanks for
  trying to help, Dagmar
 
  Am 02.10.2014 um 23:01 schrieb Adams, Jean:
   Dagmar,
  
   Can you explain more fully why rows 1, 2, and 5 in your result are
   low and rows 3 and 4 are high?  It is not clear to me from the
   information you have provided.
  
result[c(1, 2, 5), ]
   Timestamp location Event
   1 24.09.2012 09:05:011 low
   2 24.09.2012 09:49:502 low
   5 24.09.2012 10:05:105 low
  
result[3:4, ]
   Timestamp location Event
   3 24.09.2012 09:51:013  high
   4 24.09.2012 10:04:501  high
  
   Jean
  
  
   On Thu, Oct 2, 2014 at 8:13 AM, Dagmar ramga...@gmx.net
   mailto:ramga...@gmx.net wrote:
  
   Hello! I hope someone can help me. It would save me days of
 work.
   Thanks in
   advance!
   I have two dataframes which look like these:
  
  
   myframe - data.frame (Timestamp=c(24.09.2012 09:00:00,
 24.09.2012
   10:00:00,
   24.09.2012 11:00:00), Event=c(low,high,low) )
   myframe
  
  
   mydata - data.frame ( Timestamp=c(24.09.2012 09:05:01,
 24.09.2012
   09:49:50, 24.09.2012 09:51:01, 24.09.2012 10:04:50,
 24.09.2012
   10:05:10)
   , location=c(1,2,3,1,5) )
   mydata
  
  
   # I want to merge them by time so I have a dataframe which
 looks
   like this
   in the end (i.e. Low  during 5 min before and after high )
  
   result - data.frame ( Timestamp=c(24.09.2012 09:05:01,
 24.09.2012
   09:49:50, 24.09.2012 09:51:01, 24.09.2012 10:04:50,
 24.09.2012
   10:05:10)
   , location=c(1,2,3,1,5) ,
   Event=c(low, low,high,high,low))
   result
  
   Anyone knows how do merge them?
   Best regards,
   Dagmar
  
   __
   R-help@r-project.org mailto: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.
 

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


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

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech

Re: [R] merge by time, certain value if 5 min before and after an event

2014-10-03 Thread PIKAL Petr
Hi

So if I understand correctly, you want to spread value high to times 5 
minutes before its occurrence and 5 minutes after its occurrence.

If your dates are not extremely big you can prepare its expanded version and 
use code suggestions I sent previously

myframe - data.frame (Timestamp=c(24.09.2012 09:00:00, 24.09.2012 
10:00:00, 24.09.2012 11:00:00), Event=c(low,high,low) )
myframe$Timestamp - as.POSIXct(strptime(myframe$Timestamp, format=%d.%m.%Y 
%H:%M:%S))
expand-data.frame(Timestamp=seq(myframe[1,1], myframe[3,1], by=5 mins))
new - merge(expand, myframe, all=T)
position - which(new$Event==high)
hhh - c(position-1, position, position+1)
new$Event[hhh] - high

If you merge this new data frame with your mydata and use na.locf to fill 
gaps you should get desired result.

Regards
Petr


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Dagmar
 Sent: Thursday, October 02, 2014 11:25 PM
 Cc: R help
 Subject: Re: [R] merge by time, certain value if 5 min before and after
 an event

 Dear Jean and all,

 I want all lines to be low, but during times 9:55 - 10:05 a.m (i.e. a
 timespan of 10 min) I want them to be high.
 In my real data low and high refer to lowtide and hightide in
 the waddensea and I want to assign the location of my animal at the
 time it was taken to the tide (that means, there was water not only
 exactly at 10:00 (as taken from official data) but also 5 min before
 and after).

 I hope that is more understandable, if not ask again. Thanks for trying
 to help, Dagmar

 Am 02.10.2014 um 23:01 schrieb Adams, Jean:
  Dagmar,
 
  Can you explain more fully why rows 1, 2, and 5 in your result are
  low and rows 3 and 4 are high?  It is not clear to me from the
  information you have provided.
 
   result[c(1, 2, 5), ]
  Timestamp location Event
  1 24.09.2012 09:05:011 low
  2 24.09.2012 09:49:502 low
  5 24.09.2012 10:05:105 low
 
   result[3:4, ]
  Timestamp location Event
  3 24.09.2012 09:51:013  high
  4 24.09.2012 10:04:501  high
 
  Jean
 
 
  On Thu, Oct 2, 2014 at 8:13 AM, Dagmar ramga...@gmx.net
  mailto:ramga...@gmx.net wrote:
 
  Hello! I hope someone can help me. It would save me days of work.
  Thanks in
  advance!
  I have two dataframes which look like these:
 
 
  myframe - data.frame (Timestamp=c(24.09.2012 09:00:00,
 24.09.2012
  10:00:00,
  24.09.2012 11:00:00), Event=c(low,high,low) )
  myframe
 
 
  mydata - data.frame ( Timestamp=c(24.09.2012 09:05:01,
 24.09.2012
  09:49:50, 24.09.2012 09:51:01, 24.09.2012 10:04:50,
 24.09.2012
  10:05:10)
  , location=c(1,2,3,1,5) )
  mydata
 
 
  # I want to merge them by time so I have a dataframe which looks
  like this
  in the end (i.e. Low  during 5 min before and after high )
 
  result - data.frame ( Timestamp=c(24.09.2012 09:05:01,
 24.09.2012
  09:49:50, 24.09.2012 09:51:01, 24.09.2012 10:04:50,
 24.09.2012
  10:05:10)
  , location=c(1,2,3,1,5) ,
  Event=c(low, low,high,high,low))
  result
 
  Anyone knows how do merge them?
  Best regards,
  Dagmar
 
  __
  R-help@r-project.org mailto: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.


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

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost

Re: [R] merge by time, certain value if 5 min before and after an event

2014-10-03 Thread William Dunlap
Hi Terry,

Some of that combination of sort() and approx() can be done by
findInterval(), which may be quick enough that you don't need the
'thinning' part of the code.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Fri, Oct 3, 2014 at 6:05 AM, Therneau, Terry M., Ph.D.
thern...@mayo.edu wrote:
 I've attached two functions used locally.  (The attachments will be stripped
 off of the r-help response, but the questioner should get them).  The
 functions neardate and tmerge were written to deal with a query that
 comes up very often in our medical statistics work, some variety of get the
 closest creatinine value to the subject's date of rehospitalization, at
 least one week before but no more than 1 year prior, or tasks that merge
 two data sets to create a single (start, stop] style one.

 The neardate function is a variant on match().  Given two (id, date) pairs
 it will find the first pair in list 2 that has date2 = date1 (or =) and
 the same id.  The second variable can be any orderable class, but dates are
 the most common use and hence the name.

 These are being added to the survival package release that should be out
 real-soon-now, once I add some extended examples of their use to the time
 dependent covariates vignette.

 Terry Therneau

 On 10/03/2014 05:00 AM, r-help-requ...@r-project.org wrote:

 Hello! I hope someone can help me. It would save me days of work. Thanks
 in
 advance!
 I have two dataframes which look like these:


 myframe - data.frame (Timestamp=c(24.09.2012 09:00:00, 24.09.2012
 10:00:00,
 24.09.2012 11:00:00), Event=c(low,high,low) )
 myframe


 mydata - data.frame ( Timestamp=c(24.09.2012 09:05:01, 24.09.2012
 09:49:50, 24.09.2012 09:51:01, 24.09.2012 10:04:50, 24.09.2012
 10:05:10)
 , location=c(1,2,3,1,5) )
 mydata


 # I want to merge them by time so I have a dataframe which looks like this
 in the end (i.e. Low  during 5 min before and after high )

 result - data.frame ( Timestamp=c(24.09.2012 09:05:01, 24.09.2012
 09:49:50, 24.09.2012 09:51:01, 24.09.2012 10:04:50, 24.09.2012
 10:05:10)
 , location=c(1,2,3,1,5) ,
 Event=c(low, low,high,high,low))
 result

 Anyone knows how do merge them?
 Best regards,
 Dagmar


 __
 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] merge by time, certain value if 5 min before and after an event

2014-10-02 Thread Adams, Jean
Dagmar,

Can you explain more fully why rows 1, 2, and 5 in your result are low
and rows 3 and 4 are high?  It is not clear to me from the information
you have provided.

 result[c(1, 2, 5), ]
Timestamp location Event
1 24.09.2012 09:05:011   low
2 24.09.2012 09:49:502   low
5 24.09.2012 10:05:105   low

 result[3:4, ]
Timestamp location Event
3 24.09.2012 09:51:013  high
4 24.09.2012 10:04:501  high

Jean


On Thu, Oct 2, 2014 at 8:13 AM, Dagmar ramga...@gmx.net wrote:

 Hello! I hope someone can help me. It would save me days of work. Thanks in
 advance!
 I have two dataframes which look like these:


 myframe - data.frame (Timestamp=c(24.09.2012 09:00:00, 24.09.2012
 10:00:00,
 24.09.2012 11:00:00), Event=c(low,high,low) )
 myframe


 mydata - data.frame ( Timestamp=c(24.09.2012 09:05:01, 24.09.2012
 09:49:50, 24.09.2012 09:51:01, 24.09.2012 10:04:50, 24.09.2012
 10:05:10)
 , location=c(1,2,3,1,5) )
 mydata


 # I want to merge them by time so I have a dataframe which looks like this
 in the end (i.e. Low  during 5 min before and after high )

 result - data.frame ( Timestamp=c(24.09.2012 09:05:01, 24.09.2012
 09:49:50, 24.09.2012 09:51:01, 24.09.2012 10:04:50, 24.09.2012
 10:05:10)
 , location=c(1,2,3,1,5) ,
 Event=c(low, low,high,high,low))
 result

 Anyone knows how do merge them?
 Best regards,
 Dagmar

 __
 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] merge by time, certain value if 5 min before and after an event

2014-10-02 Thread Dagmar
Dear Jean and all,

I want all lines to be low, but during times 9:55 - 10:05 a.m (i.e. a 
timespan of 10 min) I want them to be high.
In my real data low and high refer to lowtide and hightide in 
the waddensea and I want to assign the location of my animal at the time 
it was taken to the tide (that means, there was water not only exactly 
at 10:00 (as taken from official data) but also 5 min before and after).

I hope that is more understandable, if not ask again. Thanks for trying 
to help,
Dagmar

Am 02.10.2014 um 23:01 schrieb Adams, Jean:
 Dagmar,

 Can you explain more fully why rows 1, 2, and 5 in your result are 
 low and rows 3 and 4 are high?  It is not clear to me from the 
 information you have provided.

  result[c(1, 2, 5), ]
 Timestamp location Event
 1 24.09.2012 09:05:011 low
 2 24.09.2012 09:49:502 low
 5 24.09.2012 10:05:105 low

  result[3:4, ]
 Timestamp location Event
 3 24.09.2012 09:51:013  high
 4 24.09.2012 10:04:501  high

 Jean


 On Thu, Oct 2, 2014 at 8:13 AM, Dagmar ramga...@gmx.net 
 mailto:ramga...@gmx.net wrote:

 Hello! I hope someone can help me. It would save me days of work.
 Thanks in
 advance!
 I have two dataframes which look like these:


 myframe - data.frame (Timestamp=c(24.09.2012 09:00:00, 24.09.2012
 10:00:00,
 24.09.2012 11:00:00), Event=c(low,high,low) )
 myframe


 mydata - data.frame ( Timestamp=c(24.09.2012 09:05:01, 24.09.2012
 09:49:50, 24.09.2012 09:51:01, 24.09.2012 10:04:50, 24.09.2012
 10:05:10)
 , location=c(1,2,3,1,5) )
 mydata


 # I want to merge them by time so I have a dataframe which looks
 like this
 in the end (i.e. Low  during 5 min before and after high )

 result - data.frame ( Timestamp=c(24.09.2012 09:05:01, 24.09.2012
 09:49:50, 24.09.2012 09:51:01, 24.09.2012 10:04:50, 24.09.2012
 10:05:10)
 , location=c(1,2,3,1,5) ,
 Event=c(low, low,high,high,low))
 result

 Anyone knows how do merge them?
 Best regards,
 Dagmar

 __
 R-help@r-project.org mailto: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] merge by time, certain value if 5 min before and after an event

2014-10-02 Thread Adams, Jean
Thanks, Dagmar.

So, shouldn't row 3 with a time of 09:51:01 be low and not high?

Jean

On Thu, Oct 2, 2014 at 4:25 PM, Dagmar ramga...@gmx.net wrote:

 Dear Jean and all,

 I want all lines to be low, but during times 9:55 - 10:05 a.m (i.e. a
 timespan of 10 min) I want them to be high.
 In my real data low and high refer to lowtide and hightide in
 the waddensea and I want to assign the location of my animal at the time
 it was taken to the tide (that means, there was water not only exactly
 at 10:00 (as taken from official data) but also 5 min before and after).

 I hope that is more understandable, if not ask again. Thanks for trying
 to help,
 Dagmar

 Am 02.10.2014 um 23:01 schrieb Adams, Jean:
  Dagmar,
 
  Can you explain more fully why rows 1, 2, and 5 in your result are
  low and rows 3 and 4 are high?  It is not clear to me from the
  information you have provided.
 
   result[c(1, 2, 5), ]
  Timestamp location Event
  1 24.09.2012 09:05:011 low
  2 24.09.2012 09:49:502 low
  5 24.09.2012 10:05:105 low
 
   result[3:4, ]
  Timestamp location Event
  3 24.09.2012 09:51:013  high
  4 24.09.2012 10:04:501  high
 
  Jean
 
 
  On Thu, Oct 2, 2014 at 8:13 AM, Dagmar ramga...@gmx.net
  mailto:ramga...@gmx.net wrote:
 
  Hello! I hope someone can help me. It would save me days of work.
  Thanks in
  advance!
  I have two dataframes which look like these:
 
 
  myframe - data.frame (Timestamp=c(24.09.2012 09:00:00, 24.09.2012
  10:00:00,
  24.09.2012 11:00:00), Event=c(low,high,low) )
  myframe
 
 
  mydata - data.frame ( Timestamp=c(24.09.2012 09:05:01, 24.09.2012
  09:49:50, 24.09.2012 09:51:01, 24.09.2012 10:04:50, 24.09.2012
  10:05:10)
  , location=c(1,2,3,1,5) )
  mydata
 
 
  # I want to merge them by time so I have a dataframe which looks
  like this
  in the end (i.e. Low  during 5 min before and after high )
 
  result - data.frame ( Timestamp=c(24.09.2012 09:05:01, 24.09.2012
  09:49:50, 24.09.2012 09:51:01, 24.09.2012 10:04:50, 24.09.2012
  10:05:10)
  , location=c(1,2,3,1,5) ,
  Event=c(low, low,high,high,low))
  result
 
  Anyone knows how do merge them?
  Best regards,
  Dagmar
 
  __
  R-help@r-project.org mailto: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.


[[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] Merge rows

2014-07-14 Thread David Stevens
Cautionary note on the solution below. Be sure the 'sndr' is either 
factor or character because, if sndr is numeric, as the list is 
populated, R will fill in non-adjacent list items with NULLs, leaving a 
list with many empty entries. So, the modified line is


  newdat[[as.character(vnam)]]-newvec[-1]

David

On 7/12/2014 8:33 AM, David Stevens wrote:
This is a (very) slightly modified version of Jim's reply that takes 
the sender's email our of the list element and uses it as the name so 
it can be accessed as newdat$'senders email' or newdat[['senders email']]


newdat-list()
for(sndr in unique(rdvdf$sender)) {
 newvec-
  as.character(unique(unlist(rdvdf[rdvdf$sender==sndr,])))
 newdat[[(sndr)]]-newvec[which(!is.na(newvec))][-1]
}

David

On 7/12/2014 1:07 AM, Jim Lemon wrote:

On Fri, 11 Jul 2014 12:19:39 PM Ryan de Vera wrote:

Hello all,

I have a data frame filled with senders and recipients. Some of the

senders

have multiple rows with different recipients and I want to merge

those

rows. For example I have

a...@email.com b...@email.com
a...@email.com c...@email.com d...@email.com
r...@email.com  f...@email.com
r...@email.com  h...@email.com

I want this to become

a...@email.com b...@email.com c...@email.com d...@email.com
r...@email.com  f...@email.com  h...@email.com

How would I go about doing this?


Hi Ryan,
This is a bit messy, but assuming that you do have a data frame like
this:

rdvdf-
data.frame(sender=rep(c(a...@email.com,r...@email.com),each=2),
recipient1=c(b...@email.com,c...@email.com,f...@email.com,h...@email.com),
  recipient2=c(NA,d...@email.com,NA,NA))

you can try this:

newdat-list()
senderno-1
for(sndr in unique(rdvdf$sender)) {
  newvec-
   as.character(unique(unlist(rdvdf[rdvdf$sender==sndr,])))
  newdat[[senderno]]-newvec[!is.na(newvec)]
  senderno-senderno+1
}

Jim

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html

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




--
David K Stevens, P.E., Ph.D.
Professor and Head, Environmental Engineering
Civil and Environmental Engineering
Utah Water Research Laboratory
8200 Old Main Hill
Logan, UT  84322-8200
435 797 3229 - voice
435 797 1363 - fax
david.stev...@usu.edu

__
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] Merge rows

2014-07-12 Thread Jim Lemon
On Fri, 11 Jul 2014 12:19:39 PM Ryan de Vera wrote:
 Hello all,
 
 I have a data frame filled with senders and recipients. Some of the 
senders
 have multiple rows with different recipients and I want to merge 
those
 rows. For example I have
 
 a...@email.com b...@email.com
 a...@email.com c...@email.com d...@email.com
 r...@email.com  f...@email.com
 r...@email.com  h...@email.com
 
 I want this to become
 
 a...@email.com b...@email.com c...@email.com d...@email.com
 r...@email.com  f...@email.com  h...@email.com
 
 How would I go about doing this?
 
Hi Ryan,
This is a bit messy, but assuming that you do have a data frame like 
this:

rdvdf-
data.frame(sender=rep(c(a...@email.com,r...@email.com),each=2),
 
recipient1=c(b...@email.com,c...@email.com,f...@email.com,h...@email.com),
 recipient2=c(NA,d...@email.com,NA,NA))

you can try this:

newdat-list()
senderno-1
for(sndr in unique(rdvdf$sender)) {
 newvec-
  as.character(unique(unlist(rdvdf[rdvdf$sender==sndr,])))
 newdat[[senderno]]-newvec[!is.na(newvec)]
 senderno-senderno+1
}

Jim

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


Re: [R] Merge rows

2014-07-12 Thread David Stevens
This is a (very) slightly modified version of Jim's reply that takes the 
sender's email our of the list element and uses it as the name so it can 
be accessed as newdat$'senders email' or newdat[['senders email']]


newdat-list()
for(sndr in unique(rdvdf$sender)) {
 newvec-
  as.character(unique(unlist(rdvdf[rdvdf$sender==sndr,])))
 newdat[[(sndr)]]-newvec[which(!is.na(newvec))][-1]
}

David

On 7/12/2014 1:07 AM, Jim Lemon wrote:

On Fri, 11 Jul 2014 12:19:39 PM Ryan de Vera wrote:

Hello all,

I have a data frame filled with senders and recipients. Some of the

senders

have multiple rows with different recipients and I want to merge

those

rows. For example I have

a...@email.com b...@email.com
a...@email.com c...@email.com d...@email.com
r...@email.com  f...@email.com
r...@email.com  h...@email.com

I want this to become

a...@email.com b...@email.com c...@email.com d...@email.com
r...@email.com  f...@email.com  h...@email.com

How would I go about doing this?


Hi Ryan,
This is a bit messy, but assuming that you do have a data frame like
this:

rdvdf-
data.frame(sender=rep(c(a...@email.com,r...@email.com),each=2),
  
recipient1=c(b...@email.com,c...@email.com,f...@email.com,h...@email.com),
  recipient2=c(NA,d...@email.com,NA,NA))

you can try this:

newdat-list()
senderno-1
for(sndr in unique(rdvdf$sender)) {
  newvec-
   as.character(unique(unlist(rdvdf[rdvdf$sender==sndr,])))
  newdat[[senderno]]-newvec[!is.na(newvec)]
  senderno-senderno+1
}

Jim

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


--
David K Stevens, P.E., Ph.D.
Professor and Head, Environmental Engineering
Civil and Environmental Engineering
Utah Water Research Laboratory
8200 Old Main Hill
Logan, UT  84322-8200
435 797 3229 - voice
435 797 1363 - fax
david.stev...@usu.edu

__
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] Merge rows

2014-07-11 Thread Sarah Goslee
Hi Ryan,

We can't tell from your example what structure your original data are
in, nor what your output is intended to look like, or for that matter
how you got from one to the other. Please don't post in HTML because
it gets mangled!

Using dput() to provide your example data is the best thing, because
that preserves R structures. And an R-format example of your desired
output would also be helpful.

Sarah

On Fri, Jul 11, 2014 at 12:19 PM, Ryan de Vera ryan.devera...@gmail.com wrote:
 Hello all,

 I have a data frame filled with senders and recipients. Some of the senders
 have multiple rows with different recipients and I want to merge those
 rows. For example I have

 a...@email.com b...@email.com
 a...@email.com c...@email.com d...@email.com
 r...@email.com  f...@email.com
 r...@email.com  h...@email.com

 I want this to become

 a...@email.com b...@email.com c...@email.com d...@email.com
 r...@email.com  f...@email.com  h...@email.com

 How would I go about doing this?

 [[alternative HTML version deleted]]

^^^ That is part of the problem!

-- 
Sarah Goslee
http://www.functionaldiversity.org

__
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] Merge rows

2014-07-11 Thread Sarah Goslee
Hi John,

I don't think your x2 is right, but who knows?

One possible approach would be:
R lapply(split(x2$recipients,  unique(x2$sender)), paste, collapse=, )
$`a...@email.com`
[1] b...@email.com, f...@email.com

$`r...@email.com`
[1] c(\c...@email.com\, \d...@email.com\), h...@email.com

but again, who knows exactly what the OP wants?

You sent this just to me; I've copied the R-help list back again.

Sarah

On Fri, Jul 11, 2014 at 3:19 PM, John McKown
john.archie.mck...@gmail.com wrote:
 On Fri, Jul 11, 2014 at 11:27 AM, Sarah Goslee sarah.gos...@gmail.com wrote:
 Hi Ryan,

 We can't tell from your example what structure your original data are
 in, nor what your output is intended to look like, or for that matter
 how you got from one to the other. Please don't post in HTML because
 it gets mangled!

 Using dput() to provide your example data is the best thing, because
 that preserves R structures. And an R-format example of your desired
 output would also be helpful.

 Sarah

 In a, perhaps vain, attempt to be helpful, I think the following
 output from what I constructed myself will show what the OP was
 talking about:

 dput(x2)
 structure(list(sender = c(a...@email.com, a...@email.com, 
 r...@email.com,
 r...@email.com), recipients = list(b...@email.com, c(c...@email.com,
 d...@email.com), f...@email.com, h...@email.com)), .Names = c(sender,
 recipients), row.names = c(NA, -4L), class = data.frame)

 I probably should have called it mailingList instead of x2, no?

 Now, after wasting too much time, perhaps somebody will come up with
 a solution. I could brute force something (yes, you can write
 FORTRAN style code in R!). But have been sensitized from doing so by
 others.

 --
 There is nothing more pleasant than traveling and meeting new people!
 Genghis Khan

 Maranatha! 
 John McKown


-- 
Sarah Goslee
http://www.sarahgoslee.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] merge question

2014-06-30 Thread Dr Eberhard W Lisse
Rolf,

I hear you. 

But, after reflection, ie I looked at my situation again, it is great :-)-O

el


Sent from Dr Lisse's iPad mini

 On Jun 30, 2014, at 0:48, Rolf Turner r.tur...@auckland.ac.nz wrote:
 
 
 On 30/06/14 10:32, Dr Eberhard W Lisse wrote:
 
 Thanks,
 
 I then set NA to 0, and can do the sutraction,
 
 great.
 
 Not so great.  I haven't gone through the issues underlying this post, but 
 replacing NA by 0 will almost surely yield nonsense.  Missing is ***not*** 
 the same thing as zero.  Pretending that it is will have disastrous 
 consequences.  Think through what you are trying to do more clearly.
 
 cheers,
 
 Rolf Turner

__
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] merge question

2014-06-29 Thread Jeff Newmiller
If you really prefer solution code, then provide reproducible example code as 
the footer requests.

Use ?merge with the all.x=TRUE parameter, and then perform your calculations on 
the resulting combined data frame, using ?ifelse and ?is.na as needed.
---
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.

On June 29, 2014 9:24:39 AM PDT, Dr Eberhard Lisse nos...@lisse.na wrote:
I have two data frames like so

 qpiso
iso requests
1A1   20
2A2  199
3AD5
4AE  176
...
189  ZW   82

 qplegit
iso requests
1A2   36
2AE4
3AM2
4AO1
...
100  ZW3


I want to create another dataframe qpspam which contains all pairs
from pqiso with the values for requests of qplegit being subtracted
from those which exist in qpiso,

ie
iso requests
1A1   20
2A2  163
3AD5
4AE  172
...
189  ZW   79

but don't know how to do this, and google isn't too helpful.

As usual, a solution is preferred, but a pointer to where I can
read this up is equally appreciated :-)-O

el

__
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] merge question

2014-06-29 Thread Michael Peng
you can get a new data frame by
merge(qpiso, qplegit, all.x = TRUE, all.y = TRUE, by = iso )
Take the subtraction on the new data frame.




2014-06-29 11:24 GMT-05:00 Dr Eberhard Lisse nos...@lisse.na:

 I have two data frames like so

  qpiso
 iso requests
 1A1   20
 2A2  199
 3AD5
 4AE  176
 ...
 189  ZW   82

  qplegit
 iso requests
 1A2   36
 2AE4
 3AM2
 4AO1
 ...
 100  ZW3


 I want to create another dataframe qpspam which contains all pairs
 from pqiso with the values for requests of qplegit being subtracted
 from those which exist in qpiso,

 ie
 iso requests
 1A1   20
 2A2  163
 3AD5
 4AE  172
 ...
 189  ZW   79

 but don't know how to do this, and google isn't too helpful.

 As usual, a solution is preferred, but a pointer to where I can
 read this up is equally appreciated :-)-O

 el

 __
 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] merge question

2014-06-29 Thread Dr Eberhard W Lisse
Thanks,

I then set NA to 0, and can do the sutraction,

great.

el


On 2014-06-29, 22:32 , Michael Peng wrote:
 you can get a new data frame by
 merge(qpiso, qplegit, all.x = TRUE, all.y = TRUE, by = iso )
 Take the subtraction on the new data frame.
 
 
 
 
 2014-06-29 11:24 GMT-05:00 Dr Eberhard Lisse nos...@lisse.na
 mailto:nos...@lisse.na:
 
 I have two data frames like so
 
  qpiso
 iso requests
 1A1   20
 2A2  199
 3AD5
 4AE  176
 ...
 189  ZW   82
 
  qplegit
 iso requests
 1A2   36
 2AE4
 3AM2
 4AO1
 ...
 100  ZW3
 
 
 I want to create another dataframe qpspam which contains all pairs
 from pqiso with the values for requests of qplegit being subtracted
 from those which exist in qpiso,
 
 ie
 iso requests
 1A1   20
 2A2  163
 3AD5
 4AE  172
 ...
 189  ZW   79
 
 but don't know how to do this, and google isn't too helpful.
 
 As usual, a solution is preferred, but a pointer to where I can
 read this up is equally appreciated :-)-O
 
 el
 
 __
 R-help@r-project.org mailto: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] merge question

2014-06-29 Thread Rolf Turner


On 30/06/14 10:32, Dr Eberhard W Lisse wrote:


Thanks,

I then set NA to 0, and can do the sutraction,

great.


Not so great.  I haven't gone through the issues underlying this post, 
but replacing NA by 0 will almost surely yield nonsense.  Missing is 
***not*** the same thing as zero.  Pretending that it is will have 
disastrous consequences.  Think through what you are trying to do more 
clearly.


cheers,

Rolf Turner

__
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] merge question

2014-06-29 Thread Dr Eberhard W Lisse
Thank you very much.

el

On 2014-06-30, 00:48 , Rolf Turner wrote:
 
 On 30/06/14 10:32, Dr Eberhard W Lisse wrote:
 
 Thanks,

 I then set NA to 0, and can do the sutraction,

 great.
 
 Not so great.  I haven't gone through the issues underlying this post,
 but replacing NA by 0 will almost surely yield nonsense.  Missing is
 ***not*** the same thing as zero.  Pretending that it is will have
 disastrous consequences.  Think through what you are trying to do more
 clearly.
 
 cheers,
 
 Rolf Turner

__
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] Merge two vectors into one

2014-03-24 Thread Frans Marcelissen
Why not simply

 a-1:3
 b-4:5
 c(a,b)
[1] 1 2 3 4 5


2014-03-22 23:22 GMT+01:00 Tham Tran hanhtham.t...@yahoo.com.vn:

 Dear R users,

 Given two vectors x and y
 a=1 2 3
 b=4 5 6

 i want to combine them into a single vector z as 1 4 2 5 3 6

 Thanks for your help

 Tham



 --
 View this message in context:
 http://r.789695.n4.nabble.com/Merge-two-vectors-into-one-tp4687361.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.


[[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] Merge two vectors into one

2014-03-24 Thread Lisa S
He wants a[1] b[1] a[2] b[2] a[3] b[3]
I think you can do:
x =  as.vector(rbind(a, b))



On Mon, Mar 24, 2014 at 2:39 PM, Frans Marcelissen 
fransiepansiekever...@gmail.com wrote:

 Why not simply

  a-1:3
  b-4:5
  c(a,b)
 [1] 1 2 3 4 5


 2014-03-22 23:22 GMT+01:00 Tham Tran hanhtham.t...@yahoo.com.vn:

  Dear R users,
 
  Given two vectors x and y
  a=1 2 3
  b=4 5 6
 
  i want to combine them into a single vector z as 1 4 2 5 3 6
 
  Thanks for your help
 
  Tham
 
 
 
  --
  View this message in context:
  http://r.789695.n4.nabble.com/Merge-two-vectors-into-one-tp4687361.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.
 

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


[[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] Merge two vectors into one

2014-03-24 Thread David Winsemius

On Mar 22, 2014, at 3:22 PM, Tham Tran wrote:

 Dear R users,
 
 Given two vectors x and y 
 a=1 2 3
 b=4 5 6
 
 i want to combine them into a single vector z as 1 4 2 5 3 6
 
 Thanks for your help

Searching Stackoverflow for [r] interleave produced this idea fron @Arun, which 
I think is superior to the c(matrix-byrow) or as.vector(matrix-byrow) 
strategies because it generalizes to unequal length vectors:

 c(a,b)[ order( c(seq_along(a), seq_along(b)))]
[1] 1 4 2 5 3 6

-- 

David Winsemius
Alameda, CA, USA

__
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] Merge two vectors into one

2014-03-24 Thread Greg Snow
Just to satisfy my curiosity:

 library(microbenchmark)

 a - 1:10
 b - 101:110

 microbenchmark(
+ m1=as.vector( rbind(a,b) ),
+ m2=c( rbind(a,b) ),
+ m3=as.vector( matrix(c(a,b), nrow=2, byrow=TRUE) ),
+ m4={x - integer(length(a)*2); x[c(TRUE,FALSE)] - a;
x[c(FALSE,TRUE)] - b; x},
+ m5={x - integer(length(a)*2); x[seq.int(1, length.out=length(a), by=2)] - a;
+ x[seq.int(2,length.out=length(b), by=2)] - b; x},
+ m6=c(a,b)[order( c(seq_along(a),seq_along(b)))]
+ )
Unit: microseconds
 exprmin  lq median  uq max neval
   m1  2.463  3.0795  3.285  4.1055   7.390   100
   m2  1.642  2.0530  2.464  2.8740  10.675   100
   m3  2.874  3.6955  4.516  4.9270  31.200   100
   m4  6.979  7.5950  8.211  9.4420  28.737   100
   m5  8.211  9.4420 10.263 11.0850 101.400   100
   m6 10.674 11.9050 12.317 13.1380  43.927   100


Of course the timings will change (and could possibly change order)
with different a and b vectors.  And there are other
advantages/disadvantages to each method beyond the timings.  But I
found this interesting.

On Mon, Mar 24, 2014 at 1:19 AM, David Winsemius dwinsem...@comcast.net wrote:

 On Mar 22, 2014, at 3:22 PM, Tham Tran wrote:

 Dear R users,

 Given two vectors x and y
 a=1 2 3
 b=4 5 6

 i want to combine them into a single vector z as 1 4 2 5 3 6

 Thanks for your help

 Searching Stackoverflow for [r] interleave produced this idea fron @Arun, 
 which I think is superior to the c(matrix-byrow) or as.vector(matrix-byrow) 
 strategies because it generalizes to unequal length vectors:

 c(a,b)[ order( c(seq_along(a), seq_along(b)))]
 [1] 1 4 2 5 3 6

 --

 David Winsemius
 Alameda, CA, USA

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



-- 
Gregory (Greg) L. Snow Ph.D.
538...@gmail.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] Merge two vectors into one

2014-03-24 Thread Tham Tran
Many thank for all your answers. it has helped me save time



--
View this message in context: 
http://r.789695.n4.nabble.com/Merge-two-vectors-into-one-tp4687361p4687447.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] Merge two vectors into one

2014-03-23 Thread David Winsemius

On Mar 22, 2014, at 3:22 PM, Tham Tran wrote:

 Dear R users,
 
 Given two vectors x and y 
 a=1 2 3
 b=4 5 6
 
 i want to combine them into a single vector z as 1 4 2 5 3 6

One way:

 c( matrix(c(a,b), nrow=2, byrow=TRUE) )

Leaving in the usual footer because Nabble's interface usually omits this and 
Nabble users often fail to read it.

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.

-- 
David Winsemius
Alameda, CA, USA

__
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] Merge two vectors into one

2014-03-23 Thread Rolf Turner

On 24/03/14 08:37, David Winsemius wrote:


On Mar 22, 2014, at 3:22 PM, Tham Tran wrote:


Dear R users,

Given two vectors x and y
a=1 2 3
b=4 5 6

i want to combine them into a single vector z as 1 4 2 5 3 6


One way:

  c( matrix(c(a,b), nrow=2, byrow=TRUE) )


It is more perspicuous to use

as.vector(matrix(c(a,b), nrow=2, byrow=TRUE))

The end result is the same, but it is better to be explicit, rather than 
relying on the side-effect of the c()-function.


See fortune(convert a matrix).

cheers,

Rolf

__
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] Merge two vectors into one

2014-03-22 Thread arun
Hi,
May be this helps:
a - 1:3
 b - 4:6
 z - as.vector(rbind(a,b))
 z
#[1] 1 4 2 5 3 6


#or
z1 - setNames(c(a,b),rep(seq_along(a),2))
z1 - as.vector(z1[order(names(z1))])
z1
#[1] 1 4 2 5 3 6
A.K.


Dear R users, 

Given two vectors x and y 
a=1 2 3 
b=4 5 6 

i want to combine them into a single vector z as 1 4 2 5 3 6 

Thanks for your help 

Tham

__
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] Merge two data frames on date field

2014-02-07 Thread arun
Hi,You haven't provided any example dataset. 

 set.seed(42)
 dat1 - data.frame(dates=seq(as.POSIXct(2009-01-01 00:00:00,format=%Y-%m-%d 
%H:%M:%S),by= '12 hour', length=12),Field1=rnorm(12),Field2=LETTERS[1:12])

set.seed(395)
 dat2 - data.frame(dates=seq(as.POSIXct(2009-01-01 00:00:00,format=%Y-%m-%d 
%H:%M:%S),by= '6 hour', 
length=12),Field1=rnorm(12),Field2=sample(LETTERS,12,replace=FALSE))


merge(dat1,dat2,by=dates)
#    dates   Field1.x Field2.x   Field1.y Field2.y
#1 2009-01-01 00:00:00  1.3709584    A -1.2152900    V
#2 2009-01-01 12:00:00 -0.5646982    B -1.2771657    P
#3 2009-01-02 00:00:00  0.3631284    C  0.4829300    W
#4 2009-01-02 12:00:00  0.6328626    D  0.5468625    Y
#5 2009-01-03 00:00:00  0.4042683    E -1.7113256    I
#6 2009-01-03 12:00:00 -0.1061245    F  0.4851851    B


merge(dat1,dat2,by=dates,all=TRUE)[1:5,]
    dates   Field1.x Field2.x   Field1.y Field2.y
1 2009-01-01 00:00:00  1.3709584    A -1.2152900    V
2 2009-01-01 06:00:00 NA NA -0.4633211    G 
3 2009-01-01 12:00:00 -0.5646982    B -1.2771657    P
4 2009-01-01 18:00:00 NA NA  0.6011199    N
5 2009-01-02 00:00:00  0.3631284    C  0.4829300    W
A.K.


why wont two tables merge correctly on a date? 

I have one table with a field called 'date' 2009-01-01 00:00:00. 
The other table also has a field call 'date'. 

The dates are something like  2009-01-01 01:00:00. 

The two tables are not merging correctly. The dates are not lining up. NA

The table also has other fields with dates in them. 

Sorry the data set is very big so I have not loaded it. 

The code I'm using to merge is m2 - merge(m1 ,mt,all=TRUE)

__
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] Merge and specify column output order

2014-01-15 Thread arun
Hi,

It would be better to post a reproducible example.

set.seed(42)
 df1 - 
cbind(as.data.frame(matrix(sample(60,40,replace=TRUE),ncol=4)),date=seq(as.Date(2013-05-10),length.out=10,by=1))
colnames(df1)[-5] - paste0(mod,1:4)

 set.seed(14)
 df2 - 
data.frame(obs=1:20,wy=sample(12,20,replace=TRUE),date=seq(as.Date(2013-05-12),length.out=20,by=3))
 An - c(colnames(df1),colnames(df2))
merge(df1,df2,by=c(date))[,An[!duplicated(An,fromLast=TRUE)]]
#  mod1 mod2 mod3 mod4 obs wy   date
#1   18   57   60   24   1  4 2013-05-12
#2   32   57   31   50   2  8 2013-05-15
#3   40   29   27   55   3 12 2013-05-18


A.K.


On Wednesday, January 15, 2014 4:44 PM, Janet Choate jsc@gmail.com wrote:
Hi,

i am merging two data frames of different time periods, so that the result
contains data from both for the same time period.
however, I want the result to output columns in a certain order.
the common column between the two data frames is date.

for example:

df1 columns:
mod1 mod2 mod3 mod4 date  #(there are actually 691 mod columns)

df2 columns:
obs wy date

after merge, i want the order of the columns to be all mod data columns
from df1, followed by the obs and wy columns from df2, followed by the date
column.

i almost get want i want with (at least it doesn't put the common column
date as the first column as merge does by default):

new = merge(df1, df2, by=c(date))[, union(names(df1), names(df2))]

however, that of course gives me all of df1 followed by all of df2, which
doesn't put obs immediately after the mod columns:

what i get:
mod1 mod2 mod3 mod4 date obs wy

what i want:
mod1 mod2 mod3 mod4 obs wy date

any suggestions on how to output the columns in the order i want, without
having to rearrange the order after the fact?

thank you for any help!
Janet

    [[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@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] Merge xts and Return.portfolio

2013-11-15 Thread Joshua Ulrich
On Fri, Nov 15, 2013 at 6:32 AM, Simone Medori simone.med...@me.com wrote:
 Hi,
 I'm triyng to merge two xts time series objects, one of them is from 
 Return.portfolio (PerformanceAnalytics).

 Despite the merging xts objects have the same indexes, the merged object 
 shows extra lines at the day before of every entry.

 I noticed that indexes of merging objects have different classes (POSIXct 
 and Date): might be this the reason? Why do I get different extra dates 
 anyway?

Yes, this is the reason.  Specifically, the cause is the difference in
timezone between the POSIXct index and the Date index.

For some reason, Return.portfolio returns a xts object with a POSIXct
index.  Convert it to Date and your merge will work.
rp - Return.portfolio(returns)
index(rp) - as.Date(index(rp))
merge(returns,rp)

 Kind regards,

 Simone


 require(PerformanceAnalytics)
 require(quantmod)

 benchmark-c(^STOXX50E,^NDX)
 downloaded-getSymbols(benchmark,from=as.Date(Sys.Date()-15))
 prices - merge.xts(na.locf(do.call(merge,lapply(downloaded, function(x) 
 Cl(get(x))
 returns - Return.calculate(prices)[-1,] #get rid of first NA

 returns
 #STOXX50E.Close NDX.Close
 #2013-11-01   -0.005153278  0.0006009953
 #2013-11-040.0  0.0014764362
 #2013-11-05   -0.005314304  0.0012024522
 #2013-11-060.006745896 -0.0010151026
 #2013-11-07   -0.004390787 -0.0188959585
 #2013-11-080.0  0.0136779259
 #2013-11-110.003236959 -0.0011464756
 #2013-11-12   -0.005945303  0.0006690495
 #2013-11-13   -0.004451870  0.0119843220
 #2013-11-140.010764042  0.0028130469

 Return.portfolio(returns)
 # portfolio.returns
 #2013-11-01 -0.0022761415
 #2013-11-04  0.0007403469
 #2013-11-05 -0.0020441262
 #2013-11-06  0.0028386740
 #2013-11-07 -0.0116652539
 #2013-11-08  0.0068094113
 #2013-11-11  0.0010398246
 #2013-11-12 -0.0026371940
 #2013-11-13  0.0037957949
 #2013-11-14  0.0067416934
 #Warning message:
 #  In Return.portfolio(returns) :
 #  weighting vector is null, calulating an equal weighted portfolio

 merge(returns,Return.portfolio(returns))

 #STOXX50E.Close NDX.Close portfolio.returns
 #2013-10-31 NANA -0.0022761415 # 
 Return.portfolio merges into extra lines!
 #2013-11-01   -0.005153278  0.0006009953NA
 #2013-11-03 NANA  0.0007403469
 #2013-11-040.0  0.0014764362NA
 #2013-11-04 NANA -0.0020441262
 #2013-11-05   -0.005314304  0.0012024522NA
 #2013-11-05 NANA  0.0028386740
 #2013-11-060.006745896 -0.0010151026NA
 #2013-11-06 NANA -0.0116652539
 #2013-11-07   -0.004390787 -0.0188959585NA
 #2013-11-07 NANA  0.0068094113
 #2013-11-080.0  0.0136779259NA
 #2013-11-10 NANA  0.0010398246
 #2013-11-110.003236959 -0.0011464756NA
 #2013-11-11 NANA -0.0026371940
 #2013-11-12   -0.005945303  0.0006690495NA
 #2013-11-12 NANA  0.0037957949
 #2013-11-13   -0.004451870  0.0119843220NA
 #2013-11-13 NANA  0.0067416934
 #2013-11-140.010764042  0.0028130469NA

Best,
--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.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] Merge xts and Return.portfolio

2013-11-15 Thread Simone Medori
Thanks

Simone

 Il giorno 15/nov/2013, alle ore 15:30, Joshua Ulrich 
 josh.m.ulr...@gmail.com ha scritto:
 
 On Fri, Nov 15, 2013 at 6:32 AM, Simone Medori simone.med...@me.com wrote:
 Hi,
 I'm triyng to merge two xts time series objects, one of them is from 
 Return.portfolio (PerformanceAnalytics).
 
 Despite the merging xts objects have the same indexes, the merged object 
 shows extra lines at the day before of every entry.
 
 I noticed that indexes of merging objects have different classes (POSIXct 
 and Date): might be this the reason? Why do I get different extra dates 
 anyway?
 Yes, this is the reason.  Specifically, the cause is the difference in
 timezone between the POSIXct index and the Date index.
 
 For some reason, Return.portfolio returns a xts object with a POSIXct
 index.  Convert it to Date and your merge will work.
 rp - Return.portfolio(returns)
 index(rp) - as.Date(index(rp))
 merge(returns,rp)
 
 Kind regards,
 
 Simone
 
 
 require(PerformanceAnalytics)
 require(quantmod)
 
 benchmark-c(^STOXX50E,^NDX)
 downloaded-getSymbols(benchmark,from=as.Date(Sys.Date()-15))
 prices - merge.xts(na.locf(do.call(merge,lapply(downloaded, function(x) 
 Cl(get(x))
 returns - Return.calculate(prices)[-1,] #get rid of first NA
 
 returns
 #STOXX50E.Close NDX.Close
 #2013-11-01   -0.005153278  0.0006009953
 #2013-11-040.0  0.0014764362
 #2013-11-05   -0.005314304  0.0012024522
 #2013-11-060.006745896 -0.0010151026
 #2013-11-07   -0.004390787 -0.0188959585
 #2013-11-080.0  0.0136779259
 #2013-11-110.003236959 -0.0011464756
 #2013-11-12   -0.005945303  0.0006690495
 #2013-11-13   -0.004451870  0.0119843220
 #2013-11-140.010764042  0.0028130469
 
 Return.portfolio(returns)
 # portfolio.returns
 #2013-11-01 -0.0022761415
 #2013-11-04  0.0007403469
 #2013-11-05 -0.0020441262
 #2013-11-06  0.0028386740
 #2013-11-07 -0.0116652539
 #2013-11-08  0.0068094113
 #2013-11-11  0.0010398246
 #2013-11-12 -0.0026371940
 #2013-11-13  0.0037957949
 #2013-11-14  0.0067416934
 #Warning message:
 #  In Return.portfolio(returns) :
 #  weighting vector is null, calulating an equal weighted portfolio
 
 merge(returns,Return.portfolio(returns))
 
 #STOXX50E.Close NDX.Close portfolio.returns
 #2013-10-31 NANA -0.0022761415 # 
 Return.portfolio merges into extra lines!
 #2013-11-01   -0.005153278  0.0006009953NA
 #2013-11-03 NANA  0.0007403469
 #2013-11-040.0  0.0014764362NA
 #2013-11-04 NANA -0.0020441262
 #2013-11-05   -0.005314304  0.0012024522NA
 #2013-11-05 NANA  0.0028386740
 #2013-11-060.006745896 -0.0010151026NA
 #2013-11-06 NANA -0.0116652539
 #2013-11-07   -0.004390787 -0.0188959585NA
 #2013-11-07 NANA  0.0068094113
 #2013-11-080.0  0.0136779259NA
 #2013-11-10 NANA  0.0010398246
 #2013-11-110.003236959 -0.0011464756NA
 #2013-11-11 NANA -0.0026371940
 #2013-11-12   -0.005945303  0.0006690495NA
 #2013-11-12 NANA  0.0037957949
 #2013-11-13   -0.004451870  0.0119843220NA
 #2013-11-13 NANA  0.0067416934
 #2013-11-140.010764042  0.0028130469NA
 
 Best,
 --
 Joshua Ulrich  |  about.me/joshuaulrich
 FOSS Trading  |  www.fosstrading.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.


Re: [R] merge multi part polygons

2013-09-11 Thread Bert Gunter
I would suggest you post this on the r-sig-ecology or r-sig-geo lists.
These seem both more relevant (r-help is for general R programming
questions, mostly) and more likely to have participants with suitable
expertise.

Cheers,
Bert


On Wed, Sep 11, 2013 at 12:32 PM, Maria Fernanda Bonetti 
fernanda_bone...@hotmail.com wrote:

 I need to extract climatic variables of many shapefiles. I managed to do
 it, but doing one at a time (I have to do this for 3000 species).
 Well, I did the following:
 first I downloaded the rasters of BIOCLIM bios -getData (Worldclim var
 = bio, res = 2.5, T = download)
 Ok, then I went with shapes (2 species did just to test). teste_sp1
 -readShapePoly (sp1, IDVAR = NULL, proj4string = CRS (as.character
 (NA)), verbose = FALSE, repair = FALSE, FALSE = force_ring, delete_null_obj
 = FALSE, retrieve_ABS_null = FALSE)
 Finally, I used a function to extract the variables of shapes v -
 extract (bios, sp1)

 THE PROBLEM:The sp1 is a multi part polygon (23 features). I could make
 the average of these 23 lines and everything would be ok right? The problem
 is that some lines appears NA, and I have no idea why, and I can not just
 ignore that.
 Do I need to join these parts into a single polygon? I tried this with
 unionSpatialPolygons but another error appears: Error in
 unionSpatialPolygons (sp1, IDS, threshold = NULL,: input lengths differ
 Can anyone help me?

 Att,
 Maria Fernanda BonettiDoutoranda do Programa de Pós Graduação em Ecologia
 e Conservação - UFPRMestre em Ecologia e ConservaçãoPesquisadora do
 Instituto de Pesquisas Cananéia - IPeC
 [[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.




-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

[[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] merge matrix row data

2013-08-01 Thread Elaine Kuo
Hello arun

Thanks for the answers.
I understand the answer to question 2.
However, about question 1, sorry I did not clarify the question.

1. If the row names are 1, 2, and 4 etc (numbers) instead of GID 1, GID 2,
and GID 3,
   is there any modification in need for the code ?
   The original data looks like below.

Original matrix

GID   D0989   D9820  D5629  D4327  D2134

1   100   1  0

2   011   0  0

4   001   0  0

5   110   0  0

7   010   0  1



Resulting matrix

D0989   D9820  D5629  D4327  D2134

Island A   11   0   1   0

Island B   01   1   0   1


Elaine


On Thu, Aug 1, 2013 at 7:15 AM, arun smartpink...@yahoo.com wrote:

 Hi Elaine,

 In that case:
 Do you have GID in the IslandA and IslandBs?

 IslandA-c(GID 1, GID 5)
 IslandB- c(GID 2, GID 4, GID 7)

 If there is no change in the two Islands, then using the same dataset:

 mat1- as.matrix(read.table(text=
 D0989  D9820  D5629  D4327  D2134
 GID_1100  1  0
 GID_2011  0  0
 GID_4001  0  0
 GID_5110  0  0
 GID_7010  0  1
 ,sep=,header=TRUE))

 row.names(mat1)- gsub(.*\\_,,row.names(mat1)) #to replace the GID_
 from the row.names()
  mat1
 #  D0989 D9820 D5629 D4327 D2134
 #1 1 0 0 1 0
 #2 0 1 1 0 0
 #4 0 0 1 0 0
 #5 1 1 0 0 0
 #7 0 1 0 0 1
  IslandA-c(GID 1, GID 5)
  IslandB- c(GID 2, GID 4, GID 7)
 res-t(sapply(c(IslandA,IslandB),function(x) {x1-
 mat1[match(gsub(.*\\s+,,get(x)),row.names(mat1)),];(!!colSums(x1))*1}))
 res
  #   D0989 D9820 D5629 D4327 D2134
 #IslandA 1 1 0 1 0
 #IslandB 0 1 1 0 1


 Regarding the use of !!colSums()
 You can check these:

  t(sapply(c(IslandA,IslandB),function(x) {x1-
 mat1[match(gsub(.*\\s+,,get(x)),row.names(mat1)),];!colSums(x1)}))
 #D0989 D9820 D5629 D4327 D2134
 #IslandA FALSE FALSE  TRUE FALSE  TRUE
 #IslandB  TRUE FALSE FALSE  TRUE FALSE

 t(sapply(c(IslandA,IslandB),function(x) {x1-
 mat1[match(gsub(.*\\s+,,get(x)),row.names(mat1)),];!!colSums(x1)}))
 #D0989 D9820 D5629 D4327 D2134
 #IslandA  TRUE  TRUE FALSE  TRUE FALSE
 #IslandB FALSE  TRUE  TRUE FALSE  TRUE

 # *1 will replace TRUE with 1 and FALSE with 0.

 A.K.






 
 From: Elaine Kuo elaine.kuo...@gmail.com
 To: arun smartpink...@yahoo.com
 Sent: Wednesday, July 31, 2013 6:58 PM
 Subject: Re: [R] merge matrix row data



 Dear Arun,

 Thank you for the clear explanation.
 The row.names question is a mistyping, for I do not have enough sleep last
 night.

 Two more questions

 1. If the row names are 1, 2, and 4 etc (numbers) instead of GID 1, GID 2,
 and GID 3,
is there any modification in need for the code ?

 2. Please kindly explain the code
 (!!colSums(x1))*1}

It is the critical part to merge the row data.

 Thanks again.

 Elaine



 On Thu, Aug 1, 2013 at 6:45 AM, arun smartpink...@yahoo.com wrote:

 Dear Elaine,
 
 I used that line only because you didn't provide the data using dput().
 So, I need to either use delimiter  , or just leave a space by first
 joining the GID and the numbers using _.  I chose the latter as I
 didn't had that much time to spent by putting , between each entries.
 After that, I removed _ using the ?gsub().  As Bert pointed out, there
 are many online resources for understanding regular expression.
 
 In this particular case, what I did was to single out the _ in the
 first pair of quotes, and replace with  space in the second pair of quotes
  .  Therefore, GID_1, would become GID 1, which is what your original
 dataset looks like.
 
 If you type row.names(mat1) on the R console and enter, you will be able
 to get the output.
 
 Hope it helps.
 Arun
 
 
 
 
 
 
 
 
 
 From: Elaine Kuo elaine.kuo...@gmail.com
 To: arun smartpink...@yahoo.com
 Cc: R help r-help@r-project.org
 Sent: Wednesday, July 31, 2013 5:07 PM
 Subject: Re: [R] merge matrix row data
 
 
 
 
 Dear Arun
 
 Thank you for the very useful help.
 However, please kindly explain the code below.
 row.names(mat1)- gsub([_], ,row.names(mat1))
 
 
 1. what does [_] mean?
 2. what doesmean?
 3. what does row.names(mat1) mean?
 
 I checked ?gsub but still did not get the idea.
 
 Thank you again
 
 Elaine
 
 
 
 On Wed, Jul 31, 2013 at 9:35 PM, arun smartpink...@yahoo.com wrote:
 
 HI,
 
 Please use ?dput()
 mat1- as.matrix(read.table(text=
 
 D0989  D9820  D5629  D4327  D2134
 GID_1100  1  0
 GID_2011  0  0
 GID_4001  0  0
 GID_5110  0

Re: [R] merge matrix row data

2013-08-01 Thread Elaine Kuo
Hello Arun

Thank for comments.

You are right. GID is the first column in the matrix this time.

In the second row of the first column, it used to be GID 1 in the first
e-mail.
But you are also right. You answered it already, and  this time In the
second row of the first column is 1.
Below is part of dput()(too many columns)

.. .Names = c(GID,
D5291, D5293, D7414, D7415, D7416, D7417, D7418,
D7419, D7420, D7421, D7422, D7423, D7424, D7425,
D7426, D7427, D7428, D7429, D7430, D7431, D7432,
D7433, D7434, D7435, D7436, D7437, D7438, D7439,
D7440, D7441, D7442, D7443, D7444, D7445, D7446,

Elaine


On Thu, Aug 1, 2013 at 12:35 PM, arun smartpink...@yahoo.com wrote:



 Hi Elaine,
 I am not sure how your original matrix keeps on changing from the
 original post.  Here, your statement about rownames are 1, 2, 4 (the answer
 I already provided in the last post) and the matrix you showed looks
 different.  It seems like GID is the first column in the matrix.  I
 requested you to dput() the data to reduce these confusions.

 mat1-as.matrix(read.table(text=
 GID   D0989   D9820  D5629  D4327  D2134
 1   100   1  0
 2   011   0  0
 4   001   0  0
 5   110   0  0
 7   010   0  1
 ,sep=,header=TRUE))
 IslandA-c(GID 1, GID 5)
 IslandB- c(GID 2, GID 4, GID
 7)t(sapply(c(IslandA,IslandB),function(x) {x1-
 mat1[match(gsub(.*\\s+,,get(x)),mat1[,1]),-1];(!!colSums(x1))*1}))
 #D0989 D9820 D5629 D4327 D2134
 #IslandA 1 1 0 1 0
 #IslandB 0 1 1 0 1
 A.K.





 
 From: Elaine Kuo elaine.kuo...@gmail.com
 To: arun smartpink...@yahoo.com
 Cc: R help r-help@r-project.org
 Sent: Thursday, August 1, 2013 12:00 AM
 Subject: Re: [R] merge matrix row data



 Hello arun

 Thanks for the answers.
 I understand the answer to question 2.
 However, about question 1, sorry I did not clarify the question.

 1. If the row names are 1, 2, and 4 etc (numbers) instead of GID 1, GID 2,
 and GID 3,
is there any modification in need for the code ?
The original data looks like below.

 Original matrix
 GID   D0989   D9820  D5629  D4327  D2134
 1   100   1  0
 2   011   0  0
 4   001   0  0
 5   110   0  0
 7   010   0  1

 Resulting matrix
 D0989   D9820  D5629  D4327  D2134
 Island A   11   0   1   0
 Island B   01   1   0   1

 Elaine



 On Thu, Aug 1, 2013 at 7:15 AM, arun smartpink...@yahoo.com wrote:

 Hi Elaine,
 
 In that case:
 Do you have GID in the IslandA and IslandBs?
 
 
 IslandA-c(GID 1, GID 5)
 IslandB- c(GID 2, GID 4, GID 7)
 
 If there is no change in the two Islands, then using the same dataset:
 
 
 mat1- as.matrix(read.table(text=
 D0989  D9820  D5629  D4327  D2134
 GID_1100  1  0
 GID_2011  0  0
 GID_4001  0  0
 GID_5110  0  0
 GID_7010  0  1
 ,sep=,header=TRUE))
 
 row.names(mat1)- gsub(.*\\_,,row.names(mat1)) #to replace the GID_
 from the row.names()
  mat1
 
 #  D0989 D9820 D5629 D4327 D2134
 
 #1 1 0 0 1 0
 #2 0 1 1 0 0
 
 #4 0 0 1 0 0
 
 #5 1 1 0 0 0
 
 #7 0 1 0 0 1
 
  IslandA-c(GID 1, GID 5)
  IslandB- c(GID 2, GID 4, GID 7)
 res-t(sapply(c(IslandA,IslandB),function(x) {x1-
 mat1[match(gsub(.*\\s+,,get(x)),row.names(mat1)),];(!!colSums(x1))*1}))
 
 res
  #   D0989 D9820 D5629 D4327 D2134
 #IslandA 1 1 0 1 0
 #IslandB 0 1 1 0 1
 
 
 Regarding the use of !!colSums()
 You can check these:
 
  t(sapply(c(IslandA,IslandB),function(x) {x1-
 mat1[match(gsub(.*\\s+,,get(x)),row.names(mat1)),];!colSums(x1)}))
 
 #D0989 D9820 D5629 D4327 D2134
 #IslandA FALSE FALSE  TRUE FALSE  TRUE
 #IslandB  TRUE FALSE FALSE  TRUE FALSE
 
 t(sapply(c(IslandA,IslandB),function(x) {x1-
 mat1[match(gsub(.*\\s+,,get(x)),row.names(mat1)),];!!colSums(x1)}))
 
 #D0989 D9820 D5629 D4327 D2134
 #IslandA  TRUE  TRUE FALSE  TRUE FALSE
 #IslandB FALSE  TRUE  TRUE FALSE  TRUE
 
 # *1 will replace TRUE with 1 and FALSE with 0.
 
 A.K.
 
 
 
 
 
 
 
 
 From: Elaine Kuo elaine.kuo...@gmail.com
 To: arun smartpink...@yahoo.com
 Sent: Wednesday, July 31, 2013 6:58 PM
 
 Subject: Re: [R] merge matrix row data
 
 
 
 Dear Arun,
 
 Thank you for the clear explanation.
 The row.names question is a mistyping, for I do not have enough sleep
 last night.
 
 Two more questions
 
 1. If the row names are 1, 2, and 4 etc (numbers) instead

Re: [R] merge matrix row data

2013-08-01 Thread arun


Hi Elaine,
I am not sure how your original matrix keeps on changing from the original 
post.  Here, your statement about rownames are 1, 2, 4 (the answer I already 
provided in the last post) and the matrix you showed looks different.  It seems 
like GID is the first column in the matrix.  I requested you to dput() the data 
to reduce these confusions.

mat1-as.matrix(read.table(text=
GID   D0989   D9820  D5629  D4327  D2134
1   1    0    0   1  0
2   0    1    1   0  0
4   0    0    1   0  0
5   1    1    0   0  0
7   0    1    0   0  1
,sep=,header=TRUE))
IslandA-c(GID 1, GID 5)
IslandB- c(GID 2, GID 4, GID 
7)t(sapply(c(IslandA,IslandB),function(x) {x1- 
mat1[match(gsub(.*\\s+,,get(x)),mat1[,1]),-1];(!!colSums(x1))*1}))
#    D0989 D9820 D5629 D4327 D2134
#IslandA 1 1 0 1 0
#IslandB 0 1 1 0 1
A.K.






From: Elaine Kuo elaine.kuo...@gmail.com
To: arun smartpink...@yahoo.com 
Cc: R help r-help@r-project.org 
Sent: Thursday, August 1, 2013 12:00 AM
Subject: Re: [R] merge matrix row data



Hello arun

Thanks for the answers.
I understand the answer to question 2.
However, about question 1, sorry I did not clarify the question.

1. If the row names are 1, 2, and 4 etc (numbers) instead of GID 1, GID 2, and 
GID 3, 
   is there any modification in need for the code ?
   The original data looks like below.

Original matrix
GID       D0989   D9820  D5629  D4327  D2134
1               1    0    0   1  0
2               0    1    1   0  0
4               0    0    1   0  0
5               1    1    0   0  0
7               0    1    0   0  1

Resulting matrix
D0989   D9820  D5629  D4327  D2134
Island A   1    1   0   1   0
Island B   0    1   1   0   1

Elaine



On Thu, Aug 1, 2013 at 7:15 AM, arun smartpink...@yahoo.com wrote:

Hi Elaine,

In that case:
Do you have GID in the IslandA and IslandBs?


IslandA-c(GID 1, GID 5)
IslandB- c(GID 2, GID 4, GID 7)

If there is no change in the two Islands, then using the same dataset:


mat1- as.matrix(read.table(text=
D0989  D9820  D5629  D4327  D2134
GID_1    1    0    0  1  0
GID_2    0    1    1  0  0
GID_4    0    0    1  0  0
GID_5    1    1    0  0  0
GID_7    0    1    0  0  1
,sep=,header=TRUE))

row.names(mat1)- gsub(.*\\_,,row.names(mat1)) #to replace the GID_ from 
the row.names()
 mat1

#  D0989 D9820 D5629 D4327 D2134

#1 1 0 0 1 0
#2 0 1 1 0 0

#4 0 0 1 0 0

#5 1 1 0 0 0

#7 0 1 0 0 1

 IslandA-c(GID 1, GID 5)
 IslandB- c(GID 2, GID 4, GID 7)
res-t(sapply(c(IslandA,IslandB),function(x) {x1- 
mat1[match(gsub(.*\\s+,,get(x)),row.names(mat1)),];(!!colSums(x1))*1}))

res
 #   D0989 D9820 D5629 D4327 D2134
#IslandA 1 1 0 1 0
#IslandB 0 1 1 0 1


Regarding the use of !!colSums()
You can check these:

 t(sapply(c(IslandA,IslandB),function(x) {x1- 
mat1[match(gsub(.*\\s+,,get(x)),row.names(mat1)),];!colSums(x1)}))

#    D0989 D9820 D5629 D4327 D2134
#IslandA FALSE FALSE  TRUE FALSE  TRUE
#IslandB  TRUE FALSE FALSE  TRUE FALSE
 
t(sapply(c(IslandA,IslandB),function(x) {x1- 
mat1[match(gsub(.*\\s+,,get(x)),row.names(mat1)),];!!colSums(x1)}))

#    D0989 D9820 D5629 D4327 D2134
#IslandA  TRUE  TRUE FALSE  TRUE FALSE
#IslandB FALSE  TRUE  TRUE FALSE  TRUE

# *1 will replace TRUE with 1 and FALSE with 0.

A.K.








From: Elaine Kuo elaine.kuo...@gmail.com
To: arun smartpink...@yahoo.com
Sent: Wednesday, July 31, 2013 6:58 PM

Subject: Re: [R] merge matrix row data



Dear Arun, 

Thank you for the clear explanation.
The row.names question is a mistyping, for I do not have enough sleep last 
night.

Two more questions

1. If the row names are 1, 2, and 4 etc (numbers) instead of GID 1, GID 2, and 
GID 3, 
   is there any modification in need for the code ?

2. Please kindly explain the code
    (!!colSums(x1))*1}

   It is the critical part to merge the row data.

Thanks again.

Elaine



On Thu, Aug 1, 2013 at 6:45 AM, arun smartpink...@yahoo.com wrote:

Dear Elaine,

I used that line only because you didn't provide the data using dput().  So, 
I need to either use delimiter  , or just leave a space by first joining 
the GID and the numbers using _.  I chose the latter as I didn't had 
that much time to spent by putting , between each entries.  After that, I 
removed _ using the ?gsub().  As Bert pointed out, there are many online 
resources for understanding regular expression.

In this particular case, what I did was to single out the _ in the first 
pair

Re: [R] merge matrix row data

2013-08-01 Thread Elaine Kuo
Hello arun

I modified your code a little bit but failed to retrieve the row.names.

The response is NULL.

Please kindly help and thanks.

Elaine


Code

load(h:/b_W_line/R_workspace/dataset_Residence_2748.RData)

  dim(dataR)

  str(dataR)

  dataR.m- as.matrix(dataR, header=TRUE)

  dataR.m[,1]

  row.names(dataR.m)


dput(dataR.m)

.skipped...0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,

0L, 0L, 0L, 0L, 0L), .Dim = c(4873L, 14L), .Dimnames = list(NULL,

c(GID, D0407, D0409, D0410, D0462, D0463, D0473,

D0475, D0488, D0489, D0492, D0493, D0504, D0536

)))


On Thu, Aug 1, 2013 at 1:24 PM, Elaine Kuo elaine.kuo...@gmail.com wrote:

 Hello Arun

 Thank for comments.

 You are right. GID is the first column in the matrix this time.

 In the second row of the first column, it used to be GID 1 in the first
 e-mail.
 But you are also right. You answered it already, and  this time In the
 second row of the first column is 1.
 Below is part of dput()(too many columns)

 .. .Names = c(GID,
 D5291, D5293, D7414, D7415, D7416, D7417, D7418,
 D7419, D7420, D7421, D7422, D7423, D7424, D7425,
 D7426, D7427, D7428, D7429, D7430, D7431, D7432,
 D7433, D7434, D7435, D7436, D7437, D7438, D7439,
 D7440, D7441, D7442, D7443, D7444, D7445, D7446,
 
 Elaine


 On Thu, Aug 1, 2013 at 12:35 PM, arun smartpink...@yahoo.com wrote:



 Hi Elaine,
 I am not sure how your original matrix keeps on changing from the
 original post.  Here, your statement about rownames are 1, 2, 4 (the answer
 I already provided in the last post) and the matrix you showed looks
 different.  It seems like GID is the first column in the matrix.  I
 requested you to dput() the data to reduce these confusions.

 mat1-as.matrix(read.table(text=
 GID   D0989   D9820  D5629  D4327  D2134
 1   100   1  0
 2   011   0  0
 4   001   0  0
 5   110   0  0
 7   010   0  1
 ,sep=,header=TRUE))
 IslandA-c(GID 1, GID 5)
 IslandB- c(GID 2, GID 4, GID
 7)t(sapply(c(IslandA,IslandB),function(x) {x1-
 mat1[match(gsub(.*\\s+,,get(x)),mat1[,1]),-1];(!!colSums(x1))*1}))
 #D0989 D9820 D5629 D4327 D2134
 #IslandA 1 1 0 1 0
 #IslandB 0 1 1 0 1
 A.K.





 
 From: Elaine Kuo elaine.kuo...@gmail.com
 To: arun smartpink...@yahoo.com
 Cc: R help r-help@r-project.org
 Sent: Thursday, August 1, 2013 12:00 AM
 Subject: Re: [R] merge matrix row data



 Hello arun

 Thanks for the answers.
 I understand the answer to question 2.
 However, about question 1, sorry I did not clarify the question.

 1. If the row names are 1, 2, and 4 etc (numbers) instead of GID 1, GID
 2, and GID 3,
is there any modification in need for the code ?
The original data looks like below.

 Original matrix
 GID   D0989   D9820  D5629  D4327  D2134
 1   100   1  0
 2   011   0  0
 4   001   0  0
 5   110   0  0
 7   010   0  1

 Resulting matrix
 D0989   D9820  D5629  D4327  D2134
 Island A   11   0   1   0
 Island B   01   1   0   1

 Elaine



 On Thu, Aug 1, 2013 at 7:15 AM, arun smartpink...@yahoo.com wrote:

 Hi Elaine,
 
 In that case:
 Do you have GID in the IslandA and IslandBs?
 
 
 IslandA-c(GID 1, GID 5)
 IslandB- c(GID 2, GID 4, GID 7)
 
 If there is no change in the two Islands, then using the same dataset:
 
 
 mat1- as.matrix(read.table(text=
 D0989  D9820  D5629  D4327  D2134
 GID_1100  1  0
 GID_2011  0  0
 GID_4001  0  0
 GID_5110  0  0
 GID_7010  0  1
 ,sep=,header=TRUE))
 
 row.names(mat1)- gsub(.*\\_,,row.names(mat1)) #to replace the
 GID_ from the row.names()
  mat1
 
 #  D0989 D9820 D5629 D4327 D2134
 
 #1 1 0 0 1 0
 #2 0 1 1 0 0
 
 #4 0 0 1 0 0
 
 #5 1 1 0 0 0
 
 #7 0 1 0 0 1
 
  IslandA-c(GID 1, GID 5)
  IslandB- c(GID 2, GID 4, GID 7)
 res-t(sapply(c(IslandA,IslandB),function(x) {x1-
 mat1[match(gsub(.*\\s+,,get(x)),row.names(mat1)),];(!!colSums(x1))*1}))
 
 res
  #   D0989 D9820 D5629 D4327 D2134
 #IslandA 1 1 0 1 0
 #IslandB 0 1 1 0 1
 
 
 Regarding the use of !!colSums()
 You can check these:
 
  t(sapply(c(IslandA,IslandB),function(x) {x1-
 mat1[match(gsub(.*\\s+,,get(x)),row.names(mat1)),];!colSums(x1)}))
 
 #D0989 D9820 D5629 D4327 D2134
 #IslandA FALSE FALSE  TRUE FALSE  TRUE
 #IslandB  TRUE FALSE FALSE  TRUE FALSE
 
 t(sapply(c(IslandA,IslandB),function(x) {x1-
 mat1[match

Re: [R] merge matrix row data

2013-08-01 Thread arun
mat1-structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L), .Dim = c(20L, 
14L), .Dimnames = list(NULL, c(GID, D0407, D0409, D0410, 
D0462, D0463, D0473, D0475, D0488, D0489, D0492, 
D0493, D0504, D0536)))

IslandA-c(GID 1, GID 5)
IslandB- c(GID 2, GID 4, GID 7)
t(sapply(c(IslandA,IslandB),function(x) {x1- 
mat1[match(gsub(.*\\s+,,get(x)),mat1[,1]),-1];(!!colSums(x1))*1}))

 #  D0407 D0409 D0410 D0462 D0463 D0473 D0475 D0488 D0489 D0492 D0493 D0504
#IslandA 0 0 0 0 0 0 0 0 0 0 0 0
#IslandB 0 0 0 0 0 1 0 0 0 0 1 0
 #   D0536
#IslandA 0
#IslandB 1
A.K.






From: Elaine Kuo elaine.kuo...@gmail.com
To: arun smartpink...@yahoo.com 
Sent: Thursday, August 1, 2013 7:57 AM
Subject: Re: [R] merge matrix row data



Thanks.

Here you go.

structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L), .Dim = c(20L, 
14L), .Dimnames = list(NULL, c(GID, D0407, D0409, D0410, 
D0462, D0463, D0473, D0475, D0488, D0489, D0492, 
D0493, D0504, D0536)))

__
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] merge matrix row data

2013-08-01 Thread arun
HI Elaine,
From the error, it looks like there are cases where none of the elements in 
one of the element matches to the GID column of dataNP.m.
It's only a guess as you didn't provide  information about the Islands.

I was able to recreate the error you got.
dataNP.m-structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L), .Dim = c(20L, 
14L), .Dimnames = list(NULL, c(GID, D0407, D0409, D0410, 
D0462, D0463, D0473, D0475, D0488, D0489, D0492, 
D0493, D0504, D0536)))

Conti_Australia- c(1,14,18)
Conti_Malay- c(2,6,8)
Island_Sumatra- c(3,9,21)
Island_New_Guinea- c(22,24,28) #none of the elements are present in the GID 
column of example dataset
lapply(c(Conti_Australia,Conti_Malay,Island_Sumatra,Island_New_Guinea),function(x)
 {x1- match(get(x),dataNP.m[,1]); x2-x1[!is.na(x1)];colSums(x2)})
#Error in colSums(x2) : 'x' must be an array of at least two dimensions


lst1-sapply(c(Conti_Australia,Conti_Malay,Island_Sumatra,Island_New_Guinea),function(x)
 {x1- match(get(x),dataNP.m[,1]); x2-x1[!is.na(x1)];if(length(x2)0) 
(!!colSums(dataNP.m[x2,-1]))*1})

t(simplify2array(lst1[lapply(lst1,length)0]))
#    D0407 D0409 D0410 D0462 D0463 D0473 D0475 D0488 D0489 D0492
#Conti_Australia 0 0 0 0 0 1 0 0 0 0
#Conti_Malay 0 0 0 0 0 1 0 0 0 0
#Island_Sumatra  0 0 0 0 0 0 0 0 0 1
#    D0493 D0504 D0536
#Conti_Australia 0 0 1
#Conti_Malay 1 0 1
#Island_Sumatra  0 0 0

A.K.






From: Elaine Kuo elaine.kuo...@gmail.com
To: arun smartpink...@yahoo.com 
Sent: Thursday, August 1, 2013 9:20 AM
Subject: Re: [R] merge matrix row data



Hello arun, 

It is Elaine again.

After running the function in the last sentence of your code, 
an error message says
Error in colSums(x1) : 'x' must be an array of at least two dimensions


Please kindly help and thanks

Elaine

dput

 dput(head(dataNP.m,20))
structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L), .Dim = c(20L, 
14L), .Dimnames = list(NULL, c(GID, D0407, D0409, D0410, 
D0462, D0463, D0473, D0475, D0488, D0489, D0492, 
D0493, D0504, D0536)))

Code

 island- t(sapply(c( Conti_Australia,Conti_Korea,Conti_Malay,
+                    Island_Sumatra,Island_Bali,Island_Lombok,
+                    
Island_Lesser_Sunda,Island_Maluku,Island_Sulawesi,Island_Kepu_Sula,
+                    Island_New_Guinea,Island_Palawan,Island_Phillipines,
+                    Island_Hainan,Island_Taiwan,
+                    
Island_Sakishima,Island_Ryukyu,Island_Amami,Island_Osumi,
+                    
Island_Kyushu,Island_Shikoku,Island_Honshu,Island_Hokkaido,Island_Sakhalin),
+  function(x) {x1- 
dataNP.m[match(get

Re: [R] merge matrix row data

2013-08-01 Thread arun
Hi Elaine,

I didn't find any errors by running the code.
Regarding the first question, the former is of class numeric, and latter is 
character
library(foreign)
dataNP_1 -read.dbf(Anseriformes_13.dbf, as.is = FALSE)
##all the other vectors


res-t(sapply(c( Conti_Australia,Conti_Korea,Conti_Malay, 
   Island_Sumatra,Island_Bali,Island_Lombok, 
   
Island_Lesser_Sunda,Island_Maluku,Island_Sulawesi,Island_Kepu_Sula, 
   Island_New_Guinea,Island_Palawan,Island_Phillipines, 
   Island_Hainan,Island_Taiwan, 
   
Island_Sakishima,Island_Ryukyu,Island_Amami,Island_Osumi, 
   
Island_Kyushu,Island_Shikoku,Island_Honshu,Island_Hokkaido,Island_Sakhalin),
 
 function(x) {x1- match(get(x),dataNP_1[,1]);(!!colSums(dataNP_1[x1,-1]))*1})) 
head(res,3)
#    D0407 D0409 D0410 D0462 D0463 D0473 D0475 D0488 D0489 D0492
#Conti_Australia 0 0 1 0 0 0 0 0 0 0
#Conti_Korea 0 0 0 0 0 0 0 0 0 0
#Conti_Malay 0 0 0 1 0 0 0 0 0 0
 #   D0493 D0504 D0536
#Conti_Australia 0 0 0
#Conti_Korea 0 0 0
#Conti_Malay 0 0 0


A.K.




From: Elaine Kuo elaine.kuo...@gmail.com
To: arun smartpink...@yahoo.com 
Sent: Thursday, August 1, 2013 6:37 PM
Subject: Re: [R] merge matrix row data



Hello Arun, 

Thanks. I went to bed early and did not reply to your mail immediately.
I have some questions.

1. What is the difference between Conti_Australia- c(1,14,18) and  
Conti_Australia- c(1,14,18)
   Both generated the same error message: 
    Error in colSums(x1) : 'x' must be an array of at least two dimensions

2. I used your following code but found nothing NULL.
   lapply(c(Conti_Australia,Conti_Korea,Conti_Malay,
    Island_Sumatra,Island_Bali,Island_Lombok,

    
Island_Lesser_Sunda,Island_Maluku,Island_Sulawesi,Island_Kepu_Sula,

    Island_New_Guinea,Island_Palawan,Island_Phillipines,
    Island_Hainan,Island_Taiwan,

    
Island_Sakishima,Island_Ryukyu,Island_Amami,Island_Osumi,

    
Island_Kyushu,Island_Shikoku,Island_Honshu,Island_Hokkaido,Island_Sakhalin),
  function(x) {x1- dataNP.m[match(get(x),dataNP.m[,1]),-1]})

3. I attached the data of Anseriformes and code of islands for your reference.
    Thanks.

Elaine





On Thu, Aug 1, 2013 at 11:19 PM, arun smartpink...@yahoo.com wrote:



A typo:

none of the elements in one of the element
should be read as:
none of the elements in one of the Islands



- Original Message -
From: arun smartpink...@yahoo.com
To: Elaine Kuo elaine.kuo...@gmail.com

Cc: R help r-help@r-project.org
Sent: Thursday, August 1, 2013 11:18 AM
Subject: Re: [R] merge matrix row data

HI Elaine,
From the error, it looks like there are cases where none of the elements in 
one of the element matches to the GID column of dataNP.m.
It's only a guess as you didn't provide  information about the Islands.

I was able to recreate the error you got.
dataNP.m-structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L), .Dim = c(20L,
14L), .Dimnames = list(NULL, c(GID, D0407, D0409, D0410,
D0462, D0463, D0473, D0475, D0488, D0489, D0492,
D0493, D0504, D0536)))

Conti_Australia- c(1,14,18)
Conti_Malay- c(2,6,8)
Island_Sumatra- c(3,9,21)
Island_New_Guinea- c(22,24,28) #none of the elements are present in the GID 
column of example dataset
lapply(c(Conti_Australia,Conti_Malay,Island_Sumatra,Island_New_Guinea),function(x)
 {x1- match(get(x),dataNP.m[,1]); x2-x1[!is.na(x1)];colSums(x2)})
#Error in colSums(x2) : 'x' must be an array of at least two dimensions


lst1-sapply(c(Conti_Australia,Conti_Malay,Island_Sumatra,Island_New_Guinea),function(x)
 {x1

Re: [R] merge matrix row data

2013-07-31 Thread arun
HI,

Please use ?dput()
mat1- as.matrix(read.table(text=
D0989  D9820  D5629  D4327  D2134
GID_1    1    0    0  1  0
GID_2    0    1    1  0  0
GID_4    0    0    1  0  0
GID_5    1    1    0  0  0
GID_7    0    1    0  0  1
,sep=,header=TRUE))
row.names(mat1)- gsub([_], ,row.names(mat1))
IslandA-c(GID 1, GID 5)
IslandB- c(GID 2, GID 4, GID 7)
 res-  t(sapply(c(IslandA,IslandB),function(x) 
{x1-mat1[match(get(x),row.names(mat1)),];(!!colSums(x1))*1} ))

 res
#    D0989 D9820 D5629 D4327 D2134
#IslandA 1 1 0 1 0
#IslandB 0 1 1 0 1
A.K.




- Original Message -
From: Elaine Kuo elaine.kuo...@gmail.com
To: r-h...@stat.math.ethz.ch r-h...@stat.math.ethz.ch
Cc: 
Sent: Wednesday, July 31, 2013 9:03 AM
Subject: [R] merge matrix row data

Dear list,



I have a matrix showing the species presence-absence on a map.

Its rows are map locations, represented by GridCellID, such as GID1 and GID
5.

Its columns are species ID, such as D0989, D9820, and D5629.

The matrix is as followed.



Now I want to merge the GridCellID according to the map location of each
island.

For instance, Island A consist of GID 1 and 5. Island B consist of GID 2,
4, and 7.

In GID 1 and 5, species D0989 are both 1.

Then I want to merge GID 1 and 5 into Island A, with species D0989 as 1.

The original matrix and the resulting matrix are listed below.

Please kindly advise how to code the calculation in R.

Please do not hesitate to ask if anything is unclear.

Thank you in advance.



Elaine



Original matrix

        D0989   D9820  D5629  D4327  D2134

GID 1    1        0        0       1      0

GID 2    0        1        1       0      0

GID 4    0        0        1       0      0

GID 5    1        1        0       0      0

GID 7    0        1        0       0      1



Resulting matrix

                D0989   D9820  D5629  D4327  D2134

Island A   1        1       0       1       0

Island B   0        1       1       0       1

    [[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@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] merge matrix row data

2013-07-31 Thread Elaine Kuo
Dear Arun

Thank you for the very useful help.
However, please kindly explain the code below.
row.names(mat1)- gsub([_], ,row.names(mat1))

1. what does [_] mean?
2. what doesmean?
3. what does row.names(mat1) mean?

I checked ?gsub but still did not get the idea.

Thank you again

Elaine


On Wed, Jul 31, 2013 at 9:35 PM, arun smartpink...@yahoo.com wrote:

 HI,

 Please use ?dput()
 mat1- as.matrix(read.table(text=
 D0989  D9820  D5629  D4327  D2134
 GID_1100  1  0
 GID_2011  0  0
 GID_4001  0  0
 GID_5110  0  0
 GID_7010  0  1
 ,sep=,header=TRUE))
 row.names(mat1)- gsub([_], ,row.names(mat1))
 IslandA-c(GID 1, GID 5)
 IslandB- c(GID 2, GID 4, GID 7)
  res-  t(sapply(c(IslandA,IslandB),function(x)
 {x1-mat1[match(get(x),row.names(mat1)),];(!!colSums(x1))*1} ))

  res
 #D0989 D9820 D5629 D4327 D2134
 #IslandA 1 1 0 1 0
 #IslandB 0 1 1 0 1
 A.K.




 - Original Message -
 From: Elaine Kuo elaine.kuo...@gmail.com
 To: r-h...@stat.math.ethz.ch r-h...@stat.math.ethz.ch
 Cc:
 Sent: Wednesday, July 31, 2013 9:03 AM
 Subject: [R] merge matrix row data

 Dear list,



 I have a matrix showing the species presence-absence on a map.

 Its rows are map locations, represented by GridCellID, such as GID1 and GID
 5.

 Its columns are species ID, such as D0989, D9820, and D5629.

 The matrix is as followed.



 Now I want to merge the GridCellID according to the map location of each
 island.

 For instance, Island A consist of GID 1 and 5. Island B consist of GID 2,
 4, and 7.

 In GID 1 and 5, species D0989 are both 1.

 Then I want to merge GID 1 and 5 into Island A, with species D0989 as 1.

 The original matrix and the resulting matrix are listed below.

 Please kindly advise how to code the calculation in R.

 Please do not hesitate to ask if anything is unclear.

 Thank you in advance.



 Elaine



 Original matrix

 D0989   D9820  D5629  D4327  D2134

 GID 1100   1  0

 GID 2011   0  0

 GID 4001   0  0

 GID 5110   0  0

 GID 7010   0  1



 Resulting matrix

 D0989   D9820  D5629  D4327  D2134

 Island A   11   0   1   0

 Island B   01   1   0   1

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



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


  1   2   3   4   >