This code works for me,

subset(merge(x1, x2, by = c("category", "id"), all = T), select=-id)
   category rnorm.10..5..2. rnorm.10..8..2.
1         1              NA        7.810377
2         1        6.493281        9.002411
3         1        6.493281        8.397378
4         1        2.757321        9.002411
5         1        2.757321        8.397378
6         1        5.815198        9.002411
7         1        5.815198        8.397378
8         1              NA        9.289451
9         2        6.653888       12.770012
10        2        6.653888       11.201305
11        2        3.372373       12.770012
12        2        3.372373       11.201305
13        2        3.986016        7.044181
14        2        7.544480        6.238959
15        3        4.540055              NA
16        3              NA       11.282507
17        3        4.181084              NA
18        3        5.973262              NA
19        3              NA        7.413175




On Wed, Jun 17, 2009 at 10:50 PM, Martin Batholdy
<batho...@googlemail.com>wrote:

> thanks a lot!
> That is really a big help.
>
>
> I tried this code;
>
> x1 <- data.frame(category = sample(3, 10, r=TRUE), rnorm(10, 5, 2))
> x2 <- data.frame(category = sample(3, 10, r=TRUE), rnorm(10, 8, 2))
>
>
> x1$id <- unlist(with(x1, tapply(x1[,2], x1[,1], seq)))
> x2$id <- unlist(with(x2, tapply(x2[,2], x2[,1], seq)))
> subset(merge(x1, x2, by = c("category", "id"), all = T), select = -id)
>
>
> But the code sometimes shows this error message;
>
> Error in `$<-.data.frame`(`*tmp*`, "id", value = c(1L, 2L, 3L, 4L, 5L,  :
>   replacement has 17 rows, data has 10
>
>
>
>
>
> Am 18.06.2009 um 03:32 schrieb Henrique Dallazuanna:
>
> Yes, try this:
>
> x1$id <- unlist(with(x1, tapply(x, x, seq)))
> x2$id <- unlist(with(x2, tapply(x, x, seq)))
> subset(merge(x1, x2, by = c("x", "id"), all = T), select = -id)
>
> On Wed, Jun 17, 2009 at 10:24 PM, Martin Batholdy <batho...@googlemail.com
> > wrote:
>
>> Is because the category is not unique, then merge are replicate the
>> values.
>>
>>
>> Yeah, ok.
>> But that is exactly my problem with the actual data set, I don't want
>> "new" rows;
>>
>> If I have;
>>
>> x1
>> 1 4
>> 1 3
>> 1 6
>> 2 9
>> 2 2
>>
>>
>> x2
>> 1 -3
>> 1 -7
>> 2 -3
>> 2 -2
>>
>>
>>
>> I want this data.frame;
>>
>> x1.x2
>> 1 4 -3
>> 1 3 -7
>> 1 6  NA
>> 2 9 -3
>> 2 2 -2
>>
>> or
>>
>> x1.x2
>> 1 4 -3
>> 1 3 -7
>> 2 9 -3
>> 2 2 -2
>>
>>
>>
>>
>> It does not matter to me if the rows are merged like;
>>
>> 1 4 -3
>> 1 3 -7
>>
>> or
>>
>> 1 4 -7
>> 1 3 -3
>>
>>
>>
>>
>> can I also do something like that with merge() ?
>>
>>
>>
>> Am 18.06.2009 um 03:18 schrieb Henrique Dallazuanna:
>>
>> Is because the category is not unique, then merge are replicate the
>> values.
>>
>> Try this example:
>>
>> x <- data.frame(category=1:10, rnorm(10))
>> y <- data.frame(category=3:12, rnorm(10))
>>
>> merge(x, y, by = "category")
>> merge(x, y, by="category", all = T)
>>
>>
>> On Wed, Jun 17, 2009 at 10:04 PM, Martin Batholdy <
>> batho...@googlemail.com> wrote:
>>
>>> I have tried to replicate the example on the help page;
>>>
>>>
>>> x <- data.frame(category = sample(3, 10, r=TRUE), rnorm(10, 5, 2))
>>> y <- data.frame(category = sample(3, 10, r=TRUE), rnorm(10, 8, 2))
>>>
>>> merge(x, y, by = "category")
>>>
>>>
>>>
>>> When I do that, I get a data.frame with 28 rows instead of 10.
>>>
>>> What am I doing wrong?
>>>
>>>
>>>
>>> Am 18.06.2009 um 02:42 schrieb David Winsemius:
>>>
>>>
>>>  Red the help page for merge:
>>>>
>>>> ?merge
>>>>
>>>>
>>>> On Jun 17, 2009, at 8:33 PM, Martin Batholdy wrote:
>>>>
>>>>  hi,
>>>>>
>>>>>
>>>>> I have two data.frames each with two columns;
>>>>>
>>>>>
>>>>> x1
>>>>>
>>>>> 1       4
>>>>> 1       3
>>>>> 1       6
>>>>> 2       9
>>>>> 2       2
>>>>> 2       5
>>>>> 3       6
>>>>> 3       7
>>>>> 3       4
>>>>>
>>>>>
>>>>> x2
>>>>>
>>>>> 1       -3
>>>>> 1       -7
>>>>> 2       -3
>>>>> 2       -2
>>>>> 2       -8
>>>>> 3       -1
>>>>> 3       -2
>>>>> 3       -1
>>>>>
>>>>> now I want to merge this data.frames to one data.frame.
>>>>>
>>>>> The problem is, that sometimes there is a different number of elements
>>>>> per category.
>>>>> (like above x1 has 3 values for the value 1 in the first row, but x2
>>>>> has only 2 values for the value 1 in the first row).
>>>>>
>>>>> Is there an easy way to merge this two data.frames by deleting the rows
>>>>> that only one data.frame "has".
>>>>> In the example, that resulting data.frame would be the data.frame x1
>>>>> and x2 except the row 3 of data.frame x1.
>>>>>
>>>>> thanks for any suggestions!
>>>>>
>>>>> ______________________________________________
>>>>> R-help@r-project.org mailing list
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide
>>>>> http://www.R-project.org/posting-guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>
>>>>
>>>> David Winsemius, MD
>>>> Heritage Laboratories
>>>> 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.
>>>
>>
>>
>>
>> --
>> Henrique Dallazuanna
>> Curitiba-Paraná-Brasil
>> 25° 25' 40" S 49° 16' 22" O
>>
>>
>>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>
>
>


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

Reply via email to