Re: [R] Match coordinates to regional polygon

2020-03-09 Thread Bert Gunter
I believe this would be better posted on r-sig-Geo  .

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 Mon, Mar 9, 2020 at 3:16 PM Miluji Sb  wrote:

> Dear all,
>
> I am trying to match a large number of coordinates (attached) sub-national
> regions using GADM shapefile.
>
> Coordinates:
>
> https://drive.google.com/file/d/1PUsi4d0wP7hB6Aps6UmpXsnIPSD3I1sT/view?usp=sharing
>
> Shapefile:
>
> https://drive.google.com/drive/folders/1DxyygjQNeg2GIS9doaFM8yM2LvBYZi8E?usp=sharing
>
>
> This is the code I am using;
>
> ###
> library(data.table)
> library(ncdf4)
> library(rgdal)
> library(raster)
> library(tidyverse)
>
> ## Read coordinates
> coords <- read.csv("coords.csv")
>
> ## Read shapefile
> world <-readOGR(dsn = "/gadm36_levels_shp", layer = "gadm36_2")
>
> ## Convert to spatial points
> pts <- coords
> coordinates(pts) <- ~lon+lat
>
> ## Provide the same CRS to the coordinates as the shapefile
> proj4string(pts) <- proj4string(world)
>
> ## Use over function to match coordinates to polygons
> pts_district <- over(pts, world)$NAME_1
> ###
>
> However, less than 20% of the coordinates are matched to a polygon. What am
> I doing wrong? Any help will be greatly appreciated.
>
> Regards,
>
> 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.
>

[[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] match() question or needle haystack problem for a data.frame

2018-10-22 Thread Knut Krueger

Am 22.10.18 um 18:02 schrieb Bert Gunter:
I suggest you spend a bit of time with an R tutorial or two and, in 
particular learn about "logical indexing," as this basic R construct 
seems to be mysterious to you.


Hi Bert,

especially the "match" help area is a little bit confusing.

And additional using a couple of programming languages with different 
logical operators and different syntax is misleading me sometimes to the 
wrong direction...


So the logic operations itself are not mysterious ;-)  but the 
translation to R.
And thank's a lot to give me the keyword to find for what I am was 
looking for: logical indexing.


I can not count the sites where I was looking to solve my problem .. 
with the wrong keywords ... :-)


Kind Regards Knut

__
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] match() question or needle haystack problem for a data.frame

2018-10-22 Thread Bert Gunter
I suggest you spend a bit of time with an R tutorial or two and, in
particular learn about "logical indexing," as this basic R construct seems
to be mysterious to you.

Cheers,
Bert




On Mon, Oct 22, 2018 at 8:22 AM Knut Krueger 
wrote:

> Am 22.10.18 um 17:01 schrieb Eric Berger:
> > v <- match(Mydata$DATA1, needles, nomatch=NA)
> >  > found <- Mydata[ !is.na (v), ]
> >  > missing <- Mdata[ is.na (v), ]
>
> Thank you it is working, additionally as Bert suggested, it seems that
>
> Mydata[Mydata$DATA1 %in% needles,]
>
> is doing the same.
>
> Kind Regards Knut
>
> __
> 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] match() question or needle haystack problem for a data.frame

2018-10-22 Thread Knut Krueger

Am 22.10.18 um 17:01 schrieb Eric Berger:

v <- match(Mydata$DATA1, needles, nomatch=NA)
 > found <- Mydata[ !is.na (v), ]
 > missing <- Mdata[ is.na (v), ]


Thank you it is working, additionally as Bert suggested, it seems that

Mydata[Mydata$DATA1 %in% needles,]

is doing the same.

Kind Regards Knut

__
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] match() question or needle haystack problem for a data.frame

2018-10-22 Thread Eric Berger
Hi Knut,
You are almost done.

> v <- match(Mydata$DATA1, needles, nomatch=NA)
> found <- Mydata[ !is.na(v), ]
> missing <- Mdata[ is.na(v), ]

HTH,
Eric


On Mon, Oct 22, 2018 at 5:51 PM Bert Gunter  wrote:

> Re-read ?match and note the examples for %in%
>
> -- 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 Mon, Oct 22, 2018 at 7:38 AM Knut Krueger 
> wrote:
>
> >
> > Hi to all
> >
> > I would like to reduce the "Mydata" to rows, only if Mydata$Data1 are in
> > needles
> >
> >
> >
> > needles =c(14390, 14391, 14392, 14427, 14428, 14429, 14430, 14431,
> > 14432, 14433, 14434, 14435, 14436, 14437, 14439, 14440, 14441, 15195,
> > 15196, 15197, 15198, 15199, 15200, 15201, 15202, 15203, 15204, 15205,
> > 15206, 15207, 15208, 15209, 17615, 17616, 17617, 17618, 17619, 17620,
> > 17621, 17622, 17623, 17624, 17625, 17626, 17627, 17628, 17629, 17630,
> > 17631, 17679, 17680, 17681, 17682, 17683, 17823, 17824, 17825, 17826,
> > 17827, 17828, 17829, 17830, 17831, 17862, 17863, 17864, 17865, 17866,
> > 17867, 17868, 17869, 17870, 17871, 17872, 17873, 17874, 17875, 17876,
> > 17877, 17878, 17879, 17880, 17881, 17882, 17883, 19255, 19256, 19257,
> > 19258, 21289, 21290, 21291, 21292, 22890, 22891, 22892, 22893, 22894,
> > 22895, 22896, 22897, 22898, 22899, 22900, 22901, 22902, 40428, 40429,
> > 40430, 40431, 40432, 40433, 40434, 40435, 40436, 40437)
> >
> > Haystack =c(14390, 14391, 14392, 14427, 14428, 14429, 14430, 14431,
> > 14432, 14433, 14434, 14435, 14436, 14437, 14439, 14440, 14441, 15187,
> > 15188, 15195, 15196, 15197, 15198, 15199, 15200, 15201, 15202, 15203,
> > 15204, 15205, 15206, 15207, 15208, 15209, 16717, 16718, 17615, 17616,
> > 17617, 17618, 17619, 17620, 17621, 17622, 17623, 17624, 17625, 17626,
> > 17627, 17628, 17629, 17630, 17631, 17679, 17680, 17681, 17682, 17683,
> > 17817, 17818, 17823, 17824, 17825, 17826, 17827, 17828, 17829, 17830,
> > 17831, 17862, 17863, 17864, 17865, 17866, 17867, 17868, 17869, 17870,
> > 17871, 17872, 17873, 17874, 17875, 17876, 17877, 17878, 17879, 17880,
> > 17881, 17882, 17883, 17886, 19255, 19256, 19257, 19258, 21289, 21290,
> > 21291, 21292, 22890, 22891, 22892, 22893, 22894, 22895, 22896, 22897,
> > 22898, 22899, 22900, 22901, 22902, 40428, 40429, 40430, 40431, 40432,
> > 40433, 40434, 40435, 40436, 40437, 40710, 40711, 49127, 49128, 52768)
> >
> > Mydata =data.frame (DATA1=Haystack, Data2=c(1:length(Haystack)))
> >
> >
> >
> > match(Mydata$DATA1, needles, nomatch=NA) does find all data which are in
> > needle - the others are set to the nomatch value.
> >
> > But I don not find out how to reduce the data.frame  -  maybe match() is
> > not helpful for that.
> >
> > Kind regards Knut
> >
> > __
> > 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.
>

[[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] match() question or needle haystack problem for a data.frame

2018-10-22 Thread Bert Gunter
Re-read ?match and note the examples for %in%

-- 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 Mon, Oct 22, 2018 at 7:38 AM Knut Krueger 
wrote:

>
> Hi to all
>
> I would like to reduce the "Mydata" to rows, only if Mydata$Data1 are in
> needles
>
>
>
> needles =c(14390, 14391, 14392, 14427, 14428, 14429, 14430, 14431,
> 14432, 14433, 14434, 14435, 14436, 14437, 14439, 14440, 14441, 15195,
> 15196, 15197, 15198, 15199, 15200, 15201, 15202, 15203, 15204, 15205,
> 15206, 15207, 15208, 15209, 17615, 17616, 17617, 17618, 17619, 17620,
> 17621, 17622, 17623, 17624, 17625, 17626, 17627, 17628, 17629, 17630,
> 17631, 17679, 17680, 17681, 17682, 17683, 17823, 17824, 17825, 17826,
> 17827, 17828, 17829, 17830, 17831, 17862, 17863, 17864, 17865, 17866,
> 17867, 17868, 17869, 17870, 17871, 17872, 17873, 17874, 17875, 17876,
> 17877, 17878, 17879, 17880, 17881, 17882, 17883, 19255, 19256, 19257,
> 19258, 21289, 21290, 21291, 21292, 22890, 22891, 22892, 22893, 22894,
> 22895, 22896, 22897, 22898, 22899, 22900, 22901, 22902, 40428, 40429,
> 40430, 40431, 40432, 40433, 40434, 40435, 40436, 40437)
>
> Haystack =c(14390, 14391, 14392, 14427, 14428, 14429, 14430, 14431,
> 14432, 14433, 14434, 14435, 14436, 14437, 14439, 14440, 14441, 15187,
> 15188, 15195, 15196, 15197, 15198, 15199, 15200, 15201, 15202, 15203,
> 15204, 15205, 15206, 15207, 15208, 15209, 16717, 16718, 17615, 17616,
> 17617, 17618, 17619, 17620, 17621, 17622, 17623, 17624, 17625, 17626,
> 17627, 17628, 17629, 17630, 17631, 17679, 17680, 17681, 17682, 17683,
> 17817, 17818, 17823, 17824, 17825, 17826, 17827, 17828, 17829, 17830,
> 17831, 17862, 17863, 17864, 17865, 17866, 17867, 17868, 17869, 17870,
> 17871, 17872, 17873, 17874, 17875, 17876, 17877, 17878, 17879, 17880,
> 17881, 17882, 17883, 17886, 19255, 19256, 19257, 19258, 21289, 21290,
> 21291, 21292, 22890, 22891, 22892, 22893, 22894, 22895, 22896, 22897,
> 22898, 22899, 22900, 22901, 22902, 40428, 40429, 40430, 40431, 40432,
> 40433, 40434, 40435, 40436, 40437, 40710, 40711, 49127, 49128, 52768)
>
> Mydata =data.frame (DATA1=Haystack, Data2=c(1:length(Haystack)))
>
>
>
> match(Mydata$DATA1, needles, nomatch=NA) does find all data which are in
> needle - the others are set to the nomatch value.
>
> But I don not find out how to reduce the data.frame  -  maybe match() is
> not helpful for that.
>
> Kind regards Knut
>
> __
> 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] match and new columns

2017-12-15 Thread Ek Esawi
Hi Val—

Here is something similar to what Bill suggested.

wainb <- which(tdat$A %in% tdat$B)
tdat[c(length(tdat$D)-1,length(tdat$D)),c("D","E")] <-
tdat[wainb,c("B","C")]

HTH

EK

On Wed, Dec 13, 2017 at 4:36 PM, Val  wrote:

