On Jun 8, 2013, at 4:09 AM, Bert Jacobs wrote: > Hi, > > I have a vector that looks like this: > RowSel <-c(0,1,0,1,2,3,0,5,5) > > Now I want to select rows from a specific dataframe DF based on that vector > like this: > SubDF <- DF[RowSel,] > > So this works fine, but I was wondering how I could add blank rows add the > locations in the vector where there is a zero: > So the final dataframe should look like this: > > SubDF > [1] blank row > [2] row 1 > [3] blank row > [4] row 1 > [5] row 2 > [6] row 3 > [7] blank row > [8] row 5 > [9] row 5 > > Do I have to use a loop for this or does there exist a straight forward > function option.
NA's in the selection vector will retrun an NA row, so convert those zeros to NA's : > str(df) 'data.frame': 9 obs. of 2 variables: $ A: num 0 3 9 0 2 0 1 0 1 $ B: int 1 1 1 2 2 3 3 4 4 > RowSel <-c(0,1,0,1,2,3,0,5,5) > df[RowSel,] A B 1 0 1 1.1 0 1 5 3 1 9 9 1 6 2 2 6.1 2 2 > is.na(RowSel) <- RowSel==0 > df[RowSel,] A B NA NA NA 1 0 1 NA.1 NA NA 1.1 0 1 5 3 1 9 9 1 NA.2 NA NA 6 2 2 6.1 2 2 -- 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.