Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Expand a sequence such that the value of any member of the sequence becomes its position and unfilled positions are coded as 0 or NA

Tags:

r

The problem:

I have a dataset in which raters have listed components they believe are noise rather than signal. So, if there were eight components total, they might have generated a list like "3,5,6," indicating that they think the third, fifth, and sixth components are noise and should be excluded from further analysis.

To facilitate my analysis of interrater reliability, I need to be able to line up vectors created by different raters and see where there is agreement; so, while "3,5,6" and "3,6" are not comparable, something like "0,0,3,0,5,6,0,0" and "0,0,3,0,0,6,0,0" would be, especially once I have converted all nonzero values to ones.

I would like to know if there is a way to expand a sequence from a specified minimum to a specified maximum value, replacing missing values with "0" or "NA," such that given:

xmin <- 1
xmax <- 8
x <- c(3,5,6)

I could produce something like:

expand.x <- c(NA,NA,3,NA,5,6,NA,NA)

The sequence command has some similarities to what I want but is not quite there.

For my purposes, it is not necessary to actually retain the values in the original vector, so

expand.x <- c(0,0,1,0,1,1,0,0)

or

expand.x <- c(FALSE,FALSE,TRUE,FALSE,TRUE,TRUE,FALSE,FALSE)

would work just as well for me, because in this format the position data recapitulates the original numeric values in the list. I feel like this should be simple, but I'm not sure even where to start on tackling this. I suspect that some way of comparing the given vector c(3,5,6) against the full sequence seq(xmin:xmax) would be key but...no ideas. Thanks!

like image 995
A Jack Avatar asked Dec 05 '13 21:12

A Jack


2 Answers

1) Try this:

> xmin:xmax %in% x
[1] FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE

2) or this

> x[ match(xmin:xmax, x) ]
[1] NA NA  3 NA  5  6 NA NA

3) or this

> replace(xmin:xmax, -x, 0)
[1] 0 0 3 0 5 6 0 0

If we replace 0 with NA then this solution gives the same result as (2).

EDIT: Added second solution.

like image 152
G. Grothendieck Avatar answered Nov 15 '22 10:11

G. Grothendieck


You can just make a vector of NAs and then use [] brackets to overwrite the values you're interested in.

> xmin = 1
> xmax = 8
> x = c(3,5,6)

> expand.x = xmin:xmax * NA
> expand.x[x] = x
> expand.x
[1] NA NA  3 NA  5  6 NA NA
like image 20
Nick Avatar answered Nov 15 '22 10:11

Nick