> Hi all,
>
> I have a data frame
> tdat <- read.table(textConnection("A B C Y
> A12 B03 C04 0.70
> A23 B05 C06 0.05
> A14 B06 C07 1.20
> A25 A23 A12 3.51
> A16 A25 A14 2,16"),header = TRUE)
>
> I want match tdat$B with tdat$A and populate the  column   values of tdat$A
> ( col A and Col B) in the newly created columns (col D and col  E).  please
> find my attempt and the desired output below
>
> Desired output
> A B C Y  D E
> A12 B03 C04 0.70  0  0
> A23 B05 C06 0.05  0  0
> A14 B06 C07 1.20  0  0
> A25 A23 A12 3.51 B05 C06
> A16 A25 A14 2,16 A23 A12
>
> my attempt,
>
> tdat$D <- 0
> tdat$E <- 0
>
> if(tdat$B %in% tdat$A)
>   {
>   tdat$D <- tdat$A[tdat$B]
>   tdat$E <- tdat$A[tdat$C]
> }
>  but did not work.
>
> Thank you in advance
>
> [[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] match and new columns

2017-12-13 Thread William Dunlap via R-help
Try the following (which won't work with factors):

> i <- match(tdat$B, tdat$A)
> newColumns <- tdat[i, c("B", "C")]
> newColumns[is.na(newColumns)] <- "0"
> transform(tdat, D=newColumns[["B"]], E=newColumns[["C"]])
A   B   CY   D   E
1 A12 B03 C04 0.70   0   0
2 A23 B05 C06 0.05   0   0
3 A14 B06 C07 1.20   0   0
4 A25 A23 A12 3.51 B05 C06
5 A16 A25 A14 2,16 A23 A12


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, Dec 13, 2017 at 5:37 PM, Val  wrote:

> Hi Bill,
>
> I put stringsAsFactors = FALSE
>  still did not work.
>
> tdat <- read.table(textConnection("A B C Y
> A12 B03 C04 0.70
> A23 B05 C06 0.05
> A14 B06 C07 1.20
> A25 A23 A12 3.51
> A16 A25 A14 2,16
> "),header =
> TRUE ,stringsAsFactors = FALSE)
> tdat$D <- 0
> tdat$E <- 0
>
> tdat$D <- (ifelse(tdat$B %in% tdat$A, tdat$A[tdat$B], 0))
> tdat$E <- (ifelse(tdat$B %in% tdat$A, tdat$A[tdat$C], 0))
> tdat
>
> I got this,
>
>  A  B  C   Y   DE1 A12 
>  B03 C04 0.7000
> 2 A23 B05 C06 0.05003 A14 
>  B06 C07 1.2000
> 4 A25 A23 A12 3.51  
> 5 A16 A25 A14 2,16  
>
>
>
>
>
> On Wed, Dec 13, 2017 at 7:23 PM, William Dunlap  wrote:
>
>> Use the stringsAsFactors=FALSE argument to read.table when
>> making your data.frame - factors are getting in your way here.
>>
>> Bill Dunlap
>> TIBCO Software
>> wdunlap tibco.com
>>
>> On Wed, Dec 13, 2017 at 3:02 PM, Val  wrote:
>>
>>> Thank you Rui,
>>> I did not get the desired result. Here is the output from your script
>>>
>>>A   B   CY D E
>>> 1 A12  B03 C04
>>> 0.70 0 0
>>> 2 A23 B05 C06 0.05 0 0
>>> 3 A14  B06 C07
>>> 1.20 0 0
>>> 4 A25 A23 A12 3.51 1 1
>>> 5 A16 A25 A14 2,16 4
>>>  4
>>>
>>>
>>> On Wed, Dec 13, 2017 at 4:36 PM, Rui Barradas 
>>> wrote:
>>>
>>> > Hello,
>>> >
>>> > Here is one way.
>>> >
>>> > tdat$D <- ifelse(tdat$B %in% tdat$A, tdat$A[tdat$B], 0)
>>> > tdat$E <- ifelse(tdat$B %in% tdat$A, tdat$A[tdat$C], 0)
>>> >
>>> >
>>> > Hope this helps,
>>> >
>>> > Rui Barradas
>>> >
>>> >
>>> > On 12/13/2017 9:36 PM, Val wrote:
>>> >
>>> >> Hi all,
>>> >>
>>> >> I have a data frame
>>> >> tdat <- read.table(textConnection("A B C Y
>>> >> A12 B03 C04 0.70
>>> >> A23 B05 C06 0.05
>>> >> A14 B06 C07 1.20
>>> >> A25 A23 A12 3.51
>>> >> A16 A25 A14 2,16
>>> "),header =
>>> TRUE)
>>> >>
>>> >> I want match tdat$B with tdat$A and populate the  column   values of
>>> >> tdat$A
>>> >> ( col A and Col B) in the newly created columns (col D and col  E).
>>> >> please
>>> >> find my attempt and the desired output below
>>> >>
>>> >> Desired output
>>> >> A B C Y  D E
>>> >> A12 B03 C04 0.70  0  0
>>> >> A23 B05 C06 0.05  0  0
>>> >> A14 B06 C07 1.20  0  0
>>> >> A25 A23 A12 3.51 B05 C06
>>> >> A16 A25 A14 2,16 A23 A12
>>> 
>>> >>
>>> >> my attempt,
>>> >>
>>> >> tdat$D <- 0
>>> >> tdat$E <- 0
>>> >>
>>> >> if(tdat$B %in% tdat$A)
>>> >>{
>>> >>tdat$D <- tdat$A[tdat$B]
>>> >>tdat$E <- tdat$A[tdat$C]
>>> >> }
>>> >>   but did not work.
>>> >>
>>> >> Thank you in advance
>>> >>
>>> >> [[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/posti
>>> >> ng-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/posti
>>> ng-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] match and new columns

2017-12-13 Thread Val
Hi Bill,

I put stringsAsFactors = FALSE
 still did not work.

tdat <- read.table(textConnection("A B C Y
A12 B03 C04 0.70
A23 B05 C06 0.05
A14 B06 C07 1.20
A25 A23 A12 3.51
A16 A25 A14 2,16"),header = TRUE ,stringsAsFactors = FALSE)
tdat$D <- 0
tdat$E <- 0

tdat$D <- (ifelse(tdat$B %in% tdat$A, tdat$A[tdat$B], 0))
tdat$E <- (ifelse(tdat$B %in% tdat$A, tdat$A[tdat$C], 0))
tdat

I got this,

 A  B  C   Y   DE
1 A12 B03 C04 0.7000
2 A23 B05 C06 0.0500
3 A14 B06 C07 1.2000
4 A25 A23 A12 3.51  
5 A16 A25 A14 2,16  





On Wed, Dec 13, 2017 at 7:23 PM, William Dunlap  wrote:

> Use the stringsAsFactors=FALSE argument to read.table when
> making your data.frame - factors are getting in your way here.
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Wed, Dec 13, 2017 at 3:02 PM, Val  wrote:
>
>> Thank you Rui,
>> I did not get the desired result. Here is the output from your script
>>
>>A   B   CY D E
>> 1 A12  B03 C04
>> 0.70 0 0
>> 2 A23 B05 C06 0.05 0 0
>> 3 A14  B06 C07
>> 1.20 0 0
>> 4 A25 A23 A12 3.51 1 1
>> 5 A16 A25 A14 2,16 4
>>  4
>>
>>
>> On Wed, Dec 13, 2017 at 4:36 PM, Rui Barradas 
>> wrote:
>>
>> > Hello,
>> >
>> > Here is one way.
>> >
>> > tdat$D <- ifelse(tdat$B %in% tdat$A, tdat$A[tdat$B], 0)
>> > tdat$E <- ifelse(tdat$B %in% tdat$A, tdat$A[tdat$C], 0)
>> >
>> >
>> > Hope this helps,
>> >
>> > Rui Barradas
>> >
>> >
>> > On 12/13/2017 9:36 PM, Val wrote:
>> >
>> >> Hi all,
>> >>
>> >> I have a data frame
>> >> tdat <- read.table(textConnection("A B C Y
>> >> A12 B03 C04 0.70
>> >> A23 B05 C06 0.05
>> >> A14 B06 C07 1.20
>> >> A25 A23 A12 3.51
>> >> A16 A25 A14 2,16
>> "),header =
>> TRUE)
>> >>
>> >> I want match tdat$B with tdat$A and populate the  column   values of
>> >> tdat$A
>> >> ( col A and Col B) in the newly created columns (col D and col  E).
>> >> please
>> >> find my attempt and the desired output below
>> >>
>> >> Desired output
>> >> A B C Y  D E
>> >> A12 B03 C04 0.70  0  0
>> >> A23 B05 C06 0.05  0  0
>> >> A14 B06 C07 1.20  0  0
>> >> A25 A23 A12 3.51 B05 C06
>> >> A16 A25 A14 2,16 A23 A12
>> 
>> >>
>> >> my attempt,
>> >>
>> >> tdat$D <- 0
>> >> tdat$E <- 0
>> >>
>> >> if(tdat$B %in% tdat$A)
>> >>{
>> >>tdat$D <- tdat$A[tdat$B]
>> >>tdat$E <- tdat$A[tdat$C]
>> >> }
>> >>   but did not work.
>> >>
>> >> Thank you in advance
>> >>
>> >> [[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/posti
>> >> ng-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/posti
>> ng-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] match and new columns

2017-12-13 Thread William Dunlap via R-help
Use the stringsAsFactors=FALSE argument to read.table when
making your data.frame - factors are getting in your way here.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, Dec 13, 2017 at 3:02 PM, Val  wrote:

> Thank you Rui,
> I did not get the desired result. Here is the output from your script
>
>A   B   CY D E
> 1 A12 B03 C04 0.70 0 0
> 2 A23 B05 C06 0.05 0 0
> 3 A14 B06 C07 1.20 0 0
> 4 A25 A23 A12 3.51 1 1
> 5 A16 A25 A14 2,16 4 4
>
>
> On Wed, Dec 13, 2017 at 4:36 PM, Rui Barradas 
> wrote:
>
> > Hello,
> >
> > Here is one way.
> >
> > tdat$D <- ifelse(tdat$B %in% tdat$A, tdat$A[tdat$B], 0)
> > tdat$E <- ifelse(tdat$B %in% tdat$A, tdat$A[tdat$C], 0)
> >
> >
> > Hope this helps,
> >
> > Rui Barradas
> >
> >
> > On 12/13/2017 9:36 PM, Val wrote:
> >
> >> Hi all,
> >>
> >> I have a data frame
> >> tdat <- read.table(textConnection("A B C Y
> >> A12 B03 C04 0.70
> >> A23 B05 C06 0.05
> >> A14 B06 C07 1.20
> >> A25 A23 A12 3.51
> >> A16 A25 A14 2,16"),header = TRUE)
> >>
> >> I want match tdat$B with tdat$A and populate the  column   values of
> >> tdat$A
> >> ( col A and Col B) in the newly created columns (col D and col  E).
> >> please
> >> find my attempt and the desired output below
> >>
> >> Desired output
> >> A B C Y  D E
> >> A12 B03 C04 0.70  0  0
> >> A23 B05 C06 0.05  0  0
> >> A14 B06 C07 1.20  0  0
> >> A25 A23 A12 3.51 B05 C06
> >> A16 A25 A14 2,16 A23 A12
> >>
> >> my attempt,
> >>
> >> tdat$D <- 0
> >> tdat$E <- 0
> >>
> >> if(tdat$B %in% tdat$A)
> >>{
> >>tdat$D <- tdat$A[tdat$B]
> >>tdat$E <- tdat$A[tdat$C]
> >> }
> >>   but did not work.
> >>
> >> Thank you in advance
> >>
> >> [[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/posti
> >> ng-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.
>

[[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] match and new columns

2017-12-13 Thread Val
Thank you Rui,
I did not get the desired result. Here is the output from your script

   A   B   CY D E
1 A12 B03 C04 0.70 0 0
2 A23 B05 C06 0.05 0 0
3 A14 B06 C07 1.20 0 0
4 A25 A23 A12 3.51 1 1
5 A16 A25 A14 2,16 4 4


On Wed, Dec 13, 2017 at 4:36 PM, Rui Barradas  wrote:

> Hello,
>
> Here is one way.
>
> tdat$D <- ifelse(tdat$B %in% tdat$A, tdat$A[tdat$B], 0)
> tdat$E <- ifelse(tdat$B %in% tdat$A, tdat$A[tdat$C], 0)
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> On 12/13/2017 9:36 PM, Val wrote:
>
>> Hi all,
>>
>> I have a data frame
>> tdat <- read.table(textConnection("A B C Y
>> A12 B03 C04 0.70
>> A23 B05 C06 0.05
>> A14 B06 C07 1.20
>> A25 A23 A12 3.51
>> A16 A25 A14 2,16"),header = TRUE)
>>
>> I want match tdat$B with tdat$A and populate the  column   values of
>> tdat$A
>> ( col A and Col B) in the newly created columns (col D and col  E).
>> please
>> find my attempt and the desired output below
>>
>> Desired output
>> A B C Y  D E
>> A12 B03 C04 0.70  0  0
>> A23 B05 C06 0.05  0  0
>> A14 B06 C07 1.20  0  0
>> A25 A23 A12 3.51 B05 C06
>> A16 A25 A14 2,16 A23 A12
>>
>> my attempt,
>>
>> tdat$D <- 0
>> tdat$E <- 0
>>
>> if(tdat$B %in% tdat$A)
>>{
>>tdat$D <- tdat$A[tdat$B]
>>tdat$E <- tdat$A[tdat$C]
>> }
>>   but did not work.
>>
>> Thank you in advance
>>
>> [[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/posti
>> ng-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] match and new columns

2017-12-13 Thread Rui Barradas

Hello,

Here is one way.

tdat$D <- ifelse(tdat$B %in% tdat$A, tdat$A[tdat$B], 0)
tdat$E <- ifelse(tdat$B %in% tdat$A, tdat$A[tdat$C], 0)


Hope this helps,

Rui Barradas

On 12/13/2017 9:36 PM, Val wrote:

Hi all,

I have a data frame
tdat <- read.table(textConnection("A B C Y
A12 B03 C04 0.70
A23 B05 C06 0.05
A14 B06 C07 1.20
A25 A23 A12 3.51
A16 A25 A14 2,16"),header = TRUE)

I want match tdat$B with tdat$A and populate the  column   values of tdat$A
( col A and Col B) in the newly created columns (col D and col  E).  please
find my attempt and the desired output below

Desired output
A B C Y  D E
A12 B03 C04 0.70  0  0
A23 B05 C06 0.05  0  0
A14 B06 C07 1.20  0  0
A25 A23 A12 3.51 B05 C06
A16 A25 A14 2,16 A23 A12

my attempt,

tdat$D <- 0
tdat$E <- 0

if(tdat$B %in% tdat$A)
   {
   tdat$D <- tdat$A[tdat$B]
   tdat$E <- tdat$A[tdat$C]
}
  but did not work.

Thank you in advance

[[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] Match ISO 8601 week-of-year numbers to month-of-year numbers on Windows with German locale

2017-01-18 Thread Janko Thyson
Hi Steve,

and thanks so much for taking the time to draft your solution! After
running through it looks like it's EXACTLY what I was looking/hoping
for!

For those interested: I also tried to get this in as a feature request
for the lubridate package >>
https://github.com/hadley/lubridate/issues/506

Here's to open source, social coding and our active user community! :-)

Janko

On Wed, Jan 18, 2017 at 1:35 PM, S Ellison  wrote:
>
>
>> -Original Message-
>> (yw <- format(posix, "%Y-%V"))
>> > # [1] "2015-52" "2015-53" "2016-53" "2016-01"
>>
>> Which, after checking back with a calendar, would give me reason to believe
>> that it using %V does in fact seem to work: it's an input to `format()` and R
>> doesn't seem to ignore it as the correct week numbers (following ISO 8601)
>> are returned.
>
> Unfortunately it does _not_ work as an input to strptime() or as.Date(). Try
> strptime(sprintf("2016-%02d", 1:52), "%Y-%V")
>
> which all return the same (rather arbitrary-looking) date. (R 3.3.1 on 
> Windows 7)
>
> So you can easily extract the week number from a date, but you can't extract 
> the month number from a week number using base R.
>
> What you _could_ do is assign an appropriate weekday in each week and use 
> that day to assign a month. Using the 8601 rules, Thursday seems sensible as 
> that is the weekday used to define the week number. The following crude 
> implementation (which allows some variation in the input string and a custom 
> weekday defaulting to Thursday) seems to work in my UK locale, though I've 
> not spent a lot of time debugging. You should be able to get it to work with 
> minimal tweaking in another locale:
>
> yw.to.month <- function(yw, yearformat="%Y", separator="-", monthformat="%m", 
> weekday=4) {
> # yw  is a character vector assumed to be in %Y-%W" or "%y-%W"
> # form (or with another separator specified by separator)
> # yearformat is a character string used by as.Date to convert the 
> year.
> # separator is the year-week separator string in yw
> # monthformat is the output format, passed to format.Date
> # weekday is the (numerica) day of the week to be used to place the 
> week
> # in a specific month. The default, 4, takes the Thursday.
>
>
> #Get the year as a character vector
> Y <- format(as.Date(yw, yearformat), "%Y")
>
> #Get the date of the first thursday in each year
> Jan01 <- as.Date(sprintf("%s-01-01", Y), "%Y-%m-%d") #constructs 1st 
> Jan for each year
> J1.w <- as.numeric( format(Jan01, "%w") ) #numerical day of week for 
> jan 1st
> date.increment <- ifelse(J1.w > weekday, 7 + weekday - J1.w, weekday 
> - J1.w) #How far to first thursday?
> Thursday1 <- as.Date( Jan01 + date.increment ) #date for first 
> thursday
>
> #Add the week number in yw to get the corresponding Thursday's date
> wknum <- as.numeric( gsub(sprintf(".+%s(.+)", separator), "\\1", yw) )
> Thursdays <- Thursday1 + 7 * (wknum - 1)
>
> #... and convert each Thursday date to month number
> # using supplied monthformat
>
> format( Thursdays, monthformat )
> }
>
> #Examples
> #Construct a collection of "-WW" strings:
> yw <- sprintf("%4d-%02d", rep(2015:2017, each=53), rep(1:53, 3))
> #NB: Week 53 does not exist in all years. e.g 'week 53' of 2016 is 
> week 1 of 2017.
> #But the week number is just used as an offset from the first 
> specified weekday,
> #so the function does returns a valid year and month if the week 
> number is
> #after the year end.
>
> yw.to.month(yw)
>
> yw.to.month(yw, monthformat="%Y-%m")
>
> #Use the month in which the Monday falls:
> yw.to.month(yw, monthformat="%Y-%m", weekday=1)
>
>
>
> Steve E
>
>
>
>
>
>
>
>
> ***
> This email and any attachments are confidential. Any u...{{dropped:8}}

__
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] Match ISO 8601 week-of-year numbers to month-of-year numbers on Windows with German locale

2017-01-18 Thread S Ellison


> -Original Message-
> (yw <- format(posix, "%Y-%V"))
> > # [1] "2015-52" "2015-53" "2016-53" "2016-01"
> 
> Which, after checking back with a calendar, would give me reason to believe
> that it using %V does in fact seem to work: it's an input to `format()` and R
> doesn't seem to ignore it as the correct week numbers (following ISO 8601)
> are returned.

Unfortunately it does _not_ work as an input to strptime() or as.Date(). Try
strptime(sprintf("2016-%02d", 1:52), "%Y-%V")

which all return the same (rather arbitrary-looking) date. (R 3.3.1 on Windows 
7)

So you can easily extract the week number from a date, but you can't extract 
the month number from a week number using base R.

What you _could_ do is assign an appropriate weekday in each week and use that 
day to assign a month. Using the 8601 rules, Thursday seems sensible as that is 
the weekday used to define the week number. The following crude implementation 
(which allows some variation in the input string and a custom weekday 
defaulting to Thursday) seems to work in my UK locale, though I've not spent a 
lot of time debugging. You should be able to get it to work with minimal 
tweaking in another locale:

yw.to.month <- function(yw, yearformat="%Y", separator="-", monthformat="%m", 
weekday=4) {
# yw  is a character vector assumed to be in %Y-%W" or "%y-%W"
# form (or with another separator specified by separator)
# yearformat is a character string used by as.Date to convert the year.
# separator is the year-week separator string in yw
# monthformat is the output format, passed to format.Date
# weekday is the (numerica) day of the week to be used to place the 
week 
# in a specific month. The default, 4, takes the Thursday.


#Get the year as a character vector
Y <- format(as.Date(yw, yearformat), "%Y")

#Get the date of the first thursday in each year
Jan01 <- as.Date(sprintf("%s-01-01", Y), "%Y-%m-%d") #constructs 1st 
Jan for each year
J1.w <- as.numeric( format(Jan01, "%w") ) #numerical day of week for 
jan 1st
date.increment <- ifelse(J1.w > weekday, 7 + weekday - J1.w, weekday - 
J1.w) #How far to first thursday?
Thursday1 <- as.Date( Jan01 + date.increment ) #date for first thursday

#Add the week number in yw to get the corresponding Thursday's date
wknum <- as.numeric( gsub(sprintf(".+%s(.+)", separator), "\\1", yw) )
Thursdays <- Thursday1 + 7 * (wknum - 1)

#... and convert each Thursday date to month number 
# using supplied monthformat

format( Thursdays, monthformat )
}

#Examples
#Construct a collection of "-WW" strings:
yw <- sprintf("%4d-%02d", rep(2015:2017, each=53), rep(1:53, 3))
#NB: Week 53 does not exist in all years. e.g 'week 53' of 2016 is week 
1 of 2017. 
#But the week number is just used as an offset from the first specified 
weekday,
#so the function does returns a valid year and month if the week number 
is 
#after the year end. 

yw.to.month(yw)

yw.to.month(yw, monthformat="%Y-%m")

#Use the month in which the Monday falls:
yw.to.month(yw, monthformat="%Y-%m", weekday=1)



Steve E








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

__
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] Match ISO 8601 week-of-year numbers to month-of-year numbers on Windows with German locale

2017-01-14 Thread peter dalgaard

> On 12 Jan 2017, at 22:20 , Nordlund, Dan (DSHS/RDA) <nord...@dshs.wa.gov> 
> wrote:
> 
> See comments inline.
> 
>> -Original Message-
>> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Bob Rudis
>> Sent: Thursday, January 12, 2017 12:41 PM
>> To: David Winsemius
>> Cc: r-help@r-project.org
>> Subject: Re: [R] Match ISO 8601 week-of-year numbers to month-of-year
>> numbers on Windows with German locale
>> 
>> Aye, but this:
>> 
>>  some_dates <- as.POSIXct(c("2015-12-24", "2015-12-31", "2016-01-01",
>> "2016-01-08"))
>> 
>>  (year_week <- format(some_dates, "%Y-%U"))
>>  ## [1] "2015-51" "2015-52" "2016-00" "2016-01"
>> 
>>  (year_week_day <- sprintf("%s-1", year_week))
>>  ## [1] "2015-51-1" "2015-52-1" "2016-00-1" "2016-01-1"
>> 
>>  (as.POSIXct(year_week_day, format = "%Y-%U-%u"))
>>  ## [1] "2015-12-21 EST" "2015-12-28 EST" "2016-01-04 EST" "2016-01-04 EST"
>> 
>> works fine on macOS & Linux (Ubuntu, anyway), but it fails on Windows (10,
>> 64bit, R 3.3.2):
>> 
>>  (as.POSIXct(year_week_day, format = "%Y-%U-%u"))
>>  ## [1] "2015-12-21 PST" "2015-12-28 PST" NA   "2016-01-04 PST"
> 
> Why do you say it works fine on Ubuntu?  The date "2016-01-01" is in week 0 
> of 2016.  There is no Monday in week 0.  So I would argue the return  value 
> of NA from Windows is more appropriate than returning the date of Monday in 
> week 1 of 2016 like Ubuntu does.

Yes, the Ubuntu output suggests that Jan 4 is Monday of both w1 and w0...! 

But I'm puzzled: Around here, we actually use week numbers (for planning), but 
they are never zero: 

2016-01-01 was Friday, week 53 (of 2015)
2014-12-31 was Wednesday, week 1 (still 2015).

So it would seem that any logic that converts dates to/from week numbers would 
need to be rather more careful than to try and use %Y for the year...

-pd

> 
> Dan
> 
> Daniel Nordlund, PhD
> Research and Data Analysis Division
> Services & Enterprise Support Administration
> Washington State Department of Social and Health Services
> 
>> 
>> On 1/12/17, David Winsemius <dwinsem...@comcast.net> wrote:
>>> 
>>>> On Jan 12, 2017, at 8:14 AM, Janko Thyson <janko.thy...@gmail.com>
>> wrote:
>>>> 
>>>> Dear list,
>>>> 
>>>> I'm experiencing problems with converting strings of the format
>>>> "-" (e.g. 2016-01, 2016-52) to proper POSIX dates
>>>> which (I
>>>> think) I need in order to retrieve the month-of-the-year number.
>>>> 
>>>> Simpler put: I'd like to match week-of-the-year numbers to
>>>> month-of-the-year numbers. Ideally, the week-of-the-year number
>> would
>>>> follow the ISO 8601 convention (i.e. format argument "%V") instead of
>>>> the US (format argument "%U") or UK (format argument "%W")
>> convention.
>>>> 
>>>> After posting this to Stackoverflow, I have strong reasons to believe
>>>> that the issue is caused by Windows:
>>>> http://stackoverflow.com/questions/41616407/match-iso-8601-week-
>> numbe
>>>> rs-to-month-of-year-on-windows-with-german-
>> locale/41617215?noredirect
>>>> =1#comment70436768_41617215
>>>> 
>>>> Example:
>>>> 
>>>> # ISO 8601 convention:
>>>> 
>>>> (yw <- format(posix, "%Y-%V"))
>>> 
>>> The documentation for R datetime format parameters ?strptime says %V
>>> is ignored on input.
>>> 
>>> 
>>>> # [1] "2015-52" "2015-53" "2016-53" "2016-01"
>>>> ywd <- sprintf("%s-1", yw)(as.POSIXct(ywd, format = "%Y-%V-%u"))
>>> 
>>> The documentation for R datetime format parameters ( = ?strptime) says
>>> %V is ignored on input.
>>> 
>>> You should leartn to post plain text to r-help.
>>> 
>>> --
>>> David.
>>> 
>>> 
>>>> # [1]
>>>> "2015-01-12 CET" "2015-01-12 CET" "2016-01-12 CET" "2016-01-12 CET"#
>>>> -> utterly wrong!!!
>>>> 
>>>> # US convention:
>>>> (yw <- format(posix, "%Y-%U"))# [1] "2015-5

Re: [R] Match ISO 8601 week-of-year numbers to month-of-year numbers on Windows with German locale

2017-01-13 Thread David Winsemius

> On Jan 13, 2017, at 3:20 AM, Janko Thyson  wrote:
> 
> Hi David,
> 
> thanks for replying and sorry about the HTML/non-plain-text email (I
> forgot to change that, shouldn't have happened).
> 
> Might just be me, but reading "The documentation for R datetime format
> parameters ?strptime says %V is ignored on input." in the
> documentation doesn't really tell me all that much. As a user, I would
> read that, not completely understand what this means and thus try to
> understand it better by applying it in actual code:
> 
> (yw <- format(posix, "%Y-%V"))
>> # [1] "2015-52" "2015-53" "2016-53" "2016-01"
> 
> Which, after checking back with a calendar, would give me reason to
> believe that it using %V does in fact seem to work: it's an input to
> `format()` and R doesn't seem to ignore it as the correct week numbers
> (following ISO 8601) are returned.
> 
> Not wanting to stress this particular aspect any further, though, I
> would slightly rephrase my original question: is it possible to use
> the ISO 8601 convention for weeknumbers at all (on Windows, using a
> German locale setting) and if so, how would I link ISO 8601
> weeknumbers to the correct month of the year?

You are using an undocumented behavior of R's implementation on Windows. If you 
have a date, it's unambiguous what month and dates should be returned, but I 
don't think it's quite so easy to say in which month an unspecified day of the 
week in a particular numbered week might lie.


-- 
David.

> 
> Thanks for help,
> Janko
> 
> On Thu, Jan 12, 2017 at 8:37 PM, David Winsemius  
> wrote:
>> 
>>> On Jan 12, 2017, at 8:14 AM, Janko Thyson  wrote:
>>> 
>>> Dear list,
>>> 
>>> I'm experiencing problems with converting strings of the format
>>> "-" (e.g. 2016-01, 2016-52) to proper POSIX dates which (I
>>> think) I need in order to retrieve the month-of-the-year number.
>>> 
>>> Simpler put: I'd like to match week-of-the-year numbers to
>>> month-of-the-year numbers. Ideally, the week-of-the-year number would
>>> follow the ISO 8601 convention (i.e. format argument "%V") instead of the
>>> US (format argument "%U") or UK (format argument "%W") convention.
>>> 
>>> After posting this to Stackoverflow, I have strong reasons to believe that
>>> the issue is caused by Windows:
>>> http://stackoverflow.com/questions/41616407/match-iso-8601-week-numbers-to-month-of-year-on-windows-with-german-locale/41617215?noredirect=1#comment70436768_41617215
>>> 
>>> Example:
>>> 
>>> # ISO 8601 convention:
>>> 
>>> (yw <- format(posix, "%Y-%V"))
>> 
>> The documentation for R datetime format parameters ?strptime says %V is 
>> ignored on input.
>> 
>> 
>>> # [1] "2015-52" "2015-53" "2016-53" "2016-01"
>>> ywd <- sprintf("%s-1", yw)(as.POSIXct(ywd, format = "%Y-%V-%u"))
>> 
>> The documentation for R datetime format parameters ( = ?strptime) says %V is 
>> ignored on input.
>> 
>> You should leartn to post plain text to r-help.
>> 
>> --
>> David.
>> 
>> 
>>> # [1]
>>> "2015-01-12 CET" "2015-01-12 CET" "2016-01-12 CET" "2016-01-12 CET"#
>>> -> utterly wrong!!!
>>> 
>>> # US convention:
>>> (yw <- format(posix, "%Y-%U"))# [1] "2015-51" "2015-52" "2016-00" "2016-01"
>>> ywd <- sprintf("%s-1", yw)(as.POSIXct(ywd, format = "%Y-%U-%u"))# [1]
>>> "2015-12-21 CET" "2015-12-28 CET" NA   "2016-01-04 CET"#
>>> -> NA problem for week 00A fellow R user tested this on both macOS and
>>> Ubuntu and he didn't encounter the issue:
>>> 
>>> some_dates <- as.POSIXct(c("2015-12-24", "2015-12-31", "2016-01-01",
>>> "2016-01-08"))
>>> (year_week <- format(some_dates, "%Y %U"))## [1] "2015 51" "2015 52"
>>> "2016 00" "2016 01"
>>> (year_week_day <- sprintf("%s 1", year_week))## [1] "2015 51 1" "2015
>>> 52 1" "2016 00 1" "2016 01 1"
>>> (as.POSIXct(year_week_day, format = "%Y %U %u"))## [1] "2015-12-21
>>> EST" "2015-12-28 EST" "2016-01-04 EST" "2016-01-04 EST"
>>> 
>>> My session info:
>>> 
 sessionInfo()
>>> R version 3.3.2 (2016-10-31)
>>> Platform: x86_64-w64-mingw32/x64 (64-bit)
>>> Running under: Windows >= 8 x64 (build 9200)
>>> 
>>> locale:[1] LC_COLLATE=German_Germany.1252
>>> LC_CTYPE=German_Germany.1252   LC_MONETARY=German_Germany.1252
>>> [4] LC_NUMERIC=C   LC_TIME=English_United
>>> States.1252
>>> 
>>> attached base packages:[1] stats graphics  grDevices utils
>>> datasets  methods   base
>>> 
>>> other attached packages:
>>> [1] fva_0.1.0   digest_0.6.10   readxl_0.1.1dplyr_0.5.0
>>> plyr_1.8.4  magrittr_1.5
>>> [7] memoise_1.0.0   testthat_1.0.2  roxygen2_5.0.1  devtools_1.12.0
>>> 
>>> loaded via a namespace (and not attached):
>>> [1] Rcpp_0.12.8 lubridate_1.6.0 assertthat_0.1  packrat_0.4.8-1
>>> crayon_1.3.2withr_1.0.2
>>> [7] R6_2.2.0DBI_0.5-1   stringi_1.1.2   rstudioapi_0.6
>>> tools_3.3.2 stringr_1.1.0  [13] tibble_1.2
>>> 
>>> Any idea on how to workaround this issue on Windows?
>>> 
>>> Thanks and 

Re: [R] Match ISO 8601 week-of-year numbers to month-of-year numbers on Windows with German locale

2017-01-13 Thread Janko Thyson
Hi David,

thanks for replying and sorry about the HTML/non-plain-text email (I
forgot to change that, shouldn't have happened).

Might just be me, but reading "The documentation for R datetime format
parameters ?strptime says %V is ignored on input." in the
documentation doesn't really tell me all that much. As a user, I would
read that, not completely understand what this means and thus try to
understand it better by applying it in actual code:

(yw <- format(posix, "%Y-%V"))
> # [1] "2015-52" "2015-53" "2016-53" "2016-01"

Which, after checking back with a calendar, would give me reason to
believe that it using %V does in fact seem to work: it's an input to
`format()` and R doesn't seem to ignore it as the correct week numbers
(following ISO 8601) are returned.

Not wanting to stress this particular aspect any further, though, I
would slightly rephrase my original question: is it possible to use
the ISO 8601 convention for weeknumbers at all (on Windows, using a
German locale setting) and if so, how would I link ISO 8601
weeknumbers to the correct month of the year?

Thanks for help,
Janko

On Thu, Jan 12, 2017 at 8:37 PM, David Winsemius  wrote:
>
>> On Jan 12, 2017, at 8:14 AM, Janko Thyson  wrote:
>>
>> Dear list,
>>
>> I'm experiencing problems with converting strings of the format
>> "-" (e.g. 2016-01, 2016-52) to proper POSIX dates which (I
>> think) I need in order to retrieve the month-of-the-year number.
>>
>> Simpler put: I'd like to match week-of-the-year numbers to
>> month-of-the-year numbers. Ideally, the week-of-the-year number would
>> follow the ISO 8601 convention (i.e. format argument "%V") instead of the
>> US (format argument "%U") or UK (format argument "%W") convention.
>>
>> After posting this to Stackoverflow, I have strong reasons to believe that
>> the issue is caused by Windows:
>> http://stackoverflow.com/questions/41616407/match-iso-8601-week-numbers-to-month-of-year-on-windows-with-german-locale/41617215?noredirect=1#comment70436768_41617215
>>
>> Example:
>>
>> # ISO 8601 convention:
>>
>> (yw <- format(posix, "%Y-%V"))
>
> The documentation for R datetime format parameters ?strptime says %V is 
> ignored on input.
>
>
>> # [1] "2015-52" "2015-53" "2016-53" "2016-01"
>> ywd <- sprintf("%s-1", yw)(as.POSIXct(ywd, format = "%Y-%V-%u"))
>
> The documentation for R datetime format parameters ( = ?strptime) says %V is 
> ignored on input.
>
> You should leartn to post plain text to r-help.
>
> --
> David.
>
>
>> # [1]
>> "2015-01-12 CET" "2015-01-12 CET" "2016-01-12 CET" "2016-01-12 CET"#
>> -> utterly wrong!!!
>>
>> # US convention:
>> (yw <- format(posix, "%Y-%U"))# [1] "2015-51" "2015-52" "2016-00" "2016-01"
>> ywd <- sprintf("%s-1", yw)(as.POSIXct(ywd, format = "%Y-%U-%u"))# [1]
>> "2015-12-21 CET" "2015-12-28 CET" NA   "2016-01-04 CET"#
>> -> NA problem for week 00A fellow R user tested this on both macOS and
>> Ubuntu and he didn't encounter the issue:
>>
>> some_dates <- as.POSIXct(c("2015-12-24", "2015-12-31", "2016-01-01",
>> "2016-01-08"))
>> (year_week <- format(some_dates, "%Y %U"))## [1] "2015 51" "2015 52"
>> "2016 00" "2016 01"
>> (year_week_day <- sprintf("%s 1", year_week))## [1] "2015 51 1" "2015
>> 52 1" "2016 00 1" "2016 01 1"
>> (as.POSIXct(year_week_day, format = "%Y %U %u"))## [1] "2015-12-21
>> EST" "2015-12-28 EST" "2016-01-04 EST" "2016-01-04 EST"
>>
>> My session info:
>>
>>> sessionInfo()
>> R version 3.3.2 (2016-10-31)
>> Platform: x86_64-w64-mingw32/x64 (64-bit)
>> Running under: Windows >= 8 x64 (build 9200)
>>
>> locale:[1] LC_COLLATE=German_Germany.1252
>> LC_CTYPE=German_Germany.1252   LC_MONETARY=German_Germany.1252
>> [4] LC_NUMERIC=C   LC_TIME=English_United
>> States.1252
>>
>> attached base packages:[1] stats graphics  grDevices utils
>> datasets  methods   base
>>
>> other attached packages:
>> [1] fva_0.1.0   digest_0.6.10   readxl_0.1.1dplyr_0.5.0
>> plyr_1.8.4  magrittr_1.5
>> [7] memoise_1.0.0   testthat_1.0.2  roxygen2_5.0.1  devtools_1.12.0
>>
>> loaded via a namespace (and not attached):
>> [1] Rcpp_0.12.8 lubridate_1.6.0 assertthat_0.1  packrat_0.4.8-1
>> crayon_1.3.2withr_1.0.2
>> [7] R6_2.2.0DBI_0.5-1   stringi_1.1.2   rstudioapi_0.6
>> tools_3.3.2 stringr_1.1.0  [13] tibble_1.2
>>
>> Any idea on how to workaround this issue on Windows?
>>
>> Thanks and best regards,
>>
>> Janko Thyson
>>
>>   [[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

Re: [R] Match ISO 8601 week-of-year numbers to month-of-year numbers on Windows with German locale

2017-01-12 Thread Nordlund, Dan (DSHS/RDA)
See comments inline.

> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Bob Rudis
> Sent: Thursday, January 12, 2017 12:41 PM
> To: David Winsemius
> Cc: r-help@r-project.org
> Subject: Re: [R] Match ISO 8601 week-of-year numbers to month-of-year
> numbers on Windows with German locale
> 
> Aye, but this:
> 
>   some_dates <- as.POSIXct(c("2015-12-24", "2015-12-31", "2016-01-01",
> "2016-01-08"))
> 
>   (year_week <- format(some_dates, "%Y-%U"))
>   ## [1] "2015-51" "2015-52" "2016-00" "2016-01"
> 
>   (year_week_day <- sprintf("%s-1", year_week))
>   ## [1] "2015-51-1" "2015-52-1" "2016-00-1" "2016-01-1"
> 
>   (as.POSIXct(year_week_day, format = "%Y-%U-%u"))
>   ## [1] "2015-12-21 EST" "2015-12-28 EST" "2016-01-04 EST" "2016-01-04 EST"
> 
> works fine on macOS & Linux (Ubuntu, anyway), but it fails on Windows (10,
> 64bit, R 3.3.2):
> 
>   (as.POSIXct(year_week_day, format = "%Y-%U-%u"))
>   ## [1] "2015-12-21 PST" "2015-12-28 PST" NA   "2016-01-04 PST"

Why do you say it works fine on Ubuntu?  The date "2016-01-01" is in week 0 of 
2016.  There is no Monday in week 0.  So I would argue the return  value of NA 
from Windows is more appropriate than returning the date of Monday in week 1 of 
2016 like Ubuntu does.

Dan

Daniel Nordlund, PhD
Research and Data Analysis Division
Services & Enterprise Support Administration
Washington State Department of Social and Health Services

> 
> On 1/12/17, David Winsemius <dwinsem...@comcast.net> wrote:
> >
> >> On Jan 12, 2017, at 8:14 AM, Janko Thyson <janko.thy...@gmail.com>
> wrote:
> >>
> >> Dear list,
> >>
> >> I'm experiencing problems with converting strings of the format
> >> "-" (e.g. 2016-01, 2016-52) to proper POSIX dates
> >> which (I
> >> think) I need in order to retrieve the month-of-the-year number.
> >>
> >> Simpler put: I'd like to match week-of-the-year numbers to
> >> month-of-the-year numbers. Ideally, the week-of-the-year number
> would
> >> follow the ISO 8601 convention (i.e. format argument "%V") instead of
> >> the US (format argument "%U") or UK (format argument "%W")
> convention.
> >>
> >> After posting this to Stackoverflow, I have strong reasons to believe
> >> that the issue is caused by Windows:
> >> http://stackoverflow.com/questions/41616407/match-iso-8601-week-
> numbe
> >> rs-to-month-of-year-on-windows-with-german-
> locale/41617215?noredirect
> >> =1#comment70436768_41617215
> >>
> >> Example:
> >>
> >> # ISO 8601 convention:
> >>
> >> (yw <- format(posix, "%Y-%V"))
> >
> > The documentation for R datetime format parameters ?strptime says %V
> > is ignored on input.
> >
> >
> >> # [1] "2015-52" "2015-53" "2016-53" "2016-01"
> >> ywd <- sprintf("%s-1", yw)(as.POSIXct(ywd, format = "%Y-%V-%u"))
> >
> > The documentation for R datetime format parameters ( = ?strptime) says
> > %V is ignored on input.
> >
> > You should leartn to post plain text to r-help.
> >
> > --
> > David.
> >
> >
> >> # [1]
> >> "2015-01-12 CET" "2015-01-12 CET" "2016-01-12 CET" "2016-01-12 CET"#
> >> -> utterly wrong!!!
> >>
> >> # US convention:
> >> (yw <- format(posix, "%Y-%U"))# [1] "2015-51" "2015-52" "2016-00"
> >> "2016-01"
> >> ywd <- sprintf("%s-1", yw)(as.POSIXct(ywd, format = "%Y-%U-%u"))# [1]
> >> "2015-12-21 CET" "2015-12-28 CET" NA   "2016-01-04 CET"#
> >> -> NA problem for week 00A fellow R user tested this on both macOS
> >> -> and
> >> Ubuntu and he didn't encounter the issue:
> >>
> >> some_dates <- as.POSIXct(c("2015-12-24", "2015-12-31", "2016-01-01",
> >> "2016-01-08"))
> >> (year_week <- format(some_dates, "%Y %U"))## [1] "2015 51" "2015 52"
> >> "2016 00" "2016 01"
> >> (year_week_day <- sprintf("%s 1", year_week))## [1] "20

Re: [R] Match ISO 8601 week-of-year numbers to month-of-year numbers on Windows with German locale

2017-01-12 Thread Bob Rudis
Aye, but this:

  some_dates <- as.POSIXct(c("2015-12-24", "2015-12-31", "2016-01-01",
"2016-01-08"))

  (year_week <- format(some_dates, "%Y-%U"))
  ## [1] "2015-51" "2015-52" "2016-00" "2016-01"

  (year_week_day <- sprintf("%s-1", year_week))
  ## [1] "2015-51-1" "2015-52-1" "2016-00-1" "2016-01-1"

  (as.POSIXct(year_week_day, format = "%Y-%U-%u"))
  ## [1] "2015-12-21 EST" "2015-12-28 EST" "2016-01-04 EST" "2016-01-04 EST"

works fine on macOS & Linux (Ubuntu, anyway), but it fails on Windows
(10, 64bit, R 3.3.2):

  (as.POSIXct(year_week_day, format = "%Y-%U-%u"))
  ## [1] "2015-12-21 PST" "2015-12-28 PST" NA   "2016-01-04 PST"

On 1/12/17, David Winsemius  wrote:
>
>> On Jan 12, 2017, at 8:14 AM, Janko Thyson  wrote:
>>
>> Dear list,
>>
>> I'm experiencing problems with converting strings of the format
>> "-" (e.g. 2016-01, 2016-52) to proper POSIX dates which
>> (I
>> think) I need in order to retrieve the month-of-the-year number.
>>
>> Simpler put: I'd like to match week-of-the-year numbers to
>> month-of-the-year numbers. Ideally, the week-of-the-year number would
>> follow the ISO 8601 convention (i.e. format argument "%V") instead of the
>> US (format argument "%U") or UK (format argument "%W") convention.
>>
>> After posting this to Stackoverflow, I have strong reasons to believe
>> that
>> the issue is caused by Windows:
>> http://stackoverflow.com/questions/41616407/match-iso-8601-week-numbers-to-month-of-year-on-windows-with-german-locale/41617215?noredirect=1#comment70436768_41617215
>>
>> Example:
>>
>> # ISO 8601 convention:
>>
>> (yw <- format(posix, "%Y-%V"))
>
> The documentation for R datetime format parameters ?strptime says %V is
> ignored on input.
>
>
>> # [1] "2015-52" "2015-53" "2016-53" "2016-01"
>> ywd <- sprintf("%s-1", yw)(as.POSIXct(ywd, format = "%Y-%V-%u"))
>
> The documentation for R datetime format parameters ( = ?strptime) says %V is
> ignored on input.
>
> You should leartn to post plain text to r-help.
>
> --
> David.
>
>
>> # [1]
>> "2015-01-12 CET" "2015-01-12 CET" "2016-01-12 CET" "2016-01-12 CET"#
>> -> utterly wrong!!!
>>
>> # US convention:
>> (yw <- format(posix, "%Y-%U"))# [1] "2015-51" "2015-52" "2016-00"
>> "2016-01"
>> ywd <- sprintf("%s-1", yw)(as.POSIXct(ywd, format = "%Y-%U-%u"))# [1]
>> "2015-12-21 CET" "2015-12-28 CET" NA   "2016-01-04 CET"#
>> -> NA problem for week 00A fellow R user tested this on both macOS and
>> Ubuntu and he didn't encounter the issue:
>>
>> some_dates <- as.POSIXct(c("2015-12-24", "2015-12-31", "2016-01-01",
>> "2016-01-08"))
>> (year_week <- format(some_dates, "%Y %U"))## [1] "2015 51" "2015 52"
>> "2016 00" "2016 01"
>> (year_week_day <- sprintf("%s 1", year_week))## [1] "2015 51 1" "2015
>> 52 1" "2016 00 1" "2016 01 1"
>> (as.POSIXct(year_week_day, format = "%Y %U %u"))## [1] "2015-12-21
>> EST" "2015-12-28 EST" "2016-01-04 EST" "2016-01-04 EST"
>>
>> My session info:
>>
>>> sessionInfo()
>> R version 3.3.2 (2016-10-31)
>> Platform: x86_64-w64-mingw32/x64 (64-bit)
>> Running under: Windows >= 8 x64 (build 9200)
>>
>> locale:[1] LC_COLLATE=German_Germany.1252
>> LC_CTYPE=German_Germany.1252   LC_MONETARY=German_Germany.1252
>> [4] LC_NUMERIC=C   LC_TIME=English_United
>> States.1252
>>
>> attached base packages:[1] stats graphics  grDevices utils
>> datasets  methods   base
>>
>> other attached packages:
>> [1] fva_0.1.0   digest_0.6.10   readxl_0.1.1dplyr_0.5.0
>> plyr_1.8.4  magrittr_1.5
>> [7] memoise_1.0.0   testthat_1.0.2  roxygen2_5.0.1  devtools_1.12.0
>>
>> loaded via a namespace (and not attached):
>> [1] Rcpp_0.12.8 lubridate_1.6.0 assertthat_0.1  packrat_0.4.8-1
>> crayon_1.3.2withr_1.0.2
>> [7] R6_2.2.0DBI_0.5-1   stringi_1.1.2   rstudioapi_0.6
>> tools_3.3.2 stringr_1.1.0  [13] tibble_1.2
>>
>> Any idea on how to workaround this issue on Windows?
>>
>> Thanks and best regards,
>>
>> Janko Thyson
>>
>>  [[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.
>

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

Re: [R] Match ISO 8601 week-of-year numbers to month-of-year numbers on Windows with German locale

2017-01-12 Thread David Winsemius

> On Jan 12, 2017, at 8:14 AM, Janko Thyson  wrote:
> 
> Dear list,
> 
> I'm experiencing problems with converting strings of the format
> "-" (e.g. 2016-01, 2016-52) to proper POSIX dates which (I
> think) I need in order to retrieve the month-of-the-year number.
> 
> Simpler put: I'd like to match week-of-the-year numbers to
> month-of-the-year numbers. Ideally, the week-of-the-year number would
> follow the ISO 8601 convention (i.e. format argument "%V") instead of the
> US (format argument "%U") or UK (format argument "%W") convention.
> 
> After posting this to Stackoverflow, I have strong reasons to believe that
> the issue is caused by Windows:
> http://stackoverflow.com/questions/41616407/match-iso-8601-week-numbers-to-month-of-year-on-windows-with-german-locale/41617215?noredirect=1#comment70436768_41617215
> 
> Example:
> 
> # ISO 8601 convention:
> 
> (yw <- format(posix, "%Y-%V"))

The documentation for R datetime format parameters ?strptime says %V is ignored 
on input.


> # [1] "2015-52" "2015-53" "2016-53" "2016-01"
> ywd <- sprintf("%s-1", yw)(as.POSIXct(ywd, format = "%Y-%V-%u"))

The documentation for R datetime format parameters ( = ?strptime) says %V is 
ignored on input.

You should leartn to post plain text to r-help.

-- 
David.


> # [1]
> "2015-01-12 CET" "2015-01-12 CET" "2016-01-12 CET" "2016-01-12 CET"#
> -> utterly wrong!!!
> 
> # US convention:
> (yw <- format(posix, "%Y-%U"))# [1] "2015-51" "2015-52" "2016-00" "2016-01"
> ywd <- sprintf("%s-1", yw)(as.POSIXct(ywd, format = "%Y-%U-%u"))# [1]
> "2015-12-21 CET" "2015-12-28 CET" NA   "2016-01-04 CET"#
> -> NA problem for week 00A fellow R user tested this on both macOS and
> Ubuntu and he didn't encounter the issue:
> 
> some_dates <- as.POSIXct(c("2015-12-24", "2015-12-31", "2016-01-01",
> "2016-01-08"))
> (year_week <- format(some_dates, "%Y %U"))## [1] "2015 51" "2015 52"
> "2016 00" "2016 01"
> (year_week_day <- sprintf("%s 1", year_week))## [1] "2015 51 1" "2015
> 52 1" "2016 00 1" "2016 01 1"
> (as.POSIXct(year_week_day, format = "%Y %U %u"))## [1] "2015-12-21
> EST" "2015-12-28 EST" "2016-01-04 EST" "2016-01-04 EST"
> 
> My session info:
> 
>> sessionInfo()
> R version 3.3.2 (2016-10-31)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
> Running under: Windows >= 8 x64 (build 9200)
> 
> locale:[1] LC_COLLATE=German_Germany.1252
> LC_CTYPE=German_Germany.1252   LC_MONETARY=German_Germany.1252
> [4] LC_NUMERIC=C   LC_TIME=English_United
> States.1252
> 
> attached base packages:[1] stats graphics  grDevices utils
> datasets  methods   base
> 
> other attached packages:
> [1] fva_0.1.0   digest_0.6.10   readxl_0.1.1dplyr_0.5.0
> plyr_1.8.4  magrittr_1.5
> [7] memoise_1.0.0   testthat_1.0.2  roxygen2_5.0.1  devtools_1.12.0
> 
> loaded via a namespace (and not attached):
> [1] Rcpp_0.12.8 lubridate_1.6.0 assertthat_0.1  packrat_0.4.8-1
> crayon_1.3.2withr_1.0.2
> [7] R6_2.2.0DBI_0.5-1   stringi_1.1.2   rstudioapi_0.6
> tools_3.3.2 stringr_1.1.0  [13] tibble_1.2
> 
> Any idea on how to workaround this issue on Windows?
> 
> Thanks and best regards,
> 
> Janko Thyson
> 
>   [[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] Match Coordinates to NUTS 2 ID

2016-05-27 Thread Miluji Sb
Thank you for your reply. I am trying to use the over function - but having
trouble. Asked at R-sig-geo. Thanks again!

Sincerely,

Milu

On Fri, May 27, 2016 at 12:49 AM, MacQueen, Don  wrote:

> Perhaps the
>   over()
> function in the sp package.
>
> (in which case, R-sig-geo might be a better place to ask).
>
> -Don
>
> --
> Don MacQueen
>
> Lawrence Livermore National Laboratory
> 7000 East Ave., L-627
> Livermore, CA 94550
> 925-423-1062
>
>
>
>
>
> On 5/26/16, 2:30 PM, "R-help on behalf of Miluji Sb"
>  wrote:
>
> >Dear all,
> >
> >
> >I have downloaded the NUTS 2 level data from
> >library(³rgdal²)
> >library(³RColorBrewer²)
> >library(³classInt²)
> >#library(³SmarterPoland²)
> >library(fields)
> >
> ># Download Administrative Level data from EuroStat
> >temp <- tempfile(fileext = ".zip")
> >download.file("
> >
> http://ec.europa.eu/eurostat/cache/GISCO/geodatafiles/NUTS_2010_60M_SH.zip
> >",
> >  temp)
> >unzip(temp)
> >
> ># Read data
> >EU_NUTS <- readOGR(dsn = "./NUTS_2010_60M_SH/data", layer =
> >"NUTS_RG_60M_2010")
> >
> ># Subset NUTS 2 level data
> >map_nuts2 <- subset(EU_NUTS, STAT_LEVL_ == 2)
> >
> >I also have data for a variable by coordinates, which looks like this:
> >
> >structure(list(LON = c(-125.25, -124.75, -124.25, -124.25, -124.25,
> >-124.25), LAT = c(49.75, 49.25, 42.75, 43.25, 48.75, 49.25),
> >yr = c(2.91457704560515, 9.94774197180345, -2.71956412885765,
> >-0.466213169185147, -36.6645659563374, 10.5168056769535)), .Names =
> >c("LON",
> >"LAT", "yr"), row.names = c(NA, 6L), class = "data.frame")
> >
> >I would like to match the coordinates to their corresponding NUTS 2 region
> >- is this possible? Any help will be high appreciated. Thank you!
> >
> >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.
>
>

[[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] Match Coordinates to NUTS 2 ID

2016-05-26 Thread MacQueen, Don
Perhaps the
  over()
function in the sp package.

(in which case, R-sig-geo might be a better place to ask).

-Don

-- 
Don MacQueen

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





On 5/26/16, 2:30 PM, "R-help on behalf of Miluji Sb"
 wrote:

>Dear all,
>
>
>I have downloaded the NUTS 2 level data from
>library(³rgdal²)
>library(³RColorBrewer²)
>library(³classInt²)
>#library(³SmarterPoland²)
>library(fields)
>
># Download Administrative Level data from EuroStat
>temp <- tempfile(fileext = ".zip")
>download.file("
>http://ec.europa.eu/eurostat/cache/GISCO/geodatafiles/NUTS_2010_60M_SH.zip
>",
>  temp)
>unzip(temp)
>
># Read data
>EU_NUTS <- readOGR(dsn = "./NUTS_2010_60M_SH/data", layer =
>"NUTS_RG_60M_2010")
>
># Subset NUTS 2 level data
>map_nuts2 <- subset(EU_NUTS, STAT_LEVL_ == 2)
>
>I also have data for a variable by coordinates, which looks like this:
>
>structure(list(LON = c(-125.25, -124.75, -124.25, -124.25, -124.25,
>-124.25), LAT = c(49.75, 49.25, 42.75, 43.25, 48.75, 49.25),
>yr = c(2.91457704560515, 9.94774197180345, -2.71956412885765,
>-0.466213169185147, -36.6645659563374, 10.5168056769535)), .Names =
>c("LON",
>"LAT", "yr"), row.names = c(NA, 6L), class = "data.frame")
>
>I would like to match the coordinates to their corresponding NUTS 2 region
>- is this possible? Any help will be high appreciated. Thank you!
>
>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.

__
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] Match beginning and end of string (grepl)

2014-09-02 Thread John McKown
On Tue, Sep 2, 2014 at 7:12 AM, Johannes Radinger
johannesradin...@gmail.com wrote:
 Hi,

 I'd like to match the beginning and the end of a string. E.g. I want to
 extract all strings from a vector that beginn with 12 and end with
 Apples:

 a - 2 green Apples
 b - 12 green Apples
 c - 12 Apples and 2 green Bananas
 d - 12 yellow Bananas

 fruitlist - c(a,b,c,d)

 # This is how to extract all that beginn with 12
 grepl(^12,fruitlist)

 But how can I get only those that also end with Apples. So basically
 just item b 12 green Apples should remain.

 Is there any clear description and examples of regular expressions
 and how to use them? I find the manual ?grepl very difficult to read.

 Thanks,
 Johannes


Please try to change your email to not use HTML, per forum requirements.

Now, on to some real help. Regular expressions are the most
complicated thing that I've ever run across except, maybe, for APL.
For me, the definitive book on them is Mastering Regular Expressions
by Jeffrey E. F. Friedl
http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124
$7.49 for the Kindle version. $32.19 for the dead tree (paperback)
version. If you go to that Amazon page, it has some other
possibilities as well.

However a very nice, free, web tutorial is available at:
http://www.regular-expressions.info/

To answer your immediate question, given your very good start,

grepl(^12.*Apples$,fruitlist);

The dollar sign says match the logical end of the string and is the
key you are looking for. In English, the regex says: Match the front
of the string (^). Now, immediately match the characters 12. The .
means match anything. Followed by the * which means match the
previous expression (anything) 0 or more times. Then match the string
Apples. Then match the end of the string ($). You don't want me to
explain how this is done. Talk about confusing to the novice. And
likely even to people who can use regular expressions farily well
(such as myself).

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

Maranatha! 
John McKown

__
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] match from a data.frame in dependence of an ID

2014-03-29 Thread arun
Hi,
Try:
#dat is the dataset:

library(plyr)
dcast(dat,ID~Group,value.var=value,fill=)
#or

dcast(dat,ID~Group,value.var=value,fill=0)
A.K.


On Friday, March 28, 2014 4:54 AM, Mat matthias.we...@fnt.de wrote:
Hello togehter,

i have a litte problem. I have an output data.frame which look like this
one:
    ID          
1  10        
2  11
3  12

Now I have another data.frame with more than one line for each ID:
    ID    Group    Value
1  10    1            10
2  10    2            20
3  10    3            30
4  10    4            40
5  10    5            50
6  11    3            60
7  11    4            70
8  12    4            80

How can i match these two data.frame for the following result:

    ID    1     2     3     4     5          
1  10    10   20    30   40   50
2  11                 60   70
3  12                       80



--
View this message in context: 
http://r.789695.n4.nabble.com/match-from-a-data-frame-in-dependence-of-an-ID-tp4687745.html
Sent from the R help mailing list archive at Nabble.com.

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


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


Re: [R] match from a data.frame in dependence of an ID

2014-03-28 Thread Lisa S
Try xtabs()
 df2 = data.frame(ID = c(10,10,10,10,10,11,11,12),Group =
c(1,2,3,4,5,3,4,4),Value = c(10,20,30,40,50,60,70,80))
 xtabs(df2$Value~df2$ID + df2$Group)

I think this is exactly what you want.


On Fri, Mar 28, 2014 at 4:51 PM, Mat matthias.we...@fnt.de wrote:

 Hello togehter,

 i have a litte problem. I have an output data.frame which look like this
 one:
 ID
 1  10
 2  11
 3  12

 Now I have another data.frame with more than one line for each ID:
 IDGroupValue
 1  10110
 2  10220
 3  10330
 4  10440
 5  10550
 6  11360
 7  11470
 8  12480

 How can i match these two data.frame for the following result:

 ID1 2 3 4 5
 1  1010   2030   40   50
 2  11 60   70
 3  12   80



 --
 View this message in context:
 http://r.789695.n4.nabble.com/match-from-a-data-frame-in-dependence-of-an-ID-tp4687745.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] match from a data.frame in dependence of an ID

2014-03-28 Thread David Carlson
This is a bit more direct. It works by forcing R to treat test
as a matrix rather than a table:

 tst2 - data.frame(ID=dimnames(test)$ID,
as.data.frame.matrix(test),
  check.names=FALSE)
 tst2
   ID  1  2  3  4  5
10 10 10 20 30 40 50
11 11  0  0 60 70  0
12 12  0  0  0 80  0
 rownames(tst2) - NULL

The last command simply changes the rownames from 10:12 to 1:3.
You may run into problems depending on what you are doing with
the data frame since 1, 2, etc are not valid column names. For
example:

 tst2$1
Error: unexpected numeric constant in tst2$1

So you might want to change to something like:

 tst2 - data.frame(ID=dimnames(test)$ID,
G=as.data.frame.matrix(a))
 tst2
   ID G.1 G.2 G.3 G.4 G.5
10 10  10  20  30  40  50
11 11   0   0  60  70   0
12 12   0   0   0  80   0 
 tst2$G.1
[1] 10  0  0


-
David L Carlson
Department of Anthropology
Texas AM University
College Station, TX 77840-4352

-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Rui Barradas
Sent: Friday, March 28, 2014 10:43 AM
To: Mat; r-help@r-project.org
Subject: Re: [R] match from a data.frame in dependence of an ID

Hello,

Maybe there are other ways but the following works.


tst2 - matrix(nrow = dim(test)[1], ncol = dim(test)[2])
tst2[] - test
tst2 - cbind(dimnames(test)[[1]], tst2)
colnames(tst2) - c(ID, dimnames(test)[[2]])
tst2 - as.data.frame(tst2)
tst2


Hope this helps,

Rui Barradas

Em 28-03-2014 15:07, Mat escreveu:
 Thanks first. Your solutions works nearly perfect, i only have
one problem
 left.

 The result looks like perfect, my problem is now, that i want
to convert the
 solution into a data.frame.

 If i try
 test-xtabs(df2$Value~df2$ID + df2$Group)
 test
 df2$Group
 df2$ID  1  2  3  4  5
  10 10 20 30 40 50
  11  0  0 60 70  0
  12  0  0  0 80  0

 perfect.

 Now i try:

 test-as.data.frame(test)
 test
 df2.ID df2.Group Freq
 1  10 1   10
 2  11 10
 3  12 10
 4  10 2   20
 5  11 20
 6  12 20
 7  10 3   30
 8  11 3   60
 9  12 30
 10 10 4   40
 11 11 4   70
 12 12 4   80
 13 10 5   50
 14 11 50
 15 12 50

 Maybe i can delete the entries on the top df2$Group and
df2$ID?
 My solution should like this one, that i can convert into a
data.frame


ID  1  2   3   4  5
 110 10 20 30 40 50
 211  0  0 60 70  0
 312  0  0  0 80  0

 Thank you
 Mat



 --
 View this message in context:
http://r.789695.n4.nabble.com/match-from-a-data-frame-in-depende
nce-of-an-ID-tp4687745p4687767.html
 Sent from the R help mailing list archive at Nabble.com.

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


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

__
R-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] match from a data.frame in dependence of an ID

2014-03-28 Thread Mat
Thanks first. Your solutions works nearly perfect, i only have one problem
left.

The result looks like perfect, my problem is now, that i want to convert the
solution into a data.frame.

If i try 
test-xtabs(df2$Value~df2$ID + df2$Group) 
test  
   df2$Group
df2$ID  1  2  3  4  5
10 10 20 30 40 50
11  0  0 60 70  0
12  0  0  0 80  0

perfect.

Now i try:

test-as.data.frame(test)
test
   df2.ID df2.Group Freq
1  10 1   10
2  11 10
3  12 10
4  10 2   20
5  11 20
6  12 20
7  10 3   30
8  11 3   60
9  12 30
10 10 4   40
11 11 4   70
12 12 4   80
13 10 5   50
14 11 50
15 12 50

Maybe i can delete the entries on the top df2$Group and df2$ID? 
My solution should like this one, that i can convert into a data.frame

  
  ID  1  2   3   4  5  
110 10 20 30 40 50
211  0  0 60 70  0
312  0  0  0 80  0

Thank you 
Mat



--
View this message in context: 
http://r.789695.n4.nabble.com/match-from-a-data-frame-in-dependence-of-an-ID-tp4687745p4687767.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] match from a data.frame in dependence of an ID

2014-03-28 Thread Rui Barradas

Hello,

Maybe there are other ways but the following works.


tst2 - matrix(nrow = dim(test)[1], ncol = dim(test)[2])
tst2[] - test
tst2 - cbind(dimnames(test)[[1]], tst2)
colnames(tst2) - c(ID, dimnames(test)[[2]])
tst2 - as.data.frame(tst2)
tst2


Hope this helps,

Rui Barradas

Em 28-03-2014 15:07, Mat escreveu:

Thanks first. Your solutions works nearly perfect, i only have one problem
left.

The result looks like perfect, my problem is now, that i want to convert the
solution into a data.frame.

If i try
test-xtabs(df2$Value~df2$ID + df2$Group)
test
df2$Group
df2$ID  1  2  3  4  5
 10 10 20 30 40 50
 11  0  0 60 70  0
 12  0  0  0 80  0

perfect.

Now i try:

test-as.data.frame(test)
test
df2.ID df2.Group Freq
1  10 1   10
2  11 10
3  12 10
4  10 2   20
5  11 20
6  12 20
7  10 3   30
8  11 3   60
9  12 30
10 10 4   40
11 11 4   70
12 12 4   80
13 10 5   50
14 11 50
15 12 50

Maybe i can delete the entries on the top df2$Group and df2$ID?
My solution should like this one, that i can convert into a data.frame


   ID  1  2   3   4  5
110 10 20 30 40 50
211  0  0 60 70  0
312  0  0  0 80  0

Thank you
Mat



--
View this message in context: 
http://r.789695.n4.nabble.com/match-from-a-data-frame-in-dependence-of-an-ID-tp4687745p4687767.html
Sent from the R help mailing list archive at Nabble.com.

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



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


Re: [R] match - returns a vector of the positions of (first) matches - looking for All positions.

2014-02-03 Thread Kehl Dániel
Dear Witold,

as a first hint: read posting guide please! People are answering in their free 
time, so please try to be polite!

For your first problem: what are you expecting? You have a dataset where most 
of the values is missing, the others all 0's most of the time, and some Inf 
values as well...

For the second problem: give a reproducible example, please.

daniel

Feladó: r-help-boun...@r-project.org [r-help-boun...@r-project.org] ; 
meghatalmaz#243;: Witold E Wolski [wewol...@gmail.com]
Küldve: 2014. február 3. 11:18
To: r-help@r-project.org
Tárgy: [R] match - returns a vector of the positions of (first) matches - 
looking for All positions.

Looking for a build in function which returns a vector of the
positions of _ALL_ matches.




--
Witold Eryk Wolski

__
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] match - returns a vector of the positions of (first) matches - looking for All positions.

2014-02-03 Thread PIKAL Petr
Hi

which(table == match)

or

which(table %in% match)

Petr


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Witold E Wolski
 Sent: Monday, February 03, 2014 11:19 AM
 To: r-help@r-project.org
 Subject: [R] match - returns a vector of the positions of (first)
 matches - looking for All positions.

 Looking for a build in function which returns a vector of the positions
 of _ALL_ matches.




 --
 Witold Eryk Wolski

 __
 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 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

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

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately 
accept such offer; The sender of this e-mail (offer) excludes any acceptance of 
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an 
express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into 
any contracts on behalf of the company except for cases in which he/she is 
expressly authorized to do so in writing, and such authorization or power of 
attorney is submitted to the recipient or the person represented by the 
recipient, or the existence of such authorization is known to the recipient of 
the person represented by the recipient.
__
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] match - returns a vector of the positions of (first) matches - looking for All positions.

2014-02-03 Thread Witold E Wolski
Hi Petr,

I was actually citing the match (%in%) docu -
match returns the positions of (first) matches.

I am looking for something returning ALL matches (sure it's
easy to write a function which does it ...
my intuition however is, that there is already such a function in R - is there
something which isn't already implemented in R? Not that I know of. )

regarding :

which(table == match)

in case table is a vector the output produced is going to be:

Warning message:
In table == match :
  longer object length is not a multiple of shorter object length



regards


On 3 February 2014 11:36, PIKAL Petr petr.pi...@precheza.cz wrote:
 Hi

 which(table == match)

 or

 which(table %in% match)

 Petr


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Witold E Wolski
 Sent: Monday, February 03, 2014 11:19 AM
 To: r-help@r-project.org
 Subject: [R] match - returns a vector of the positions of (first)
 matches - looking for All positions.

 Looking for a build in function which returns a vector of the positions
 of _ALL_ matches.




 --
 Witold Eryk Wolski

 __
 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 
 žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
 pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu 
 případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je 
 adresátovi či osobě jím zastoupené známá.

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

 In case that this e-mail forms part of business dealings:
 - the sender reserves the right to end negotiations about entering into a 
 contract in any time, for any reason, and without stating any reasoning.
 - if the e-mail contains an offer, the recipient is entitled to immediately 
 accept such offer; The sender of this e-mail (offer) excludes any acceptance 
 of the offer on the part of the recipient containing any amendment or 
 variation.
 - the sender insists on that the respective contract is concluded only upon 
 an express mutual agreement on all its aspects.
 - the sender of this e-mail informs that he/she is not authorized to enter 
 into any contracts on behalf of the company except for cases in which he/she 
 is expressly authorized to do so in writing, and such authorization or power 
 of attorney is submitted to the recipient or the person represented by the 
 recipient, or the existence of such authorization is known to the recipient 
 of the person represented by the recipient.



-- 
Witold Eryk Wolski

__
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] match - returns a vector of the positions of (first) matches - looking for All positions.

2014-02-03 Thread PIKAL Petr
Hi

Posting questions without context may result in answers unrelated to your 
problem.

Anyway it works for me without problem

table-sample(letters[1:5], 20, replace=T)
which(table %in% a)
[1]  1  2 19

 version
   _
platform   i386-w64-mingw32
arch   i386
os mingw32
system i386, mingw32
status Under development (unstable)
major  3
minor  1.0
year   2013
month  12
day19
svn rev64488
language   R
version.string R Under development (unstable) (2013-12-19 r64488)
nickname   Unsuffered Consequences

Regards
Petr


 -Original Message-
 From: Witold E Wolski [mailto:wewol...@gmail.com]
 Sent: Monday, February 03, 2014 1:07 PM
 To: PIKAL Petr
 Cc: r-help@r-project.org
 Subject: Re: [R] match - returns a vector of the positions of (first)
 matches - looking for All positions.

 Hi Petr,

 I was actually citing the match (%in%) docu - match returns the
 positions of (first) matches.

 I am looking for something returning ALL matches (sure it's easy to
 write a function which does it ...
 my intuition however is, that there is already such a function in R -
 is there something which isn't already implemented in R? Not that I
 know of. )

 regarding :

 which(table == match)

 in case table is a vector the output produced is going to be:

 Warning message:
 In table == match :
   longer object length is not a multiple of shorter object length



 regards


 On 3 February 2014 11:36, PIKAL Petr petr.pi...@precheza.cz wrote:
  Hi
 
  which(table == match)
 
  or
 
  which(table %in% match)
 
  Petr
 
 
  -Original Message-
  From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
  project.org] On Behalf Of Witold E Wolski
  Sent: Monday, February 03, 2014 11:19 AM
  To: r-help@r-project.org
  Subject: [R] match - returns a vector of the positions of (first)
  matches - looking for All positions.
 
  Looking for a build in function which returns a vector of the
  positions of _ALL_ matches.
 
 
 
 
  --
  Witold Eryk Wolski
 
  __
  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 žádné smlouvy s výjimkou případů, kdy k tomu byl písemně
 zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly
 adresátovi tohoto emailu případně osobě, kterou adresát zastupuje,
 předloženy nebo jejich existence je adresátovi či osobě jím zastoupené
 známá.
 
  This e-mail and any documents attached to it may be confidential and
 are intended only for its intended recipients.
  If you received this e-mail by mistake, please immediately inform its
 sender. Delete the contents of this e-mail with all attachments and its
 copies from your system.
  If you are not the intended recipient of this e-mail, you are not
 authorized to use, disseminate, copy or disclose this e-mail in any
 manner.
  The sender of this e-mail shall not be liable for any possible damage
 caused by modifications of the e-mail or by delay with transfer of the
 email.
 
  In case that this e-mail forms part of business dealings:
  - the sender reserves the right to end negotiations about entering
 into a contract in any time, for any reason, and without stating any
 reasoning.
  - if the e-mail contains an offer, the recipient is entitled to
 immediately accept such offer; The sender of this e-mail (offer)
 excludes any acceptance of the offer on the part of the recipient
 containing any amendment or variation.
  - the sender insists on that the respective contract is concluded
 only upon an express mutual agreement on all its aspects.
  - the sender of this e-mail informs that he/she

Re: [R] match - returns a vector of the positions of (first) matches - looking for All positions.

2014-02-03 Thread Witold E Wolski
Thank you. That's what I was looking for.

I am  wondering what does the documentation means by (first) than?

match {base}
match returns a vector of the positions of (first) matches of its
first argument in its second.





On 3 February 2014 15:02, PIKAL Petr petr.pi...@precheza.cz wrote:
 Hi

 Posting questions without context may result in answers unrelated to your 
 problem.

 Anyway it works for me without problem

 table-sample(letters[1:5], 20, replace=T)
 which(table %in% a)
 [1]  1  2 19

 version
_
 platform   i386-w64-mingw32
 arch   i386
 os mingw32
 system i386, mingw32
 status Under development (unstable)
 major  3
 minor  1.0
 year   2013
 month  12
 day19
 svn rev64488
 language   R
 version.string R Under development (unstable) (2013-12-19 r64488)
 nickname   Unsuffered Consequences

 Regards
 Petr


 -Original Message-
 From: Witold E Wolski [mailto:wewol...@gmail.com]
 Sent: Monday, February 03, 2014 1:07 PM
 To: PIKAL Petr
 Cc: r-help@r-project.org
 Subject: Re: [R] match - returns a vector of the positions of (first)
 matches - looking for All positions.

 Hi Petr,

 I was actually citing the match (%in%) docu - match returns the
 positions of (first) matches.

 I am looking for something returning ALL matches (sure it's easy to
 write a function which does it ...
 my intuition however is, that there is already such a function in R -
 is there something which isn't already implemented in R? Not that I
 know of. )

 regarding :

 which(table == match)

 in case table is a vector the output produced is going to be:

 Warning message:
 In table == match :
   longer object length is not a multiple of shorter object length



 regards


 On 3 February 2014 11:36, PIKAL Petr petr.pi...@precheza.cz wrote:
  Hi
 
  which(table == match)
 
  or
 
  which(table %in% match)
 
  Petr
 
 
  -Original Message-
  From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
  project.org] On Behalf Of Witold E Wolski
  Sent: Monday, February 03, 2014 11:19 AM
  To: r-help@r-project.org
  Subject: [R] match - returns a vector of the positions of (first)
  matches - looking for All positions.
 
  Looking for a build in function which returns a vector of the
  positions of _ALL_ matches.
 
 
 
 
  --
  Witold Eryk Wolski
 
  __
  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 žádné smlouvy s výjimkou případů, kdy k tomu byl písemně
 zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly
 adresátovi tohoto emailu případně osobě, kterou adresát zastupuje,
 předloženy nebo jejich existence je adresátovi či osobě jím zastoupené
 známá.
 
  This e-mail and any documents attached to it may be confidential and
 are intended only for its intended recipients.
  If you received this e-mail by mistake, please immediately inform its
 sender. Delete the contents of this e-mail with all attachments and its
 copies from your system.
  If you are not the intended recipient of this e-mail, you are not
 authorized to use, disseminate, copy or disclose this e-mail in any
 manner.
  The sender of this e-mail shall not be liable for any possible damage
 caused by modifications of the e-mail or by delay with transfer of the
 email.
 
  In case that this e-mail forms part of business dealings:
  - the sender reserves the right to end negotiations about entering
 into a contract in any time, for any reason, and without stating any
 reasoning.
  - if the e-mail contains an offer, the recipient is entitled to
 immediately accept such offer; The sender

Re: [R] match - returns a vector of the positions of (first) matches - looking for All positions.

2014-02-03 Thread PIKAL Petr
Hi

If you are in doubt, try it. It is usually easy and does not do any harm.

test-sample(letters[1:5], 20, replace=T)

match(test,c)
 [1] NA NA NA  1  1 NA  1  1 NA NA NA  1  1 NA NA NA NA NA NA NA
match(c, test)
[1] 4

First occurence of c is at 4th position of test vector. There are others but 
only the first is returned.

Regards
Petr

 -Original Message-
 From: Witold E Wolski [mailto:wewol...@gmail.com]
 Sent: Monday, February 03, 2014 3:29 PM
 To: PIKAL Petr
 Cc: r-help@r-project.org
 Subject: Re: [R] match - returns a vector of the positions of (first)
 matches - looking for All positions.

 Thank you. That's what I was looking for.

 I am  wondering what does the documentation means by (first) than?

 match {base}
 match returns a vector of the positions of (first) matches of its first
 argument in its second.





 On 3 February 2014 15:02, PIKAL Petr petr.pi...@precheza.cz wrote:
  Hi
 
  Posting questions without context may result in answers unrelated to
 your problem.
 
  Anyway it works for me without problem
 
  table-sample(letters[1:5], 20, replace=T) which(table %in% a) [1]
  1  2 19
 
  version
 _
  platform   i386-w64-mingw32
  arch   i386
  os mingw32
  system i386, mingw32
  status Under development (unstable)
  major  3
  minor  1.0
  year   2013
  month  12
  day19
  svn rev64488
  language   R
  version.string R Under development (unstable) (2013-12-19 r64488)
  nickname   Unsuffered Consequences
 
  Regards
  Petr
 
 
  -Original Message-
  From: Witold E Wolski [mailto:wewol...@gmail.com]
  Sent: Monday, February 03, 2014 1:07 PM
  To: PIKAL Petr
  Cc: r-help@r-project.org
  Subject: Re: [R] match - returns a vector of the positions of
 (first)
  matches - looking for All positions.
 
  Hi Petr,
 
  I was actually citing the match (%in%) docu - match returns the
  positions of (first) matches.
 
  I am looking for something returning ALL matches (sure it's easy to
  write a function which does it ...
  my intuition however is, that there is already such a function in R
 -
  is there something which isn't already implemented in R? Not that I
  know of. )
 
  regarding :
 
  which(table == match)
 
  in case table is a vector the output produced is going to be:
 
  Warning message:
  In table == match :
longer object length is not a multiple of shorter object length
 
 
 
  regards
 
 
  On 3 February 2014 11:36, PIKAL Petr petr.pi...@precheza.cz wrote:
   Hi
  
   which(table == match)
  
   or
  
   which(table %in% match)
  
   Petr
  
  
   -Original Message-
   From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
   project.org] On Behalf Of Witold E Wolski
   Sent: Monday, February 03, 2014 11:19 AM
   To: r-help@r-project.org
   Subject: [R] match - returns a vector of the positions of (first)
   matches - looking for All positions.
  
   Looking for a build in function which returns a vector of the
   positions of _ALL_ matches.
  
  
  
  
   --
   Witold Eryk Wolski
  
   __
   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 žádné smlouvy s výjimkou případů, kdy k tomu byl písemně
  zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly
  adresátovi tohoto emailu případně osobě, kterou adresát zastupuje,
  předloženy nebo jejich existence je adresátovi či osobě jím
  zastoupené známá.
  
   This e-mail and any documents attached to it may be confidential
   and
  are intended only for its intended recipients.
   If you received this e-mail

Re: [R] match - returns a vector of the positions of (first) matches - looking for All positions.

2014-02-03 Thread William Dunlap
 table-sample(letters[1:5], 20, replace=T)
 which(table %in% a)
 [1]  1  2 19

How is this different than
   which( table == a)
?  The latter is probably a tad faster.

I thought the OP had 2 vectors, x and table, and wanted a list the length
of x, such that the i'th element of the list gave the positions of the elements
of the table that matched x[i].  I don't know why this would be useful, but a
naive implementation would be
f - function(x, table)lapply(structure(x, names=x), 
function(xi)which(table == xi))
 used as
t1 - c(a, b, a, c, b)
t2 - c(b, b, c, d, c)
f(t1, t2)
   $a
   integer(0)

   $b
   [1] 1 2

   $a
   integer(0)
   
   $c
   [1] 3 5
   
   $b
   [1] 1 2

Bill Dunlap
TIBCO Software
wdunlap tibco.com


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
 Behalf
 Of PIKAL Petr
 Sent: Monday, February 03, 2014 6:03 AM
 To: Witold E Wolski
 Cc: r-help@r-project.org
 Subject: Re: [R] match - returns a vector of the positions of (first) matches 
 - looking for
 All positions.
 
 Hi
 
 Posting questions without context may result in answers unrelated to your 
 problem.
 
 Anyway it works for me without problem
 
 table-sample(letters[1:5], 20, replace=T)
 which(table %in% a)
 [1]  1  2 19
 
  version
_
 platform   i386-w64-mingw32
 arch   i386
 os mingw32
 system i386, mingw32
 status Under development (unstable)
 major  3
 minor  1.0
 year   2013
 month  12
 day19
 svn rev64488
 language   R
 version.string R Under development (unstable) (2013-12-19 r64488)
 nickname   Unsuffered Consequences
 
 Regards
 Petr
 
 
  -Original Message-
  From: Witold E Wolski [mailto:wewol...@gmail.com]
  Sent: Monday, February 03, 2014 1:07 PM
  To: PIKAL Petr
  Cc: r-help@r-project.org
  Subject: Re: [R] match - returns a vector of the positions of (first)
  matches - looking for All positions.
 
  Hi Petr,
 
  I was actually citing the match (%in%) docu - match returns the
  positions of (first) matches.
 
  I am looking for something returning ALL matches (sure it's easy to
  write a function which does it ...
  my intuition however is, that there is already such a function in R -
  is there something which isn't already implemented in R? Not that I
  know of. )
 
  regarding :
 
  which(table == match)
 
  in case table is a vector the output produced is going to be:
 
  Warning message:
  In table == match :
longer object length is not a multiple of shorter object length
 
 
 
  regards
 
 
  On 3 February 2014 11:36, PIKAL Petr petr.pi...@precheza.cz wrote:
   Hi
  
   which(table == match)
  
   or
  
   which(table %in% match)
  
   Petr
  
  
   -Original Message-
   From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
   project.org] On Behalf Of Witold E Wolski
   Sent: Monday, February 03, 2014 11:19 AM
   To: r-help@r-project.org
   Subject: [R] match - returns a vector of the positions of (first)
   matches - looking for All positions.
  
   Looking for a build in function which returns a vector of the
   positions of _ALL_ matches.
  
  
  
  
   --
   Witold Eryk Wolski
  
   __
   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 žádné smlouvy s výjimkou případů, kdy k tomu byl písemně
  zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly
  adresátovi tohoto emailu případně osobě, kterou adresát zastupuje,
  předloženy nebo jejich existence je adresátovi či osobě jím zastoupené
  známá.
  
   This e-mail and any

Re: [R] match values in dependence of ID and Date

2013-10-17 Thread arun
Hi,

I think based on your title, the output you provided is not clear. If it 
depends on Date, there should be four columns.
library(reshape2)

res1 - dcast(merge(dat,dat2,by=ID),ID+Name~Date,value.var=Value)
 colnames(res1)[3:6] - c(First, Second, Third, Fourth)
 rownames(res1) - 1:nrow(res1)


#or
res2 - 
reshape(merge(dat,dat2,by=ID),idvar=c(ID,Name),timevar=Date,direction=wide)
 dimnames(res2) - dimnames(res1)

 res2
#  ID Name First Second Third Fourth
#1  1 Andy    10 15    NA NA
#2  2 John 7 NA    10 15
#3  3  Amy    10 NA    NA NA


A.K.






On Thursday, October 17, 2013 9:31 AM, arun smartpink...@yahoo.com wrote:
Hi,
Try:
dat - read.table(text=
ID    Name
1    Andy
2    John
3    Amy,sep=,header=TRUE,stringsAsFactors=FALSE)

dat2 - read.table(text=
ID  Date    Value
1    2013-10-01    10
1    2013-10-02    15
2    2013-10-01    7
2    2013-10-03    10
2    2013-10-04    15
3    2013-10-01    
10,sep=,header=TRUE,colClasses=c(numeric,Date,numeric))

library(plyr)

 res - 
reshape(ddply(merge(dat,dat2,by=ID),.(ID),mutate,id=((seq_along(ID)-1)%%3+1))[,-3],idvar=c(ID,Name),timevar=id,direction=wide)
 rownames(res) - 1:nrow(res)
 colnames(res)[3:5] - c(First, Second, Third)

 res
#  ID Name First Second Third
#1  1 Andy    10 15    NA
#2  2 John 7 10    15
#3  3  Amy    10 NA    NA
A.K.







On Thursday, October 17, 2013 7:42 AM, Mat matthias.we...@fnt.de wrote:
hello togehter,

i have a little problem, maybe you can help me.

I have a data.frame like this one:

ID    Name
1     Andy
2     John
3     Amy

and a data.frame like this:

ID   Date            Value
1    2013-10-01    10
1    2013-10-02    15
2    2013-10-01    7
2    2013-10-03    10
2    2013-10-04    15
3    2013-10-01    10

the result should be this one:

ID    Name   First   Second    Third
1     Andy    10     15
2     John     7      10           15
3     Amy     10

maybe you can help me, to do this?

Thank you.

Mat



--
View this message in context: 
http://r.789695.n4.nabble.com/match-values-in-dependence-of-ID-and-Date-tp4678433.html
Sent from the R help mailing list archive at Nabble.com.

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


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


Re: [R] match values in dependence of ID and Date

2013-10-17 Thread arun
Hi,
Try:
dat - read.table(text=
ID    Name
1    Andy
2    John
3    Amy,sep=,header=TRUE,stringsAsFactors=FALSE)

dat2 - read.table(text=
ID  Date    Value
1    2013-10-01    10
1    2013-10-02    15
2    2013-10-01    7
2    2013-10-03    10
2    2013-10-04    15
3    2013-10-01    
10,sep=,header=TRUE,colClasses=c(numeric,Date,numeric))

library(plyr)

 res - 
reshape(ddply(merge(dat,dat2,by=ID),.(ID),mutate,id=((seq_along(ID)-1)%%3+1))[,-3],idvar=c(ID,Name),timevar=id,direction=wide)
 rownames(res) - 1:nrow(res)
 colnames(res)[3:5] - c(First, Second, Third)

 res
#  ID Name First Second Third
#1  1 Andy    10 15    NA
#2  2 John 7 10    15
#3  3  Amy    10 NA    NA
A.K.






On Thursday, October 17, 2013 7:42 AM, Mat matthias.we...@fnt.de wrote:
hello togehter,

i have a little problem, maybe you can help me.

I have a data.frame like this one:

ID    Name
1     Andy
2     John
3     Amy

and a data.frame like this:

ID   Date            Value
1    2013-10-01    10
1    2013-10-02    15
2    2013-10-01    7
2    2013-10-03    10
2    2013-10-04    15
3    2013-10-01    10

the result should be this one:

ID    Name   First   Second    Third
1     Andy    10     15
2     John     7      10           15
3     Amy     10

maybe you can help me, to do this?

Thank you.

Mat



--
View this message in context: 
http://r.789695.n4.nabble.com/match-values-in-dependence-of-ID-and-Date-tp4678433.html
Sent from the R help mailing list archive at Nabble.com.

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


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


Re: [R] match rows of R

2013-06-26 Thread Berend Hasselman

On 26-06-2013, at 10:03, Sachinthaka Abeywardana sachin.abeyward...@gmail.com 
wrote:

 Hi all,
 
 What would be an efficient way to match rows of a matrix to a vector?
 
 ex:
 
 m-matrix(1:9, nrow=3)
 
 m [,1] [,2] [,3]
 [1,]147
 [2,]258
 [3,]369
 
 #
 which(m==c(2,5,8))# I want this to return 2
 ##

Something like this:

matroweqv - function(m,v) which(t(m)==v, arr.ind=TRUE)[,2][1]

matroweqv(m,c(2,5,8))
# [1] 2


Berend

__
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] match rows of R

2013-06-26 Thread Yuliya Matveyeva
I suggest using vectorization :

find_row - function(m,v) { which(!(abs(rowSums(m - rep(v, each = nrow(m)))
))  0) }

The function matroweqv mentioned above would give any row with the first
element equal to the first element in vector v.
The function find_row matches each row of the matrix as a whole to the
vector v.


2013/6/26 Sachinthaka Abeywardana sachin.abeyward...@gmail.com

 Hi all,

 What would be an efficient way to match rows of a matrix to a vector?

 ex:

 m-matrix(1:9, nrow=3)

 m [,1] [,2] [,3]
 [1,]147
 [2,]258
 [3,]369

 #
 which(m==c(2,5,8))# I want this to return 2
 ##

 Thanks,
 Sachin

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




-- 
Sincerely yours,
Yulia Matveyeva,
Department of Statistical Modelling,
Faculty of Mathematics and Mechanics,
St Petersburg State University, Russia

[[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] match rows of R

2013-06-26 Thread Berend Hasselman

On 26-06-2013, at 10:30, Yuliya Matveyeva yuliya.rm...@gmail.com wrote:

 I suggest using vectorization :
 
 find_row - function(m,v) { which(!(abs(rowSums(m - rep(v, each = nrow(m)))
 ))  0) }
 
 The function matroweqv mentioned above would give any row with the first
 element equal to the first element in vector v.

Correct.
This version should be better

matroweqv - function(m,v) {
z - which(t(m)==v, arr.ind=TRUE,useNames=FALSE)
if(dim(z)[1]==0) return(NA) else if(all(z[,2]==z[1,2])) return(z[1,2])  
else return(NA)
} 

Instead of NA one could also return -1 if no row equals the vector.

Berend


 The function find_row matches each row of the matrix as a whole to the
 vector v.
 
 
 2013/6/26 Sachinthaka Abeywardana sachin.abeyward...@gmail.com
 
 Hi all,
 
 What would be an efficient way to match rows of a matrix to a vector?
 
 ex:
 
 m-matrix(1:9, nrow=3)
 
 m [,1] [,2] [,3]
 [1,]147
 [2,]258
 [3,]369
 
 #
 which(m==c(2,5,8))# I want this to return 2
 ##
 
 Thanks,
 Sachin
 
[[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.
 
 
 
 
 -- 
 Sincerely yours,
 Yulia Matveyeva,
 Department of Statistical Modelling,
 Faculty of Mathematics and Mechanics,
 St Petersburg State University, Russia
 
   [[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] match rows of R

2013-06-26 Thread arun
Hi,
Try:
 roweqv- function(m,v) 
which(!is.na(match(interaction(as.data.frame(m),drop=TRUE),paste(v,collapse=.
v- c(2,5,8)
roweqv(m,v)
#[1] 2


set.seed(24)
m1-matrix(sample(1:15,3e5,replace=TRUE),ncol=3)
v1- c(10,12,4)

 system.time(res- roweqv(m1,v1))
  # user  system elapsed 
  #0.132   0.000   0.130 
res
# [1] 5   381  2760  3793  9667 16881 18866 21219 24961 36220 38366 54382
#[13] 54951 55825 57167 67636 70713 71087 73284 82797 83255 85748 86216 86690
#[25] 93120 95399 96370

head(m1[res,])
# [,1] [,2] [,3]
#[1,]   10   12    4
#[2,]   10   12    4
#[3,]   10   12    4
#[4,]   10   12    4
#[5,]   10   12    4
#[6,]   10   12    4

v2- c(20,5,4)
roweqv(m1,v2)
#integer(0)

A.K.
- Original Message -
From: Sachinthaka Abeywardana sachin.abeyward...@gmail.com
To: r-help@r-project.org r-help@r-project.org
Cc: 
Sent: Wednesday, June 26, 2013 4:03 AM
Subject: [R] match rows of R

Hi all,

What would be an efficient way to match rows of a matrix to a vector?

ex:

m-matrix(1:9, nrow=3)

m     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

#
which(m==c(2,5,8))        # I want this to return 2
##

Thanks,
Sachin

    [[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] match rows of R

2013-06-26 Thread arun
This might also work:

roweqv2- function(m,v){indx-1+Reduce(+,lapply(seq_len(ncol(m)),function(i) 
(2^i)*(m[,i]==v[i])))
             which(indx==max(indx))}    

roweqv2(m,v)
#[1] 2
 system.time(res2-roweqv2(m1,v1))
#   user  system elapsed 
  #0.008   0.000   0.008 
 identical(res,res2)
#[1] TRUE

#On a bigger dataset


set.seed(248)
 m1-matrix(sample(1:15,3e7,replace=TRUE),ncol=3)
 v1- c(10,12,4)
 system.time(res- roweqv(m1,v1))
#   user  system elapsed 
# 12.404   1.248  13.677 
  system.time(res2-roweqv2(m1,v1))
#   user  system elapsed 
 # 0.760   0.312   1.076 

identical(res,res2)
#[1] TRUE


A.K.



- Original Message -
From: arun smartpink...@yahoo.com
To: Sachinthaka Abeywardana sachin.abeyward...@gmail.com
Cc: R help r-help@r-project.org
Sent: Wednesday, June 26, 2013 3:26 PM
Subject: Re: [R] match rows of R

Hi,
Try:
 roweqv- function(m,v) 
which(!is.na(match(interaction(as.data.frame(m),drop=TRUE),paste(v,collapse=.
v- c(2,5,8)
roweqv(m,v)
#[1] 2


set.seed(24)
m1-matrix(sample(1:15,3e5,replace=TRUE),ncol=3)
v1- c(10,12,4)

 system.time(res- roweqv(m1,v1))
  # user  system elapsed 
  #0.132   0.000   0.130 
res
# [1] 5   381  2760  3793  9667 16881 18866 21219 24961 36220 38366 54382
#[13] 54951 55825 57167 67636 70713 71087 73284 82797 83255 85748 86216 86690
#[25] 93120 95399 96370

head(m1[res,])
# [,1] [,2] [,3]
#[1,]   10   12    4
#[2,]   10   12    4
#[3,]   10   12    4
#[4,]   10   12    4
#[5,]   10   12    4
#[6,]   10   12    4

v2- c(20,5,4)
roweqv(m1,v2)
#integer(0)

A.K.
- Original Message -
From: Sachinthaka Abeywardana sachin.abeyward...@gmail.com
To: r-help@r-project.org r-help@r-project.org
Cc: 
Sent: Wednesday, June 26, 2013 4:03 AM
Subject: [R] match rows of R

Hi all,

What would be an efficient way to match rows of a matrix to a vector?

ex:

m-matrix(1:9, nrow=3)

m     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

#
which(m==c(2,5,8))        # I want this to return 2
##

Thanks,
Sachin

    [[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] match a task No. to a few person-IDs

2013-02-15 Thread PIKAL Petr
Hi

?merge

Regards
Petr

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Mat
 Sent: Friday, February 15, 2013 10:45 AM
 To: r-help@r-project.org
 Subject: [R] match a task No. to a few person-IDs
 
 hello together,
 
 i have a task No. in a data.frame
Task_No   Team
 A49397 1
 B49396 1
 
 and now i want to match my Person-IDs to each Task_No. My Person Ids
 look like this one:
 IDTeam
 A 50191
 B 472 1
 C 140 1
 D 50131
 
 The solution should be this:
 
Task_No   Team ID
 A49397 1 5019
 A49397 1 472
 A49397 1 140
 A49397 1 5013
 B49396 1 5019
 B49396 1472
 B49396 1140
 B49396 15013
 
 perhabs anyone can help me.
 
 Thank you
 
 Mat
 
 
 
 --
 View this message in context: http://r.789695.n4.nabble.com/match-a-
 task-No-to-a-few-person-IDs-tp4658646.html
 Sent from the R help mailing list archive at Nabble.com.
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-
 guide.html
 and provide commented, minimal, self-contained, reproducible code.

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


Re: [R] match a task No. to a few person-IDs

2013-02-15 Thread arun
Hi,
Try this:
dat1- read.table(text=
  Task_No  Team   
A    49397    1 
B    49396    1
,sep=,header=TRUE)

dat2- read.table(text=
    ID    Team
A 5019    1
B 472    1
C 140    1
D 5013    1
,sep=,header=TRUE)
 res1- as.matrix(merge(dat1,dat2,by=Team))
 row.names(res1)-LETTERS[match(res1[,2],dat1[,1])]  


library(plyr)
res2- as.matrix(join(dat1,dat2,by=Team))
 row.names(res2)-LETTERS[match(res2[,1],dat1[,1])]
 res2
#  Task_No Team   ID
#A   49397    1 5019
#A   49397    1  472
#A   49397    1  140
#A   49397    1 5013
#B   49396    1 5019
#B   49396    1  472
#B   49396    1  140
#B   49396    1 5013
A.K.




- Original Message -
From: Mat matthias.we...@fnt.de
To: r-help@r-project.org
Cc: 
Sent: Friday, February 15, 2013 4:45 AM
Subject: [R] match a task No. to a few person-IDs

hello together,

i have a task No. in a data.frame
       Task_No       Team    
A    49397         1          
B    49396         1      

and now i want to match my Person-IDs to each Task_No. My Person Ids look
like this one:
    ID        Team
A 5019    1
B 472    1
C 140    1
D 5013    1

The solution should be this:

       Task_No       Team     ID
A    49397         1               5019
A    49397         1               472
A    49397         1               140
A    49397         1               5013
B    49396         1         5019
B    49396         1            472
B    49396         1            140
B    49396         1            5013

perhabs anyone can help me.

Thank you

Mat



--
View this message in context: 
http://r.789695.n4.nabble.com/match-a-task-No-to-a-few-person-IDs-tp4658646.html
Sent from the R help mailing list archive at Nabble.com.

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


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


Re: [R] match a task No. to a few person-IDs

2013-02-15 Thread Rui Barradas

Hello,

Use ?merge.


tasks - read.table(text = 
Task_No   Team
A49397   1  
B49396   1  
, header = TRUE)

ids - read.table(text = 
IDTeam
A 5019  1
B 472  1
C 140  1
D 5013  1
, header = TRUE)


merge(tasks, ids, all.x = TRUE)


Hope this helps,

Rui Barradas

Em 15-02-2013 09:45, Mat escreveu:

hello together,

i have a task No. in a data.frame
Task_No   Team
A49397   1  
B49396   1  

and now i want to match my Person-IDs to each Task_No. My Person Ids look
like this one:
 IDTeam
A 5019  1
B 472   1
C 140   1
D 5013  1

The solution should be this:

Task_No   Team ID
A49397   1 5019
A49397   1 472
A49397   1 140
A49397   1 5013
B49396   1 5019
B49396   1472
B49396   1140
B49396   15013

perhabs anyone can help me.

Thank you

Mat



--
View this message in context: 
http://r.789695.n4.nabble.com/match-a-task-No-to-a-few-person-IDs-tp4658646.html
Sent from the R help mailing list archive at Nabble.com.

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



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


Re: [R] match in dependence of 2 columns

2013-02-14 Thread Rui Barradas

Hello,

You can do something like this:


join(dat1, dat2[, c(cu.nr, name, value)], 
by=c(cu.nr.,name),type=right)



Hope this helps,

Rui Barradas
Em 14-02-2013 07:03, Mat escreveu:

thank you, this code works:

library(plyr)
join(dat1,dat2,by=c(cu.nr.,name),type=right)

but my dat2 frame has a lot of columns, which i don't want to match.
How can i say, that only the column value should be match to dat1 ?

Thank you.

Mat




--
View this message in context: 
http://r.789695.n4.nabble.com/match-in-dependence-of-2-columns-tp4658405p4658508.html
Sent from the R help mailing list archive at Nabble.com.

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



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


Re: [R] match in dependence of 2 columns

2013-02-14 Thread arun
HI,

If the dataset is similar to this:
dat1- read.table(text= 
  cu.nr. name  value  
A 1 Evo 100 
B 1 Mer 80  
C 2 Ford    50  
,sep=,header=TRUE,stringsAsFactors=F) 
dat2- read.table(text= 
  cu.nr. name  value2 value3    
A 33 Evo   60   120 
B 1 Mer    40    90 
C 1 Pold   20    30   
,sep=,header=TRUE,stringsAsFactors=F)  
library(plyr)
join(dat1,subset(dat2,select=c(cu.nr.,name)),by=c(cu.nr.,name),type=right)
#  cu.nr. name value
#1 33  Evo    NA
#2  1  Mer    80
#3  1 Pold    NA
#or
join(dat1,dat2[,c(cu.nr.,name)],by=c(cu.nr.,name),type=right)


A.K.



- Original Message -
From: Mat matthias.we...@fnt.de
To: r-help@r-project.org
Cc: 
Sent: Thursday, February 14, 2013 2:03 AM
Subject: Re: [R] match in dependence of 2 columns

thank you, this code works:

library(plyr)
join(dat1,dat2,by=c(cu.nr.,name),type=right) 

but my dat2 frame has a lot of columns, which i don't want to match.
How can i say, that only the column value should be match to dat1 ?

Thank you.

Mat




--
View this message in context: 
http://r.789695.n4.nabble.com/match-in-dependence-of-2-columns-tp4658405p4658508.html
Sent from the R help mailing list archive at Nabble.com.

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


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


Re: [R] match in dependence of 2 columns

2013-02-13 Thread John Kane
?merge

If I have not reversed the data sets this should work
With data sets called dat1 and dat2 

merge(dat1,dat2, all.y = TRUE)

John Kane
Kingston ON Canada


 -Original Message-
 From: matthias.we...@fnt.de
 Sent: Wed, 13 Feb 2013 06:39:57 -0800 (PST)
 To: r-help@r-project.org
 Subject: [R] match in dependence of 2 columns
 
 Hello,
 
 i want to match a column of one data.frame to another, my problem is,
 that i
 only want to match the data, if 2 columns are equal.
 
 i have a example:
 
 data.frame1
   cu.nr. name  value
 A 1 Evo 100
 B 1 Mer 80
 C 2 Ford50
 
 And now i want to match the value to data.frame 2 in dependence of cu.nr.
 and name.
 
 data.frame2
   cu.nr. name
 A 33 Evo
 B 1 Mer
 C 1 Pold
 
 as you can see, there is only one row with equal cu.nr. and name. How can
 i
 match the data as desired?
 The solution has to look like this one:
 
   cu.nr. name  Value
 A 33 Evo
 B 1 Mer   80
 C 1 Pold
 
 thanks.
 
 Mat
 
 
 
 --
 View this message in context:
 http://r.789695.n4.nabble.com/match-in-dependence-of-2-columns-tp4658405.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.


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

__
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] match in dependence of 2 columns

2013-02-13 Thread Rui Barradas

Hello,

Try the following.


data.frame1 - read.table(text = 
  cu.nr. name  value
A 1 Evo 100
B 1 Mer 80
C 2 Ford50
, header = TRUE)


data.frame2 - read.table(text = 
  cu.nr. name
A 33 Evo
B 1 Mer
C 1 Pold
, header = TRUE)

merge(data.frame2, data.frame1, all.x = TRUE)



Hope this helps,

Rui Barradas

Em 13-02-2013 14:39, Mat escreveu:

Hello,

i want to match a column of one data.frame to another, my problem is, that i
only want to match the data, if 2 columns are equal.

i have a example:

data.frame1
   cu.nr. name  value
A 1 Evo 100
B 1 Mer 80
C 2 Ford50

And now i want to match the value to data.frame 2 in dependence of cu.nr.
and name.

data.frame2
   cu.nr. name
A 33 Evo
B 1 Mer
C 1 Pold

as you can see, there is only one row with equal cu.nr. and name. How can i
match the data as desired?
The solution has to look like this one:

   cu.nr. name  Value
A 33 Evo
B 1 Mer   80
C 1 Pold

thanks.

Mat



--
View this message in context: 
http://r.789695.n4.nabble.com/match-in-dependence-of-2-columns-tp4658405.html
Sent from the R help mailing list archive at Nabble.com.

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



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


Re: [R] match in dependence of 2 columns

2013-02-13 Thread Mat
thank you, this code works:

library(plyr)
join(dat1,dat2,by=c(cu.nr.,name),type=right) 

but my dat2 frame has a lot of columns, which i don't want to match.
How can i say, that only the column value should be match to dat1 ?

Thank you.

Mat




--
View this message in context: 
http://r.789695.n4.nabble.com/match-in-dependence-of-2-columns-tp4658405p4658508.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] match a complete data.frame

2013-02-12 Thread Mat
i found a way :-)

data-merge(data,project,by.x=PJ_ID,by.y=PROJ_ID,all.x=TRUE)

perhaps this can help someone

Mat



--
View this message in context: 
http://r.789695.n4.nabble.com/match-a-complete-data-frame-tp4658250p4658252.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] match a complete data.frame

2013-02-12 Thread David Winsemius

On Feb 12, 2013, at 5:10 AM, Mat wrote:

 hello togehter,
 
 how can i match a complete data.frame to another one?
 
 I know i can match one column with that code:
 
 A-match(data$PJ_ID,project$PROJ_ID)
 data$Name-proj$Name[A]

How about:

data - data[  match(data$PJ_ID,project$PROJ_ID) , ]

(Untested in the absence of data ... or 'data'.




 but how can i match the total data.frame from project to data?
 indivduel matching for each column is not very productive
 
-- 

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] match and substitute two variables

2012-12-03 Thread Rui Barradas

Hello,

Try the following.

for(nm in name){
code[grep(gsub([ [:alpha:]]+, , nm), code)] - nm
}
code


Hope this helps,

Rui Barradas
Em 03-12-2012 15:32, irene escreveu:

Hello,
I have two variables (of different length and from two different data
frames):

code- c(101001,  1032, 102, 101001, 102, 1032);
name- c(101001 Alta, 102 Bassa, 1032 Media);

and I would like to substitute the first variable with the  second variable
according to their shared numerical part, thus obtaining the following
result:

code.new
101001 Alta  1032 Media 102 Bassa101001 Alta 102 Bassa 1032
Media

I tried using: - sapply(code, gsub, pattern=\\d+, replacement=name) but
the replacement cannot be of length more than one, thus my output is only
101001 Alta 101001 Alta... I am not sure how to get the right answer...

Thank you!



--
View this message in context: 
http://r.789695.n4.nabble.com/match-and-substitute-two-variables-tp4651893.html
Sent from the R help mailing list archive at Nabble.com.

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


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


Re: [R] match and substitute two variables

2012-12-03 Thread irene
It works perfectly, thank you!



--
View this message in context: 
http://r.789695.n4.nabble.com/match-and-substitute-two-variables-tp4651893p4651906.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] match and substitute two variables

2012-12-03 Thread arun
Hi,
try this:
 paste(code,gsub(\\d+,,name)[match(code,gsub(\\D+,,name))],sep= )
#[1] 101001  Alta 1032  Media  102  Bassa   101001  Alta 102  Bassa  
#[6] 1032  Media 

A.K.



- Original Message -
From: irene irenerube...@yahoo.it
To: r-help@r-project.org
Cc: 
Sent: Monday, December 3, 2012 10:32 AM
Subject: [R] match and substitute two variables

Hello, 
I have two variables (of different length and from two different data
frames):

code- c(101001,  1032, 102, 101001, 102, 1032);
name- c(101001 Alta, 102 Bassa, 1032 Media);

and I would like to substitute the first variable with the  second variable
according to their shared numerical part, thus obtaining the following
result:

code.new
101001 Alta  1032 Media 102 Bassa101001 Alta 102 Bassa 1032
Media

I tried using: - sapply(code, gsub, pattern=\\d+, replacement=name) but
the replacement cannot be of length more than one, thus my output is only
101001 Alta 101001 Alta... I am not sure how to get the right answer...

Thank you!



--
View this message in context: 
http://r.789695.n4.nabble.com/match-and-substitute-two-variables-tp4651893.html
Sent from the R help mailing list archive at Nabble.com.

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


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


Re: [R] match values from data.frame and vector

2012-04-15 Thread peter dalgaard

On Apr 15, 2012, at 09:36 , Omphalodes Verna wrote:

 Dear R helpers!
 
 I have a vector 'x1' and data.frame 'df1'. Do you have any suggestion how to 
 get vector x2, which will be a result of matching values from vector 'x1' and 
 values from 'df1'? Please, see the example:
 
 x1 - c(rep(1,3), rep(NA,2), rep(2,4))
 df1 - data.frame(c1 = c(1,2), c2 = c(5,6))
 
 I would like to get vector x2:
 x2
 [1]  5  5  5 NA NA  6  6  6  6
 
 Thanks a lot, OV

You mean like this?

 df1$c2[match(x1, df1$c1)]
[1]  5  5  5 NA NA  6  6  6  6


-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@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] match matrices of different lengths

2012-01-05 Thread Justin Haynes
see ?merge

 merge(xx,aa,by.x='x',by.y='a')
x   y   b
1 2.00112e+11 1.0 1.2
2 2.00112e+11 1.1 1.9

making the two matricies time series does not mean that R knows that the
first column is a datetime.
and depending on your desired result, that may not be important.

hope that helps,

Justin


On Thu, Jan 5, 2012 at 5:51 AM, Thijs vanden Bergh 
bergh.thijsvan...@gmail.com wrote:

 was trying to match different matrices of different lengths with in
 the first collumn date and time info (yearmonthdayhourminute). the
 routine needs to return NA´s where data  of either of the matrices is
 non existent.

 have been trying the following:

 
 x - c(200112030003, 200112030004, 200112030005, 200112030006)
 y - c(0.1, 1, 1.1, 1.5)
 a - c(200112030004, 200112030005, 200112030007, 200112030008,
 200112030009)
 b - c(1.2, 1.9, 2.0, 2.5, 2.1)

 xx - cbind(x, y)
 aa - cbind(a, b)

 xxnew - ts(xx)
 aanew - ts(aa)

 cc - ts.union(xxnew, aanew)
 cc
 

 this does however not give the wished for result as it simply cbinds
 the two matrices and filles up empty spots that are created due to the
 one matrix being shorter then the other at the bottom end of the
 shortest matrix. i realy want the routine to match matrix xx and aa
 to time in the first collumn of both matrices.

 any help towards this end would be much appreciated,

 th.

 __
 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] match first consecutive list of capitalized words in string

2011-11-11 Thread Richter-Dumke, Jonas
Thank you very much for your help.

I'm using the second suggestion in my program and it works very well.

Jonas


-Original Message-
From: Gabor Grothendieck [mailto:ggrothendi...@gmail.com]
Sent: Thu 11/10/2011 5:58 AM
To: Richter-Dumke, Jonas
Cc: r-help@r-project.org
Subject: Re: [R] match first consecutive list of capitalized words in string
 
On Tue, Nov 8, 2011 at 7:48 AM, Richter-Dumke, Jonas
rich...@demogr.mpg.de wrote:
 Dear R-Helpers,

 this is my first post ever to a mailing list, so please feel free to point 
 out any missunderstandings on my side regarding the conventions of this 
 mailing list.

 My problem:

 Assuming the following character vector is given:

 names - c(filia Maria, vidua Joh Dirck Kleve (oo 02.02.1732), Bernardus 
 Engelb Franciscus Linde j.u.Doktor referendarius sereniss Judex et gograven 
 Rheinensis)

 Is there a regular expression matching the first consecutive list of 
 capitalized words in a single characterstring (Maria, Joh Dirck Kleve, 
 Bernardus Engelb Franciscus Linde)?
 This expression would very reliably seperate the person names from the 
 additional information in my historic church register transcription.


Try this. It matches a word boundary followed by zero or more of the
parenthesized expression.  That expression is an upper case letter
followed by zero or more lower case letters followed by one or more
spaces.  Finally we match the last word which consists of an upper
case letter followed by zero or more lower case letters and a word
boundary.  Note that it assumes R 2.14.0 or later:

 re - \\b([[:upper:]][[:lower:]]* +)*[[:upper:]][[:lower:]]*\\b
 regmatches(names, regexpr(re, names))
[1] Maria Joh Dirck Kleve
[3] Bernardus Engelb Franciscus Linde

-- 
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com


--
This mail has been sent through the MPI for Demographic ...{{dropped:10}}

__
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] match first consecutive list of capitalized words in string

2011-11-09 Thread Peter Alspach
Tena koe Jonas

Something like the following may help, although you should probably read the 
help on regexpr regarding locales.

Names - c(filia Maria, vidua Joh Dirck Kleve (oo 02.02.1732), Bernardus 
Engelb Franciscus Linde j.u.Doktor referendarius sereniss Judex et gograven 
Rheinensis)
Names1 - sub('^[0-9a-z]* ', '', Names)
Names1
ttReg - regexpr(' [^A-Z]', Names1)
ifelse (ttReg0, substring(Names1, 1, regexpr(' [^A-Z]', Names1)-1), Names1)

Incidentally, it is not good practice to call your objects 'names' since that 
is a function in R.

HTH 

Peter Alspach

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Richter-Dumke, Jonas
 Sent: Wednesday, 9 November 2011 1:49 a.m.
 To: r-help@r-project.org
 Subject: [R] match first consecutive list of capitalized words in
 string
 
 Dear R-Helpers,
 
 this is my first post ever to a mailing list, so please feel free to
 point out any missunderstandings on my side regarding the conventions
 of this mailing list.
 
 My problem:
 
 Assuming the following character vector is given:
 
 names - c(filia Maria, vidua Joh Dirck Kleve (oo 02.02.1732),
 Bernardus Engelb Franciscus Linde j.u.Doktor referendarius sereniss
 Judex et gograven Rheinensis)
 
 Is there a regular expression matching the first consecutive list of
 capitalized words in a single characterstring (Maria, Joh Dirck
 Kleve, Bernardus Engelb Franciscus Linde)?
 This expression would very reliably seperate the person names from the
 additional information in my historic church register transcription.
 
 Thank you very much for your effort,
 
 Jonas
 
 --
 This mail has been sent through the MPI for Demographic
 ...{{dropped:10}}
 
 __
 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.

The contents of this e-mail are confidential and may be subject to legal 
privilege.
 If you are not the intended recipient you must not use, disseminate, 
distribute or
 reproduce all or any part of this e-mail or attachments.  If you have received 
this
 e-mail in error, please notify the sender and delete all material pertaining 
to this
 e-mail.  Any opinion or views expressed in this e-mail are those of the 
individual
 sender and may not represent those of The New Zealand Institute for Plant and
 Food Research Limited.

__
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] match first consecutive list of capitalized words in string

2011-11-09 Thread Gabor Grothendieck
On Tue, Nov 8, 2011 at 7:48 AM, Richter-Dumke, Jonas
rich...@demogr.mpg.de wrote:
 Dear R-Helpers,

 this is my first post ever to a mailing list, so please feel free to point 
 out any missunderstandings on my side regarding the conventions of this 
 mailing list.

 My problem:

 Assuming the following character vector is given:

 names - c(filia Maria, vidua Joh Dirck Kleve (oo 02.02.1732), Bernardus 
 Engelb Franciscus Linde j.u.Doktor referendarius sereniss Judex et gograven 
 Rheinensis)

 Is there a regular expression matching the first consecutive list of 
 capitalized words in a single characterstring (Maria, Joh Dirck Kleve, 
 Bernardus Engelb Franciscus Linde)?
 This expression would very reliably seperate the person names from the 
 additional information in my historic church register transcription.


Try this. It matches a word boundary followed by zero or more of the
parenthesized expression.  That expression is an upper case letter
followed by zero or more lower case letters followed by one or more
spaces.  Finally we match the last word which consists of an upper
case letter followed by zero or more lower case letters and a word
boundary.  Note that it assumes R 2.14.0 or later:

 re - \\b([[:upper:]][[:lower:]]* +)*[[:upper:]][[:lower:]]*\\b
 regmatches(names, regexpr(re, names))
[1] Maria Joh Dirck Kleve
[3] Bernardus Engelb Franciscus Linde

-- 
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at 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] match and which give NA for some values, but not others

2011-07-27 Thread Daniel Nordlund
 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
 On Behalf Of Heemun Kwok
 Sent: Tuesday, July 26, 2011 8:50 PM
 To: r-help@r-project.org
 Subject: [R] match and which give NA for some values, but not others
 
 Hello list,
 I am having trouble with match and which giving a NA result for some
 values, but not others.  Here is a simple example:
 
  aaa - seq(0,1,by=0.05)
 
  aaa
  [1] 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65
 0.70
 [16] 0.75 0.80 0.85 0.90 0.95 1.00
 
  match(0.5,aaa)
 [1] 11
 
  match(0.6,aaa)
 [1] NA
 
  which(aaa==0.5)
 [1] 11
 
  which(aaa==0.6)
 integer(0)
 
  sessionInfo()
 R version 2.12.2 (2011-02-25)
 Platform: i386-pc-mingw32/i386 (32-bit)
 
 Am I missing something? Any thoughts?
 
 Thank you,
 Heemun
 
 

Take a look at R FAQ 7.31.

Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA 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] match and which give NA for some values, but not others

2011-07-27 Thread Rolf Turner


You are failing to take account of floating point arithmetic.
This is FAQ 7.31.

Really, it is advisable to check the FAQ before firing off a
naive inquiry to the r-help list.

cheers,

Rolf Turner

On 27/07/11 15:50, Heemun Kwok wrote:

Hello list,
I am having trouble with match and which giving a NA result for some 
values, but not others.  Here is a simple example:


aaa- seq(0,1,by=0.05)
aaa

  [1] 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70
[16] 0.75 0.80 0.85 0.90 0.95 1.00


match(0.5,aaa)

[1] 11


match(0.6,aaa)

[1] NA


which(aaa==0.5)

[1] 11


which(aaa==0.6)

integer(0)


sessionInfo()

R version 2.12.2 (2011-02-25)
Platform: i386-pc-mingw32/i386 (32-bit)

Am I missing something? Any thoughts?


__
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] Match strings across two differently sized dataframes and copy corresponding row to dataframe

2011-06-30 Thread jim holtman
?merge

On Thu, Jun 30, 2011 at 9:35 AM, Chris Beeley chris.bee...@gmail.com wrote:
 Hello-

 Sorry, this is a bit of a noob question, but I can't seem to progress
 it any further.

 I have two dataframes which contain a series of strings which exactly
 match. The problem is one has more rows than the other (more cases
 have been added) and they have been sorted so that they are not in the
 same order. The smaller dataframe, though, contains in another column
 which has codes classifying the strings.

 So, for every row of the larger dataframe, I want to look up the
 string in the smaller dataframe, and then use that row number to copy
 across the code for the string into the larger dataframe. Here's my
 idea so far:

 # comments is the smaller dataframe with the codes, mydata is the
 larger dataframe to which I would like to copy it.

 commvec=charmatch(comments$ImproveOne, mydata$Improve)  # this is the
 match between the strings one way
 datavec=charmatch(mydata$Improve, comments$ImproveOne) # this is the
 match the other way

 mydata$ImproveCat1=NA # produce a variable to hold the copied codes

 mydata$ImproveCat1[datavec[!is.na(datavec)]]=
 comments$ImproveCat[commvec[!is.na(commvec)]] # for all the non
 missing row numbers identified in the larger dataframe-
 # copy the corresponding code from the smaller dataframe (which lives
 in comments$ImproveCat

 However, the last command doesn't work because the variables are not
 the same length. They nearly are though, not sure if that's
 coincidence or shows I'm close

 length(mydata$ImproveCat1[datavec[!is.na(datavec)]]) # yields 1567

 length(comments$ImproveCat[commvec[!is.na(commvec)]]) # yields 1512

 I'm sorry, I did try to construct an example dataframe, but ironically
 I can't make that work either! Sorry!

 Any help gratefully received.

 Many thanks!

 Chris Beeley
 Institute of Mental Health, 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.




-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

__
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] Match numeric vector against rows in a matrix?

2011-01-11 Thread Kevin Ummel
Hi Petr,

Sorry for the delay. I ended up implementing a simple and fast but 
not-universal or elegant solution, since it turned out that I only needed to 
test a rather small subset of combinations for my purposes.

Nevertheless, I did go back and try the solutions posted. Bill's 'binary 
expansion' approach was the fastest by about a factor of 8.

Cheers,
Kevin

On Jan 11, 2011, at 9:20 AM, Petr Savicky wrote:

 Dear Kevin Ummel:
 
 There were several suggestions on R-help concering your question below.
 None of them suggests a base function. I would also expect that
 there is a base function for matching the rows. A related function is
 dist(), but it takes only one matrix as input and computes the distances
 between all pairs of its rows. Matching only some rows would require
 a modification of dist(), which would take two matrices and compare
 rows of one of them to the rows of the other. However, i do not know
 such a function in R.
 
 Is some of the suggestions on R-help suitable for your purposes?
 
 Thank you in advance for your kind reply.
 
 Best regards, Petr Savicky.
 
 On Wed, Jan 05, 2011 at 07:16:47PM +, Kevin Ummel wrote:
 Two posts in one day is not a good day...and this question seems like it 
 should have an obvious answer:
 
 I have a matrix where rows are unique combinations of 1's and 0's:
 
 combs=as.matrix(expand.grid(c(0,1),c(0,1)))
 combs
 Var1 Var2
 [1,]00
 [2,]10
 [3,]01
 [4,]11
 
 I want a single function that will give the row index containing an exact 
 match with vector x:
 
 x=c(0,1)
 
 The solution needs to be applied many times, so I need something quick -- I 
 was hoping a base function would do it, but I'm drawing a blank.
 
 Thanks!
 Kevin
 
 __
 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] Match numeric vector against rows in a matrix?

2011-01-07 Thread Petr Savicky
On Wed, Jan 05, 2011 at 07:16:47PM +, Kevin Ummel wrote:
 Two posts in one day is not a good day...and this question seems like it 
 should have an obvious answer:
 
 I have a matrix where rows are unique combinations of 1's and 0's:
 
  combs=as.matrix(expand.grid(c(0,1),c(0,1)))
  combs
  Var1 Var2
 [1,]00
 [2,]10
 [3,]01
 [4,]11
 
 I want a single function that will give the row index containing an exact 
 match with vector x:
 
  x=c(0,1)
 
 The solution needs to be applied many times, so I need something quick -- I 
 was hoping a base function would do it, but I'm drawing a blank.

If the matrix can have different number of columns, then
also the following can be used

  combs - as.matrix(expand.grid(c(0,1),c(0,1),c(0,1)))
  x - c(0,1,1)
  which(rowSums(combs != rep(x, each=nrow(combs))) == 0) # [1] 7

Petr Savicky.

__
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] Match numeric vector against rows in a matrix?

2011-01-07 Thread Henrique Dallazuanna
Try this:

 which(colSums(t(combs) == x) == ncol(combs))

On Wed, Jan 5, 2011 at 5:16 PM, Kevin Ummel kevinum...@gmail.com wrote:

 Two posts in one day is not a good day...and this question seems like it
 should have an obvious answer:

 I have a matrix where rows are unique combinations of 1's and 0's:

  combs=as.matrix(expand.grid(c(0,1),c(0,1)))
  combs
 Var1 Var2
 [1,]00
 [2,]10
 [3,]01
 [4,]11

 I want a single function that will give the row index containing an exact
 match with vector x:

  x=c(0,1)

 The solution needs to be applied many times, so I need something quick -- I
 was hoping a base function would do it, but I'm drawing a blank.

 Thanks!
 Kevin

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




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[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] Match numeric vector against rows in a matrix?

2011-01-07 Thread William Dunlap
 -Original Message-
 From: r-help-boun...@r-project.org 
 [mailto:r-help-boun...@r-project.org] On Behalf Of Petr Savicky
 Sent: Friday, January 07, 2011 2:11 AM
 To: r-help@r-project.org
 Subject: Re: [R] Match numeric vector against rows in a matrix?
 
 On Wed, Jan 05, 2011 at 07:16:47PM +, Kevin Ummel wrote:
  Two posts in one day is not a good day...and this question 
 seems like it should have an obvious answer:
  
  I have a matrix where rows are unique combinations of 1's and 0's:
  
   combs=as.matrix(expand.grid(c(0,1),c(0,1)))
   combs
   Var1 Var2
  [1,]00
  [2,]10
  [3,]01
  [4,]11
  
  I want a single function that will give the row index 
 containing an exact match with vector x:
  
   x=c(0,1)
  
  The solution needs to be applied many times, so I need 
 something quick -- I was hoping a base function would do it, 
 but I'm drawing a blank.
 
 If the matrix can have different number of columns, then
 also the following can be used
 
   combs - as.matrix(expand.grid(c(0,1),c(0,1),c(0,1)))
   x - c(0,1,1)
   which(rowSums(combs != rep(x, each=nrow(combs))) == 0) # [1] 7

If the combs matrix always has that form, why not
dispense with it and treat x as the binary expansion
of the row number (+1)?
1 + sum( 2^(seq_along(x)-1) * x )
   [1] 7

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 
 
 Petr Savicky.
 
 __
 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] Match numeric vector against rows in a matrix?

2011-01-05 Thread David Winsemius


On Jan 5, 2011, at 2:16 PM, Kevin Ummel wrote:

Two posts in one day is not a good day...and this question seems  
like it should have an obvious answer:


I have a matrix where rows are unique combinations of 1's and 0's:


combs=as.matrix(expand.grid(c(0,1),c(0,1)))
combs

Var1 Var2
[1,]00
[2,]10
[3,]01
[4,]11

I want a single function that will give the row index containing an  
exact match with vector x:



x=c(0,1)


 intersect( which(combs[,1]==x[1]), which(combs[,2]==x[2]) )
[1] 3

Or maybe even faster:

  which( combs[,1]==x[1]  combs[,2]==x[2])
[1] 3



The solution needs to be applied many times, so I need something  
quick -- I was hoping a base function would do it, but I'm drawing a  
blank.




--

David Winsemius, MD
West Hartford, CT

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


Re: [R] Match() on raw objects ?

2010-07-29 Thread bruno Piguet
On Tue, 27 Jul 2010, Prof Brian Ripley wrote :


 Note that raw values in R are really intended to be passed around and not
 manipulated: if you do much of the latter, coercing to integer, say, is
 likely to be much more efficient.

 Indeed :

I tried a minimal benchmark :

a - charToRaw(paste(citation(), \002, licence(), \003, garbage))
proc.time()
for (i in 1:1) {
   j0 - match (as.raw(2), a)
   j1 - match (as.raw(3), a)
}
proc.time()
for (i in 1:1) {
   b - as.integer(a)
   j0 - match (2, b)
   j1 - match (3, b)
}
proc.time()

In this case, the solution using as.integer() looks about 4 time faster.

Bruno.

[[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] Match() on raw objects ?

2010-07-27 Thread Prof Brian Ripley

You failed to supply a raw value to match, and if you do it works:


match(as.raw(2), a)

[1] 9

for the ninth value is *not* '2, indeed': it is as.raw(2).

I think you are confusing R objects with their printed representation.
(In this case values are coerced to strings, and a[9] is thus coerced 
to 02, which is not matched by the integer 02 nor the byte \002.)


On Tue, 27 Jul 2010, bruno Piguet wrote:


Dear all,

  Sorry to bother you with a question that must be a FAQ, but I'm not
clever enough to have Google give me the answer.

  I want to filter out some bytes that I read in a file, before applying
rawToChar (to prevent null character problems)

  So, I tried things like match(some_byte, my_raw_tab),   but this
didn't work.

  Here is a minimal example :

a - charToRaw(paste(garbage, \002, some text, \003, garbage))
print (a[9])
i - match (02, a)
j - match ('\002', a)
print (c(i, j))

 All I get as result of match calls are NA's, while print (a[9]) ensures
me that the ninth value of a is 2, indeed.

  Of course, hand-made search gives me the result :
for (i in seq(length(a)))
{
 if (a[i] == 2) {
   print (i)
 }
}

Any hints ?

Thanks in advance,

Bruno.
PS : R version 2.11.0 (2010-04-22)

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



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

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


Re: [R] Match() on raw objects ?

2010-07-27 Thread bruno Piguet
Dear Prof Ripley,

You wrote :

 You failed to supply a raw value to match, and if you do it works:

  match(as.raw(2), a)

 [1] 9

 for the ninth value is *not* '2, indeed': it is as.raw(2).


Thanks for the clarification.

I think you are confusing R objects with their printed representation.


   Yes, I must admit. And in this case I was confused by the fact that my
implementation of a replacement function doing a step-by-step search worked
without any need of as.raw()

my_match - function (needle, hay_stack) {
  for (i in seq(length(hay_stack)))
  {
if (hay_stack[i] == needle) {
  return(i)
}
  }
  return(NA)
}

So, even if the ninth value is *not* '2, but as.raw(2), the comparison a[9]
== 2 is true.


 (In this case values are coerced to strings, and a[9] is thus coerced to
 02, which is not matched by the integer 02 nor the byte \002.)


   OK.  Had I tried 02, it would have worked, but only by chance.

   For my personal guidance, is there a search function that does not coerce
to string, and thus would be faster ?
   (even if I must admit that, currently match(as.raw(), ) is fast enough
and about three times faster than my very naïve replacement).

Bruno.

[[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] Match() on raw objects ?

2010-07-27 Thread Prof Brian Ripley

On Tue, 27 Jul 2010, bruno Piguet wrote:


Dear Prof Ripley,

You wrote :
  You failed to supply a raw value to match, and if you do it
  works:

match(as.raw(2), a)

  [1] 9

  for the ninth value is *not* '2, indeed': it is as.raw(2).


Thanks for the clarification.

  I think you are confusing R objects with their printed
  representation.


   Yes, I must admit. And in this case I was confused by the fact that my
implementation of a replacement function doing a step-by-step search worked
without any need of as.raw()

my_match - function (needle, hay_stack) {
  for (i in seq(length(hay_stack)))
  {
    if (hay_stack[i] == needle) {
  return(i)
    }
  }
  return(NA)
}

So, even if the ninth value is *not* '2, but as.raw(2), the comparison a[9]
== 2 is true.


Correct.  That coerces to integer.


  (In this case values are coerced to strings, and a[9] is thus
  coerced to 02, which is not matched by the integer 02 nor the
  byte \002.)


   OK.  Had I tried 02, it would have worked, but only by chance.


No, not 'by chance': that is what the help page for match() tells you 
happens.



   For my personal guidance, is there a search function that does not coerce
to string, and thus would be faster ?
   (even if I must admit that, currently match(as.raw(), ) is fast enough
and about three times faster than my very naïve replacement).


Depends on how many values you want to look up.

Note that raw values in R are really intended to be passed around and 
not manipulated: if you do much of the latter, coercing to integer, 
say, is likely to be much more efficient.



Bruno.




--
Brian D. Ripley,  rip...@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Match() on raw objects ?

2010-07-27 Thread bruno Piguet
on 2010/7/27, Prof Brian Ripley wrote :


OK.  Had I tried 02, it would have worked, but only by chance.


 No, not 'by chance': that is what the help page for match() tells you
 happens.


Well, i mean that *I* would have typed  02 by chance.

I read, but didn't fully understand the sentence :
 Factors, raw vectors and lists are converted to character vectors,
  and then ‘x’ and ‘table’ are coerced to a common type (the later
   of the two types in R's ordering, etc,

I thought that the eventual common type would be integer, not character.

But it's clear, now.

Thanks again,

Bruno.

[[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] Match 2 vectors

2010-05-28 Thread Dennis Murphy
Hi:

On Thu, May 27, 2010 at 10:26 PM, Kang Min ngokang...@gmail.com wrote:

 Hi,

 I have 2 dataframes of unequal length, and I would like to match a
 factor to them so that both dataframes will have the same number of
 rows.

 example:
 # create the 2 dataframes with unequal length
 data1 - data.frame(letters, 1:26)[-c(5,10,19:21),]
 data2 - data.frame(letters, 1:26)[-c(6,9,15:18),]


If this is your real problem, then an easy thing to do is

data1 - data2 - data.frame(letters[1:26])
data1[c(5,10,19:21), ] - NA
data2[c(6, 9, 15:18), ] - NA
data3 - cbind(data1, data2)
names(data3, c('lett1', 'lett2'))
data3

However, I suspect this is not your real problem, so let's add a variable to
each of the data frames you posted and try again:

data1 - data.frame(letters, 1:26)[-c(5,10,19:21),]
data2 - data.frame(letters, 1:26)[-c(6,9,15:18),]

data1$x - rpois(nrow(data1), 10)
data2$y - rpois(nrow(data2), 5)

# Now merge data1 and data2:
(data3 - merge(data1, data2, all = TRUE))

   letters X1.26  x  y
1a 1  7  1
2b 2 13  3
3c 3 10  5
4d 4  4  9
5e 5 NA  4
6f 6 10 NA
7g 7 15  4
8h 8 10  6
9i 9 12 NA
10   j10 NA  3
11   k11  8  6
12   l12 12  3
13   m13 10  3
14   n14  7  8
15   o15  6 NA
16   p16  8 NA
17   q17  6 NA
18   r18  4 NA
19   s19 NA  2
20   t20 NA  6
21   u21 NA  4
22   v22 12  6
23   w23 10  3
24   x24  7  8
25   y25  9  6
26   z26 15  7

To get rid of the second column, set data3[, 2] - NULL.

HTH,
Dennis


 data2a - match(data1[,1], data2[,1])
 data2b - data2[data2a,]

 When I match data1 to data2, and combine the data2a vector to the
 original data2, I'm still missing some rows. I need to get the 26
 rows, and preferably with the first column displaying all the levels.
 In data2b the mismatches show up as NA in the whole row.

 Thanks.
 Kang Min

 __
 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] Match 2 vectors

2010-05-28 Thread Kang Min
That's what I wanted, thanks!!


On May 28, 5:13 pm, Dennis Murphy djmu...@gmail.com wrote:
 Hi:

 On Thu, May 27, 2010 at 10:26 PM, Kang Min ngokang...@gmail.com wrote:
  Hi,

  I have 2 dataframes of unequal length, and I would like to match a
  factor to them so that both dataframes will have the same number of
  rows.

  example:
  # create the 2 dataframes with unequal length
  data1 - data.frame(letters, 1:26)[-c(5,10,19:21),]
  data2 - data.frame(letters, 1:26)[-c(6,9,15:18),]

 If this is your real problem, then an easy thing to do is

     data1 - data2 - data.frame(letters[1:26])
     data1[c(5,10,19:21), ] - NA
     data2[c(6, 9, 15:18), ] - NA
     data3 - cbind(data1, data2)
     names(data3, c('lett1', 'lett2'))
     data3

 However, I suspect this is not your real problem, so let's add a variable to
 each of the data frames you posted and try again:

 data1 - data.frame(letters, 1:26)[-c(5,10,19:21),]
 data2 - data.frame(letters, 1:26)[-c(6,9,15:18),]

 data1$x - rpois(nrow(data1), 10)
 data2$y - rpois(nrow(data2), 5)

 # Now merge data1 and data2:
 (data3 - merge(data1, data2, all = TRUE))

    letters X1.26  x  y
 1        a     1  7  1
 2        b     2 13  3
 3        c     3 10  5
 4        d     4  4  9
 5        e     5 NA  4
 6        f     6 10 NA
 7        g     7 15  4
 8        h     8 10  6
 9        i     9 12 NA
 10       j    10 NA  3
 11       k    11  8  6
 12       l    12 12  3
 13       m    13 10  3
 14       n    14  7  8
 15       o    15  6 NA
 16       p    16  8 NA
 17       q    17  6 NA
 18       r    18  4 NA
 19       s    19 NA  2
 20       t    20 NA  6
 21       u    21 NA  4
 22       v    22 12  6
 23       w    23 10  3
 24       x    24  7  8
 25       y    25  9  6
 26       z    26 15  7

 To get rid of the second column, set data3[, 2] - NULL.

 HTH,
 Dennis





  data2a - match(data1[,1], data2[,1])
  data2b - data2[data2a,]

  When I match data1 to data2, and combine the data2a vector to the
  original data2, I'm still missing some rows. I need to get the 26
  rows, and preferably with the first column displaying all the levels.
  In data2b the mismatches show up as NA in the whole row.

  Thanks.
  Kang Min

  __
  r-h...@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-h...@r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guidehttp://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] match function or ==

2010-04-08 Thread Matthew Dowle
Please install v1.3 from R-forge :
install.packages(data.table,repos=http://R-Forge.R-project.org;)

It will be ready for CRAN soon.

Please follow up on datatable-h...@lists.r-forge.r-project.org

Matthew

bo bozha...@hotmail.com wrote in message 
news:1270689586866-1755876.p...@n4.nabble.com...

 Thank you very much for the help.

 I installed data.table package, but I keep getting the following warnings:

 setkey(DT,id,date)
 Warning messages:
 1: In `[.data.table`(deref(x), o) :
 This R session is  2.4.0. Please upgrade to 2.4.0+.

 I'm using R 2.10, but why I keep getting warnings on upgrades. Thanks 
 again.


 -- 
 View this message in context: 
 http://n4.nabble.com/match-function-or-tp1754505p1755876.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] match function or ==

2010-04-07 Thread Patrick Burns

You might want to investigate the
'data.table' package.

On 07/04/2010 16:15, bo wrote:


Hi all,

I have a huge dataset(5000k observations), which contains the daily sales
for each company.
If I want to find out the a company with unique company id number, which
function I should use that is more efficient, match or ==?  For example,

use-dataset[!is.na(match(dataset$companyID, 12345)),]
or
use-dataset[dataset$companyID==12345,]

Thank you very much.



--
Patrick Burns
pbu...@pburns.seanet.com
http://www.burns-stat.com
(home of 'Some hints for the R beginner'
and 'The R Inferno')

__
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] match function or ==

2010-04-07 Thread bo

Thank you very much for the help. 

I installed data.table package, but I keep getting the following warnings:

 setkey(DT,id,date)
Warning messages:
1: In `[.data.table`(deref(x), o) :
This R session is  2.4.0. Please upgrade to 2.4.0+.

I'm using R 2.10, but why I keep getting warnings on upgrades. Thanks again.


-- 
View this message in context: 
http://n4.nabble.com/match-function-or-tp1754505p1755876.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] match problem

2009-12-15 Thread Jim Holtman

?merge

What is the problem you are trying to solve?

Sent from my iPhone.

On Dec 15, 2009, at 4:50, Bunny, lautloscrew.com bu...@lautloscrew.com 
 wrote:



Hi all,

I dont know if match is the right approach here. I´d like to match t 
o data.frames. One big dataframe and one small dataframe. In SQL, wh 
at i want to do what only be simple relation.


The first consists of two columns a) some value b) some key that is  
explained in the other dataframe. What i want to do is create  
(cbind) both to one dataframe like:


df1:

a  b
1  2
2  3
3  2
4  2
5  1

df2:
1 class1
2 class2
3 class3


to newdf:

a b class
1 2 class2
2 3 class3
3 2 class2
...
and so forth

I have connected R to several relational databases but I dont think  
it´s necessary here and that there should be a simpler solution for  
my problem. Maybe some combination of do.call, mapply, lapply or mat 
ch can do the job.
Unfortunately I am not really familiar with these and still keep  
tryin.


thx in advance for any help.

Best regards

matt

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

2009-07-01 Thread Jorge Ivan Velez
Dear Christopher,
Try this:

merge(x,y,all=TRUE)

HTH,

Jorge


On Wed, Jul 1, 2009 at 2:51 PM, Hane, Christopher A 
christopher.h...@ingenixconsulting.com wrote:


  Hello,

 I'm trying to vectorize some assignment statements using match(), but
 can't seem to get it correct.

 I have 2 data frames each with a key column of unique values. I want to
 copy a column from one frame to another where the key values are the
 same.  The data frames are not the same length, and the set of keys is
 non-overlapping (each frame has keys not in the other).

 Example:
 x - data.frame(id=c(1,3,4,6,7,9,10), r=runif(7))
  x
  id r
 1  1 0.4243219
 2  3 0.2389127
 3  4 0.7094532
 4  6 0.2053836
 5  7 0.9630027
 6  9 0.1218458
 7 10 0.9183175
  y - data.frame(id=c(1,2,4,7,8,9,10,11,12))
  y
  id
 1  1
 2  2
 3  4
 4  7
 5  8
 6  9
 7 10
 8 11
 9 12

 I want to copy the x$r values to y to obtain
 y
  idr
 1  10.4243219
 2  2NA
 3  40.7094532
 4  70.9630027
 5  8NA
 6  90.1218458
 7 100.9183175
 8 11NA
 9 12NA

 xIdx - match(x$id,y$id) yields a vector of length x, so it cannot be
 used directly to copy to y.

 Any help in vectorizing this assignment would be most welcome.

 Chris

 This e-mail, including attachments, may include confidential and/or
 proprietary information, and may be used only by the person or entity
 to which it is addressed. If the reader of this e-mail is not the intended
 recipient or his or her authorized agent, the reader is hereby notified
 that any dissemination, distribution or copying of this e-mail is
 prohibited. If you have received this e-mail in error, please notify the
 sender by replying to this message and delete this e-mail immediately.

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

2008-12-02 Thread Peter Alspach
Tena koe Robert

You could first apply paste

apply(types, 1, paste, collapse=':')

and then match.

HTH 

Peter Alspach 

 -Original Message-
 From: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] On Behalf Of threshold
 Sent: Wednesday, 3 December 2008 8:32 a.m.
 To: r-help@r-project.org
 Subject: [R] match
 
 
 Hi, I would like to check which rows of 'types.prev' matrix 
 pop up in 'types', following R in-built procedure. I tried 
 'match' function but it works in case of the one dimensional vectors.
 Will appreciate any suggestions. 
 best, robert 
 
  types
  edateK  
  [1,] 20060819 12.5
  [2,] 20060819 17.5
  [3,] 20060819 22.5
  [4,] 20070217 12.5
  [5,] 20060617 10.0
  [6,] 20060617 15.0
  [7,] 20060520 12.5
  [8,] 20060520 15.0
  [9,] 20060520 17.5
 [10,] 20061118 10.0
 [11,] 20061118 12.5
 [12,] 20061118 15.0
 [13,] 20061118 17.5 
 
  types.prev
  edateK  
  [1,] 20060819 10.0
  [2,] 20060819 12.5
  [3,] 20060819 17.5
  [4,] 20070217 10.0
  [5,] 20070217 12.5
  [6,] 20070217 15.0
  [7,] 20060617 10.0
  [8,] 20060617 12.5
  [9,] 20060617 15.0
 [10,] 20060617 20.0
 [11,] 20060520 10.0
 [12,] 20060520 12.5
 [13,] 20060520 17.5
 [14,] 20061118 12.5
 [15,] 20061118 15.0
 [16,] 20061118 17.5
 
 
 --
 View this message in context: 
 http://www.nabble.com/match-tp20799206p20799206.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.
 

The contents of this e-mail are confidential and may be subject to legal 
privilege.
 If you are not the intended recipient you must not use, disseminate, 
distribute or
 reproduce all or any part of this e-mail or attachments.  If you have received 
this
 e-mail in error, please notify the sender and delete all material pertaining 
to this
 e-mail.  Any opinion or views expressed in this e-mail are those of the 
individual
 sender and may not represent those of The New Zealand Institute for Plant and
 Food Research Limited.

__
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] match and incomparables

2008-09-12 Thread Adam D. I. Kramer

I can replicate this and also do not understand it.


match(1:3,1:3,incomparables=5)

[1] NA  2  3

match(1:3,1:3,incomparables=4)

[1] 1 2 3

match(1:3,1:3,incomparables=3)

[1] 1 2 3

match(1:3,1:3,incomparables=2)

[1] 1 2 3

match(1:3,1:3,incomparables=1)

[1] NA  2  3

...every other integer value for incomparables produces 1 2 and 3 for
output. I'm using R 2.7.2, self-compiled, under linux.

--Adam

On Fri, 12 Sep 2008, McGehee, Robert wrote:


Hello,
I was playing around with the newly implemented 'incomparables' argument
in 'match' and realized the argument does not behave anything like I
expected. Can someone explain what is going on here? Sorry if I'm
misreading the documentation.


match(1:3, 1:3, incomparables=1)

[1] NA  2  3  # This seems right, the 1 in 'x' is 'incomparable'


match(1:3, 1:3, incomparables=2)

[1] 1  2  3   # Shouldn't this be 1 NA 3? Why isn't the 2 incomparable?


match(1:3, 1:3, incomparables=5)

[1] NA  2  3   # Why isn't the 5 ignored?

Note from ?match:
incomparables: a vector of values that cannot be matched. Any value in
x matching a value in this vector is assigned the nomatch value. For
historical reasons, FALSE is equivalent to NULL.

Thanks in advance!
Robert

platform   i386-pc-mingw32
arch   i386
os mingw32
system i386, mingw32
status
major  2
minor  7.2
year   2008
month  08
day25
svn rev46428
language   R
version.string R version 2.7.2 (2008-08-25)

Robert McGehee, CFA
Geode Capital Management, LLC
One Post Office Square, 28th Floor | Boston, MA | 02109
Tel: 617/392-8396Fax:617/476-6389
mailto:[EMAIL PROTECTED]



This e-mail, and any attachments hereto, are intended fo...{{dropped:12}}

__
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] match and incomparables

2008-09-12 Thread Bert Gunter
Looks like a bug to me. Compare:

 match (1:3,1:3,incom=3)
[1] 1 2 3
 match (1:3,1:3,incom=2:3)
[1] 1 2 3
 match (1:3,1:3,incom=1:2)
[1] NA NA  3
 match (1:3,1:3,incom=1)
[1] NA  2  3

Cheers,
Bert


-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Adam D. I. Kramer
Sent: Friday, September 12, 2008 3:06 PM
To: McGehee, Robert
Cc: r-help
Subject: Re: [R] match and incomparables

I can replicate this and also do not understand it.

 match(1:3,1:3,incomparables=5)
[1] NA  2  3
 match(1:3,1:3,incomparables=4)
[1] 1 2 3
 match(1:3,1:3,incomparables=3)
[1] 1 2 3
 match(1:3,1:3,incomparables=2)
[1] 1 2 3
 match(1:3,1:3,incomparables=1)
[1] NA  2  3

...every other integer value for incomparables produces 1 2 and 3 for
output. I'm using R 2.7.2, self-compiled, under linux.

--Adam

On Fri, 12 Sep 2008, McGehee, Robert wrote:

 Hello,
 I was playing around with the newly implemented 'incomparables' argument
 in 'match' and realized the argument does not behave anything like I
 expected. Can someone explain what is going on here? Sorry if I'm
 misreading the documentation.

 match(1:3, 1:3, incomparables=1)
 [1] NA  2  3  # This seems right, the 1 in 'x' is 'incomparable'

 match(1:3, 1:3, incomparables=2)
 [1] 1  2  3   # Shouldn't this be 1 NA 3? Why isn't the 2 incomparable?

 match(1:3, 1:3, incomparables=5)
 [1] NA  2  3   # Why isn't the 5 ignored?

 Note from ?match:
 incomparables: a vector of values that cannot be matched. Any value in
 x matching a value in this vector is assigned the nomatch value. For
 historical reasons, FALSE is equivalent to NULL.

 Thanks in advance!
 Robert

 platform   i386-pc-mingw32
 arch   i386
 os mingw32
 system i386, mingw32
 status
 major  2
 minor  7.2
 year   2008
 month  08
 day25
 svn rev46428
 language   R
 version.string R version 2.7.2 (2008-08-25)

 Robert McGehee, CFA
 Geode Capital Management, LLC
 One Post Office Square, 28th Floor | Boston, MA | 02109
 Tel: 617/392-8396Fax:617/476-6389
 mailto:[EMAIL PROTECTED]



 This e-mail, and any attachments hereto, are intended fo...{{dropped:12}}

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


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

__
R-help@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] match and incomparables

2008-09-12 Thread Prof Brian Ripley

On Fri, 12 Sep 2008, Bert Gunter wrote:


Looks like a bug to me. Compare:


A bug fixed in the R-devel version of R (because of a related problem with 
duplicated).



match (1:3,1:3,incom=3)

[1] 1 2 3

match (1:3,1:3,incom=2:3)

[1] 1 2 3

match (1:3,1:3,incom=1:2)

[1] NA NA  3

match (1:3,1:3,incom=1)

[1] NA  2  3

Cheers,
Bert


-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Adam D. I. Kramer
Sent: Friday, September 12, 2008 3:06 PM
To: McGehee, Robert
Cc: r-help
Subject: Re: [R] match and incomparables

I can replicate this and also do not understand it.


match(1:3,1:3,incomparables=5)

[1] NA  2  3

match(1:3,1:3,incomparables=4)

[1] 1 2 3

match(1:3,1:3,incomparables=3)

[1] 1 2 3

match(1:3,1:3,incomparables=2)

[1] 1 2 3

match(1:3,1:3,incomparables=1)

[1] NA  2  3

...every other integer value for incomparables produces 1 2 and 3 for
output. I'm using R 2.7.2, self-compiled, under linux.

--Adam

On Fri, 12 Sep 2008, McGehee, Robert wrote:


Hello,
I was playing around with the newly implemented 'incomparables' argument
in 'match' and realized the argument does not behave anything like I
expected. Can someone explain what is going on here? Sorry if I'm
misreading the documentation.


match(1:3, 1:3, incomparables=1)

[1] NA  2  3  # This seems right, the 1 in 'x' is 'incomparable'


match(1:3, 1:3, incomparables=2)

[1] 1  2  3   # Shouldn't this be 1 NA 3? Why isn't the 2 incomparable?


match(1:3, 1:3, incomparables=5)

[1] NA  2  3   # Why isn't the 5 ignored?

Note from ?match:
incomparables: a vector of values that cannot be matched. Any value in
x matching a value in this vector is assigned the nomatch value. For
historical reasons, FALSE is equivalent to NULL.

Thanks in advance!
Robert

platform   i386-pc-mingw32
arch   i386
os mingw32
system i386, mingw32
status
major  2
minor  7.2
year   2008
month  08
day25
svn rev46428
language   R
version.string R version 2.7.2 (2008-08-25)

Robert McGehee, CFA
Geode Capital Management, LLC
One Post Office Square, 28th Floor | Boston, MA | 02109
Tel: 617/392-8396Fax:617/476-6389
mailto:[EMAIL PROTECTED]



This e-mail, and any attachments hereto, are intended fo...{{dropped:12}}

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

http://www.R-project.org/posting-guide.html

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



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

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



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

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


Re: [R] match problem by rownames

2008-09-09 Thread Charilaos Skiadas

As suggested in ?[.data.frame, try:

dat[match('a1', rownames(dat)),]


Haris Skiadas
Department of Mathematics and Computer Science
Hanover College

On Sep 9, 2008, at 2:41 AM, Xianming Wei wrote:


Hi all,

While dat['a1',] and dat['a10',] produce the same results in the
following example, I'd like dat['a1',] to return NAs.

dat - data.frame(x1 = paste(letters[1:5],10, sep=''), x2=rnorm(5))
rownames(dat) - dat$x1
dat['a1',]
dat['a10',]


sessionInfo()

R version 2.7.2 (2008-08-25)
i386-pc-mingw32

locale:
LC_COLLATE=English_Australia.1252;LC_CTYPE=English_Australia. 
1252;LC_MON
ETARY=English_Australia.1252;LC_NUMERIC=C;LC_TIME=English_Australia. 
1252


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


other attached packages:
[1] lattice_0.17-13

loaded via a namespace (and not attached):
[1] grid_2.7.2




Regards,
Xianming



__
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] match problem by rownames

2008-09-09 Thread Dimitris Rizopoulos
try this:

dat - data.frame(x1 = paste(letters[1:5],10, sep=''), x2=rnorm(5))
row.names(dat) - dat$x1

dat['a1' %in% row.names(dat), ]
dat['a10'  %in% row.names(dat), ]


I hope it helps.

Best,
Dimitris


 Hi all,

 While dat['a1',] and dat['a10',] produce the same results in the
 following example, I'd like dat['a1',] to return NAs.

 dat - data.frame(x1 = paste(letters[1:5],10, sep=''), x2=rnorm(5))
 rownames(dat) - dat$x1
 dat['a1',]
 dat['a10',]

 sessionInfo()
 R version 2.7.2 (2008-08-25)
 i386-pc-mingw32

 locale:
 LC_COLLATE=English_Australia.1252;LC_CTYPE=English_Australia.1252;LC_MON
 ETARY=English_Australia.1252;LC_NUMERIC=C;LC_TIME=English_Australia.1252

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


 other attached packages:
 [1] lattice_0.17-13

 loaded via a namespace (and not attached):
 [1] grid_2.7.2


 Regards,
 Xianming



 DISCLAIMER:\ For details of our e-mail disclaimer, pleas...{{dropped:15}}

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



-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043399
Fax: +31/(0)10/7044657

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

2007-11-22 Thread jim holtman
Can you provide an example of the input data and what you would like
the output to be.  When you say first entry is this a single cell,
or the whole row?  So an example would help to understand what you are
after.

On Nov 22, 2007 8:45 AM, Marco chiapello [EMAIL PROTECTED] wrote:
 Hi,
 I'm new about R. My problem is:
 I have 2 lists of proteins and I would compare them. I import the 2
 lists like 2 different matrixs and I would that the first entry in the
 matrix 1 match with all entries of the matrix 2, then the second entry
 of the matrix 1 matchs with all entries of the matrix 2 and repeat
 this process with all entries of the matrix 1. Anybodies know can I do
 it?
 Thanks,
 Marco
 -
 Contatti
  *Mail:
   [EMAIL PROTECTED]
   [EMAIL PROTECTED]
  *Messanger:
   [EMAIL PROTECTED]
  *Skype:
   marpello
  *Sito:
   http://web.mac.com/marpello/iWeb/Sito/index.html

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




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

What is the problem you are trying to solve?

__
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] Match and replace between two data.frames

2007-10-25 Thread Henrique Dallazuanna
HI,

Perhaps:

cbind(ID=DF$ID,as.data.frame(sapply(levels(VAL$type),
function(x){DF[,x]=VAL$number[VAL$type==x];DF[,x]})))


On 25/10/2007, Lauri Nikkinen [EMAIL PROTECTED] wrote:

 Hi folks,

 I have two dataframes like these:

 DF - data.frame(ID=c(AA1234,AB3233,AC4353,AD2345,AE7453),
 CK32344=c(1,3,2,4,1), CK32664=c(2,1,1,2,3), CK33422=c(2,2,1,3,2))
 VAL - data.frame(num=rep(6,3), type=c(CK32344, CK32664,
 CK33422), number=c(32,452,234))

 I want to replace the values in DF$CK32344, DF$CK32664 DF$CK33422 with
 the corresponding number in the VAL$number.

 The results should look like this:

 RES - data.frame(ID=c(AA1234,AB3233,AC4353,AD2345,AE7453),
 CK32344=rep(32, 5), CK32664=rep(425, 5), CK33422=rep(234, 5))

 How do I do that?

 Regards,
 Lauri

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




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

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