[R] look up and Missing
David Winsemius
dwinsemius at comcast.net
Sun Nov 8 17:39:44 CET 2009
On Nov 8, 2009, at 11:08 AM, David Winsemius wrote:
>
> On Nov 8, 2009, at 10:23 AM, Ashta wrote:
>
>> 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))
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)
>
> "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 wrote the like this but it gave me which is not correct.
>> False false false false false
>
> I could not get your code to produce this. I got:
> Error: unexpected '==' in "K<- list(if(temp$v2)=="
>
>>
>> 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?
>
> 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
>
>>
> --
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
> ______________________________________________
> 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.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list