Hello List,I have a data frame which having the character columns: | a1 | b1 | c1 | d1 | | a2 | b2 | c2 | d2 | | a3 | b3 | c3 | d3 | | a4 | b4 | c4 | d4 | | a5 | b5 | c5 | d5 | I need to do: if a1 not = "Positive" and not = "VUS" then values of? b1, c1 and d1 will be zero out. And do the same thing for the a2 to a5 series. I write the code below to do this. But it doesn't work. Would you please correct my code? Thank you, Kai for (i in 1:5)? { ? if (isTRUE(try$a[i] != "Positive" && try$a[i] != "VUS")) ? { ? ? try$b[i]== '' ? ? try$c[i] == '' ? ? try$d[i]== '' ? } } [[alternative HTML version deleted]]
Can you make R code that creates an actual sample data frame that looks like you want the answer to look? say, just using the data.frame function and literal strings. Oh, and read the Posting Guide... you need to send your email using plain text format or it may get garbled when the list strips the HTML formatting. On May 30, 2021 9:28:52 AM PDT, Kai Yang via R-help <r-help at r-project.org> wrote:>Hello List,I have a data frame which having the character columns: > >| a1 | b1 | c1 | d1 | >| a2 | b2 | c2 | d2 | >| a3 | b3 | c3 | d3 | >| a4 | b4 | c4 | d4 | >| a5 | b5 | c5 | d5 | > > > >I need to do: if a1 not = "Positive" and not = "VUS" then values of? >b1, c1 and d1 will be zero out. And do the same thing for the a2 to a5 >series. >I write the code below to do this. But it doesn't work. Would you >please correct my code? >Thank you, >Kai > > >for (i in 1:5)? >{ >? if (isTRUE(try$a[i] != "Positive" && try$a[i] != "VUS")) >? { >? ? try$b[i]== '' >? ? try$c[i] == '' >? ? try$d[i]== '' >? } >} > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.-- Sent from my phone. Please excuse my brevity.
Kai, You have made a simple mistake. And now you cannot see it. I believe this is not uncommon among programmers. It has happened to me more times than I want to recall.> On May 30, 2021, at 9:28 AM, Kai Yang via R-help <r-help at r-project.org> wrote: > > Hello List,I have a data frame which having the character columns: > > | a1 | b1 | c1 | d1 | > | a2 | b2 | c2 | d2 | > | a3 | b3 | c3 | d3 | > | a4 | b4 | c4 | d4 | > | a5 | b5 | c5 | d5 | > > > > I need to do: if a1 not = "Positive" and not = "VUS" then values of b1, c1 and d1 will be zero out. And do the same thing for the a2 to a5 series. > I write the code below to do this. But it doesn't work. Would you please correct my code? > Thank you, > Kai > > > for (i in 1:5) > { > if (isTRUE(try$a[i] != "Positive" && try$a[i] != "VUS")) > { > try$b[i]== ''What is the above line intended to do? I think you need to read the line out loud stating the effect of each operator. If you still do not see what gives, copy and paste each line to the R prompt after the loop has run (i.e. with `i' having value 5) paying attention to any results that are printed out.> try$c[i] == '' > try$d[i]== '' > } > } > > > [[alternative HTML version deleted]] >HTH, Chuck p.s. It is highly recommended to use `<-' as the assignment operator.
Hello, You don't need a loop, the R way is a vectorized solution and it's also clearer. Create a logical index (note only one &) and assign b, c, d where it's TRUE. i <- try$a != "Positive" & try$a != "VUS" try <- within(try, { b[i] <- '' c[i] <- '' d[i] <- '' }) Hope this helps, Rui Barradas ?s 17:28 de 30/05/21, Kai Yang via R-help escreveu:> Hello List,I have a data frame which having the character columns: > > | a1 | b1 | c1 | d1 | > | a2 | b2 | c2 | d2 | > | a3 | b3 | c3 | d3 | > | a4 | b4 | c4 | d4 | > | a5 | b5 | c5 | d5 | > > > > I need to do: if a1 not = "Positive" and not = "VUS" then values of? b1, c1 and d1 will be zero out. And do the same thing for the a2 to a5 series. > I write the code below to do this. But it doesn't work. Would you please correct my code? > Thank you, > Kai > > > for (i in 1:5) > { > ? if (isTRUE(try$a[i] != "Positive" && try$a[i] != "VUS")) > ? { > ? ? try$b[i]== '' > ? ? try$c[i] == '' > ? ? try$d[i]== '' > ? } > } > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >
Hi Kai, You seem to be asking the same question again and again. This does not give us the warm feeling that you know what you want. testdf<-data.frame(a=c("Negative","Positive","Neutral","Random","VUS"), b=c("No","Yes","No","Maybe","Yes"), c=c("Off","On","Off","Off","On"), d=c("Bad","Good","Bad","Bad","Good"), stringsAsFactors=FALSE) testdf match_strings<-c("Positive","VUS") testdf$b<-ifelse(testdf$a %in% match_strings,testdf$b,"") testdf$c<-ifelse(testdf$a %in% match_strings,testdf$c,"") testdf$d<-ifelse(testdf$a %in% match_strings,testdf$d,"") testdf I have assumed that you mean "zero length strings" rather than "zeros". Also note that your initial code was producing logical values that were never assigned to anything. Jim On Mon, May 31, 2021 at 2:29 AM Kai Yang via R-help <r-help at r-project.org> wrote:> > Hello List,I have a data frame which having the character columns: > > | a1 | b1 | c1 | d1 | > | a2 | b2 | c2 | d2 | > | a3 | b3 | c3 | d3 | > | a4 | b4 | c4 | d4 | > | a5 | b5 | c5 | d5 | > > > > I need to do: if a1 not = "Positive" and not = "VUS" then values of b1, c1 and d1 will be zero out. And do the same thing for the a2 to a5 series. > I write the code below to do this. But it doesn't work. Would you please correct my code? > Thank you, > Kai > > > for (i in 1:5) > { > if (isTRUE(try$a[i] != "Positive" && try$a[i] != "VUS")) > { > try$b[i]== '' > try$c[i] == '' > try$d[i]== '' > } > } > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.