[R] refer to next line within a data-frame an select cases
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Dec 16 05:27:13 CET 2008
grep for "pos" or "neg" reducing the data frame to
the rows prior to those matched. With this reduced
data frame pick out those row2 for which "a" is in row1:
# input
DF <- data.frame(row1 = c("a", "pos", "a", "neg", "a", "neg", "a", "pos"),
row2 = c(12, NA, 3, NA, 5, NA, 11, NA), stringsAsFactors = FALSE)
# soln 1. get pos and neg
pos <- with(DF[grep("pos", DF$row1)-1,], row2[row1 == "a"])
neg <- with(DF[grep("neg", DF$row1)-1,], row2[row1 == "a"])
An alternative is available if we can assume that rows with
pos or neg in row1 always have NA in row2. In that case
na.locf from zoo can fill in the NAs with the prior value:
# soln 2
library(zoo)
pos <- with(DF, na.locf(row2)[row1 == "pos"])
neg <- with(DF, na.locf(row2)[row1 == "neg"])
# or this which creates variables neg, pos and a
# so we ignore the a:
# soln 3 (also requires na.locf from zoo):
attach(split(na.locf(DF$row2), DF$row1))
On Mon, Dec 15, 2008 at 10:35 PM, Jörg Groß <joerg at licht-malerei.de> wrote:
> Hi,
>
> I have a problem sorting and selecting entries within a data-frame and I
> don't know if it is possible to solve it with R ... (probably yes, but I
> have no idea how).
>
>
> Following Data;
>
>
> row1 row2
> a 12
> pos NA
> a 3
> neg NA
> a 5
> neg NA
> a 11
> pos NA
>
>
> I want to extract the values in row 2 in the lines with an "a" in row1.
>
> But I want to have two vectors: vector x with all a-values (in row2) when
> the label in the next line is "pos"
> and vector y with all a-values when the label in the next line is "neg".
>
>
> So:
>
> x = 12, 11
> y = 4, 5
>
>
> How can I do that? How can I refer to values of the next line?
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list