Re: [R] Subtracting rows by id

From: Sara Maxwell <smaxwell_at_ucsc.edu>
Date: Wed, 25 May 2011 15:16:10 -0700

That worked perfectly. Thank you Dennis - I very much appreciate the help!

Sara Maxwell, PhD, Postdoctoral Fellow
Marine Conservation Institute
University of California Santa Cruz
Long Marine Laboratory
100 Shaffer Road
Santa Cruz CA 95060 USA
+1 206 355 3249
Sara.Maxwell_at_Marine-Conservation.org
www.Marine-Conservation.org

On May 25, 2011, at 12:58 PM, Dennis Murphy wrote:

> Hi:
>
> Interesting problem. Here's one approach:
>
> library(plyr)
> # Read in your datasets as data frames rather than matrices
> dataset1 <- data.frame(id1 = rep(1:3, each = 10),
> value1 = sample(seq_len(100), 30, replace =
> TRUE))
> dataset2 <- data.frame(id2 = 1:3, subtract.value = c(1, 3, 5))
>
> # The idea is to use the rows of dataset2 as parameters for
> # subsetting and removing the first n_i rows. The tail() function
> # serves the purpose:
> foo <- function(id2, subtract.value) tail(subset(dataset1, id1 ==
> id2), -subtract.value)
>
> # Use the mdply function in the plyr package:
>> mdply(dataset2, foo)[, -(1:2)]
> id1 value1
> 1 1 2
> 2 1 55
> 3 1 18
> 4 1 4
> 5 1 3
> 6 1 76
> 7 1 74
> 8 1 21
> 9 1 97
> 10 2 19
> 11 2 49
> 12 2 20
> 13 2 73
> 14 2 79
> 15 2 95
> 16 2 52
> 17 3 60
> 18 3 58
> 19 3 68
> 20 3 59
> 21 3 13
>
>
> HTH,
> Dennis
>
> On Wed, May 25, 2011 at 9:55 AM, Sara Maxwell <smaxwell_at_ucsc.edu>
> wrote:
>> Dear R users,
>>
>> I have two datasets:
>>
>> id1 <- c(rep(1,10), rep(2,10), rep(3,10))
>> value1 <- sample(1:100, 30, replace=TRUE)
>> dataset1 <- cbind(id1,value1)
>>
>> id2 <- c(1,2,3)
>> subtract.value <- c(1,3,5)
>> dataset2 <- cbind(id2, subtract.value)
>>
>> I want to subtract the number of rows in the subtract.value that
>> corresponds to the id value in dataset1. So for the 1 in id1, I want
>> to remove the first row, for 2 in id1 I want to remove the first 3
>> rows, for 3 in id1 I want to remove the first 5 rows, finally
>> creating
>> a new dataframe with the remaining values.
>>
>> I am having trouble structuring a loop that can do this by the unique
>> ids in the first dataset while matching the ids in the datasets.
>>
>> Any thoughts would be greatly appreciated.
>>
>> Thank you,
>> Sara
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help_at_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_at_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. Received on Wed 25 May 2011 - 22:28:21 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 25 May 2011 - 23:30:09 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive