Skip to content

look up and Missing

7 messages · Ashta, jim holtman, John Kane +3 more

#
HI  R-Users

Assume that I have a data frame 'temp' with several variables (v1,v2,v3,v4,v5.).

  v1 v2 v3  v4 v5
   1  2   3   3    6
   5  2  4    2    0
   2 -9   5   4    3
   6  2   1   3    4

1, I want to look at the entire row values of when v2 =-9
   like
         2 -9   5   4    3

I wrote
K<- list(if(temp$v2)==-9))

I wrote the like this but  it gave me  which is not correct.
   False false false false false

2. I want assign that values  as missing if   v2 = -9.  (ie., I want
exclude from the analysis

How do I do it  in R?

Thanks in advance
#
Here is how to find out which rows contain -9 and then you can do with
it as you please:
v1 v2 v3 v4 v5
1  1  2  3  3  6
2  5  2  4  2  0
3  2 -9  5  4  3
4  6  2  1  3  4
[1] 3

        
On Sun, Nov 8, 2009 at 10:23 AM, Ashta <sewashm at gmail.com> wrote:

  
    
#
On Nov 8, 2009, at 10:23 AM, Ashta wrote:

            
"if" would be the wrong R function to use. It's mostly for program  
control. And where did the "3" come from? You were working with the  
column temp$v2. Oh, you wanted a row rather than the column, "v2"? So  
how were you going to select that row? Perhaps:

K <-temp[ temp$v2 == -9, ]
K
I could not get your code to produce this. I got:
Error: unexpected '==' in "K<- list(if(temp$v2)=="
Your request is not well specified at least to my reading, because I  
could not tell if you wanted the re-assignment to occur in temp (and  
that was after I came down on the row side of the whether you wanted a  
row or column.) . The following assumes you wanted the row in question  
(created above)  modified outside of "temp".

 > is.na(K) <- K == -9
 > K
   v1 v2 v3 v4 v5
3  2 NA  5  4  3

If you had used ifelse you would have gotten close, but the data type  
would have been a list, which may not have been what you expected:

 > K <- ifelse(K==-9, NA, K)
 > K
[[1]]
[1] 2

[[2]]
[1] NA

[[3]]
[1] 5

[[4]]
[1] 4

[[5]]
[1] 3
#
I'm not quite sure I understood the second queston but does this work?

subset(temp, xx$v2==-9)

subset(temp, xx$v2!= -9)
--- On Sun, 11/8/09, Ashta <sewashm at gmail.com> wrote:

            
__________________________________________________________________
Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now
http://ca.toolbar.yahoo.com.
#
try this:

temp.new <- temp[temp$v2 != -9, ]
temp.new


I hope it helps.

Best,
Dimitris
Ashta wrote:

  
    
#
On Nov 8, 2009, at 11:08 AM, David Winsemius wrote:

            
A further thought, that might be more useful if you were intending to  
supply a portion of a dataframe to an analytical function, would be  
the subset function:

t2 <- subset(temp, v2 != -9)

E. g.:

lm( v1 ~ v2 + v3, data= subset(temp, v2 != -9)
David Winsemius, MD
Heritage Laboratories
West Hartford, CT