[R] subset question

jim holtman jholtman at gmail.com
Tue Aug 28 02:49:50 CEST 2007


Here is one way of checking to see if a row contains a particular
value and setting the contents of a new column:

n <- 20
# create test data
x <- data.frame(sample(letters,n),sample(letters,n),sample(letters,n),sample(letters,n))
# add a column indicating if the row contains 'a', 'b' or 'c'
x$a <- apply(x[, 1:4], 1, function(.row) any(.row %in% c('a','b','c'))) + 0


On 8/27/07, Kirsten Beyer <kirsten-beyer at uiowa.edu> wrote:
> I would like to code records in a dataset with a 1 if any of the
> columns 9-67 contain a particular code, and zero if they don't.  I've
> been working with "subset" and it seems that something like
> subset(data, data[9:67]--"12345") would work, but I have been
> unsuccessful so far.  It seems like a simple problem - any help is
> appreciated!
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list