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:> xv1 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> which(apply(x, 1, function(.row) any(.row == -9)))[1] 3> >On Sun, Nov 8, 2009 at 10:23 AM, Ashta <sewashm at gmail.com> 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)) > > 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 > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
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))"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 falseI 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
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:> From: Ashta <sewashm at gmail.com> > Subject: [R] look up and Missing > To: r-help at stat.math.ethz.ch > Received: Sunday, November 8, 2009, 10:23 AM > 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 > > ______________________________________________ > 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. >__________________________________________________________________ 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:> 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 > > ______________________________________________ > 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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Dear Ashta, Is this what you want? x <- read.table(textConnection("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"), header = TRUE) closeAllConnections() x # Option 1 x1 <- x # copy of x just for this example x1$v2[which(x1$v2 == -9)] <- NA x1 # Option 2 x2 <- x # copy of x just for this example x2$v2 <- with(x2, ifelse(v2 == -9, NA, v2)) x2 HTH, Jorge On Sun, 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)) > > 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 > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]