Re: [R] row-wise conditional update in dataframe

2008-01-22 Thread jiho
On 2008-January-22 , at 03:10 , Jon Erik Ween wrote: > That got me there. I suppose R prefers absolute field references in > scripts rather than macrosubstitutions of field names like you would > do in pearl or shell scripts? no, actually, the problem is that apply works on arrays/matrices[1],

Re: [R] row-wise conditional update in dataframe

2008-01-21 Thread Jon Erik Ween
Thanks Jim That got me there. I suppose R prefers absolute field references in scripts rather than macrosubstitutions of field names like you would do in pearl or shell scripts? Anyway, thanks for you help. Cheers Jon Soli Deo Gloria Jon Erik Ween, MD, MS Scientist, Kunin-Lunenfeld Appli

Re: [R] row-wise conditional update in dataframe

2008-01-21 Thread jim holtman
If you only want a subset, then use that in the function: Dataset.target <- apply(x,1,function(.row) sum(is.na(.row[3:8]))) This will put it back in column1: > x <- matrix(1,10,10) > x[sample(1:100,10)] <- NA > x[,1] <- 0 # make sure column 1 has no NAs so sums are correct > x [,1] [,2] [

Re: [R] row-wise conditional update in dataframe

2008-01-21 Thread Jon Erik Ween
Thanks Jim I see how this works. Problem is, I need to interrogate only a subset of fields. In your example, I need to put the total number of "NA" fields out of fields 3..8, excluding 1,2 9 10. Also, I don't see how the method inserts the sum into a particular field in a row. I guess you

Re: [R] row-wise conditional update in dataframe

2008-01-21 Thread jim holtman
You need to do 'is.na(x)' instead of "x == NA".. Here is a way of doing it: > x <- matrix(1,10,10) > x[sample(1:100,10)] <- NA > x [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,]111111111 1 [2,]111111 NA11

[R] row-wise conditional update in dataframe

2008-01-21 Thread Jon Erik Ween
Hi! I need to conditionally update a dataframe field based on values in other fields and can't find even how to search for this right. Sorry if this has been asked before. But, specifically, I have a 490 X 221 dataframe and need to count, by row, how many fields in Dataframe$field_a...Dataf