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