[R] R grep question

2021-05-26 Thread Kai Yang via R-help
Hi List,
I wrote the code to create a new variable:
CRC$MMR.gene<-ifelse(grep("MLH1"|"MSH2",CRC$gene.all,value=T),"Yes","No")
 

I need to create MMR.gene column in CRC data frame, ifgene.all column contenes 
MLH1 or MSH2, then the MMR.gene=Yes, if not,MMR.gene=No

But, the code doesn't work for me. Can anyone tell how to fix the code?

Thank you,

Kai
[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Testing optimization solvers with equality constraints

2021-05-26 Thread Abby Spurdle
I need to retract my previous post.
(Except the part that the R has extremely good numerical capabilities).

I ran some of the examples, and Hans W was correct.

__
R-help@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.


Re: [R] Decompose df1 into another df2 based on values in df1

2021-05-26 Thread Bert Gunter
Thank you for the reprex. However your specification was too vague for me
to know exactly what your data are like, so I tried to assume the most
general possibility, with the consequence that I may be giving you an
answer to the wrong question. Hopefully, you can adjust as needed to get
what you want.

I need also warn you that I am nearly certain there are more elegant,
cleverer, faster ways to do this. I just used simple tools. So you may wish
to wait a bit to see whether others can improve on my attempt.

First of all, I assumed the "a2/a3" in S5 in d1 is a typo and it should be
"a2|a3". If it is is not a typo then substitute "\\||\\/" for "\\|" in the
strsplit function in the code that follows.
Secondly, I assumed that your identifiers, "a1" for example, could occur
more than 1 time in your data. If the only possibilities are 0 or 1 times,
then the code I provided --in particular the last sapply-- is too
complicated. A faster approach in that case might be to use R's outer()
function; I leave that as an exercise for you or someone else to help you
with if so.

Here is my code for your reprex:

getall<- function(x){
   ul <-unlist(strsplit(x,"\\|"))
   ul[ul != "w"]
}
allvals <- lapply(d1, getall)
uneeks <- sort(unique(unlist(allvals)))
sapply(allvals, function(x)table(factor(x, levels = uneeks)))


## which gives
> sapply(allvals, function(x)table(factor(x, levels = uneeks)))
   S1 S2 S3 S4 S5
a1  1  0  0  0  0
a2  1  0  1  0  1
a3  0  0  0  0  1
b1  1  1  1  0  0
b3  1  0  1  0  0
b4  0  0  1  1  0
c1  0  0  1  0  0
c2  0  1  0  0  0
c4  0  0  1  1  0

Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, May 26, 2021 at 2:18 PM Adrian Johnson 
wrote:

> Hello,
>
> I am trying to convert a df (given below as d1) into df2 (given below as
> res).
>
>  I tried using loops for each row. I cannot get it right.  Moreover the df
> is 25 x 500 in dimension and I cannot get it to work.
>
> Could anyone help me here please.
>
> Thanks.
> Adrian.
>
> d1 <-
> structure(list(S1 = c("a1|a2", "b1|b3", "w"), S2 = c("w", "b1",
> "c2"), S3 = c("a2", "b3|b4|b1", "c1|c4"), S4 = c("w", "b4", "c4"
> ), S5 = c("a2/a3", "w", "w")), class = "data.frame", row.names = c("A",
> "B", "C"))
>
> res <-
> structure(list(S1 = c(1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L),
> S2 = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L), S3 = c(0L,
> 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L), S4 = c(0L, 0L, 0L, 0L,
> 0L, 0L, 1L, 0L, 0L, 1L), S5 = c(0L, 1L, 1L, 0L, 0L, 0L, 0L,
> 0L, 0L, 0L)), class = "data.frame", row.names = c("a1", "a2",
> "a3", "b1", "b2", "b3", "b4", "c1", "c2", "c4"))
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


[R] Decompose df1 into another df2 based on values in df1

2021-05-26 Thread Adrian Johnson
Hello,

I am trying to convert a df (given below as d1) into df2 (given below as
res).

 I tried using loops for each row. I cannot get it right.  Moreover the df
is 25 x 500 in dimension and I cannot get it to work.

Could anyone help me here please.

Thanks.
Adrian.

d1 <-
structure(list(S1 = c("a1|a2", "b1|b3", "w"), S2 = c("w", "b1",
"c2"), S3 = c("a2", "b3|b4|b1", "c1|c4"), S4 = c("w", "b4", "c4"
), S5 = c("a2/a3", "w", "w")), class = "data.frame", row.names = c("A",
"B", "C"))

res <-
structure(list(S1 = c(1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L),
S2 = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L), S3 = c(0L,
1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L), S4 = c(0L, 0L, 0L, 0L,
0L, 0L, 1L, 0L, 0L, 1L), S5 = c(0L, 1L, 1L, 0L, 0L, 0L, 0L,
0L, 0L, 0L)), class = "data.frame", row.names = c("a1", "a2",
"a3", "b1", "b2", "b3", "b4", "c1", "c2", "c4"))

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] [External Email] Group by and add a constant value based on a condition dply

2021-05-26 Thread Rui Barradas

Hello,

I too don't see why grouping is needed but here it goes.


df1 %>%
  group_by(Department, Class) %>%
  mutate(Value = Value + 5*(Date == "4.01.2020"))


Hope this helps,

Rui Barradas

Às 17:01 de 26/05/21, Christopher W Ryan via R-help escreveu:

Is the grouping beforehand necessary? Could you simply, "for all the dates
that are "4.01.2020"  and have the "Value" greater than zero  add 5 to the
"Value" "?  I may be missing something.

--Chris Ryan

On Wed, May 26, 2021 at 11:53 AM Elahe chalabi via R-help <
r-help@r-project.org> wrote:


Hi everyone,

I have the following dataframe:



   structure(list(Department = c("A", "A", "A", "A", "A", "A", "A",
  "A", "B", "B", "B", "B", "B", "B", "B", "B"), Class = c(1L, 1L,
 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), Value = c(0L,
 100L, 800L, 800L, 0L, 300L, 1200L, 0L, 0L, 0L, 400L, 400L, 200L,
 800L, 1200L, 0L), Date = c("1.01.2020", "2.01.2020", "3.01.2020",
 "4.01.2020", "1.01.2020", "2.01.2020", "3.01.2020", "4.01.2020",
 "1.01.2020", "2.01.2020", "3.01.2020", "4.01.2020", "1.01.2020",
 "2.01.2020", "3.01.2020", "4.01.2020")), class = "data.frame",
row.names = c(NA,
  -16L))

  using dplyr I need to group by "Depatment" and "Class" and then for all
the dates that are "4.01.2020"  and have the "Value" greater than zero  add
5 to the "Value", meaning the desired dataframe will be (NewValue column) :



structure(list(Department = c("A", "A", "A", "A", "A", "A", "A",
  "A", "B", "B", "B", "B", "B", "B", "B", "B"), Class = c(1L, 1L,
  1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), Value = c(0L,
  100L, 800L, 800L, 0L, 300L, 1200L, 0L, 0L, 0L, 400L, 400L, 200L,
  800L, 1200L, 0L), Date = c("1.01.2020", "2.01.2020", "3.01.2020",
  "4.01.2020", "1.01.2020", "2.01.2020", "3.01.2020", "4.01.2020",
  "1.01.2020", "2.01.2020", "3.01.2020", "4.01.2020", "1.01.2020",
  "2.01.2020", "3.01.2020", "4.01.2020"), NewValue = c(0L, 100L,
  800L, 805L, 0L, 300L, 1200L, 0L, 0L, 0L, 400L, 405L, 200L, 800L,
  1200L, 0L)), class = "data.frame", row.names = c(NA, -16L))


Thanks a lot for any help!
Elahe

__
R-help@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.



[[alternative HTML version deleted]]

__
R-help@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.



__
R-help@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.


Re: [R] [External Email] Group by and add a constant value based on a condition dply

2021-05-26 Thread Christopher W Ryan via R-help
Is the grouping beforehand necessary? Could you simply, "for all the dates
that are "4.01.2020"  and have the "Value" greater than zero  add 5 to the
"Value" "?  I may be missing something.

--Chris Ryan

On Wed, May 26, 2021 at 11:53 AM Elahe chalabi via R-help <
r-help@r-project.org> wrote:

> Hi everyone,
>
> I have the following dataframe:
>
>
>
>   structure(list(Department = c("A", "A", "A", "A", "A", "A", "A",
>  "A", "B", "B", "B", "B", "B", "B", "B", "B"), Class = c(1L, 1L,
> 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), Value = c(0L,
> 100L, 800L, 800L, 0L, 300L, 1200L, 0L, 0L, 0L, 400L, 400L, 200L,
> 800L, 1200L, 0L), Date = c("1.01.2020", "2.01.2020", "3.01.2020",
> "4.01.2020", "1.01.2020", "2.01.2020", "3.01.2020", "4.01.2020",
> "1.01.2020", "2.01.2020", "3.01.2020", "4.01.2020", "1.01.2020",
> "2.01.2020", "3.01.2020", "4.01.2020")), class = "data.frame",
> row.names = c(NA,
>  -16L))
>
>  using dplyr I need to group by "Depatment" and "Class" and then for all
> the dates that are "4.01.2020"  and have the "Value" greater than zero  add
> 5 to the "Value", meaning the desired dataframe will be (NewValue column) :
>
>
>
>structure(list(Department = c("A", "A", "A", "A", "A", "A", "A",
>  "A", "B", "B", "B", "B", "B", "B", "B", "B"), Class = c(1L, 1L,
>  1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), Value = c(0L,
>  100L, 800L, 800L, 0L, 300L, 1200L, 0L, 0L, 0L, 400L, 400L, 200L,
>  800L, 1200L, 0L), Date = c("1.01.2020", "2.01.2020", "3.01.2020",
>  "4.01.2020", "1.01.2020", "2.01.2020", "3.01.2020", "4.01.2020",
>  "1.01.2020", "2.01.2020", "3.01.2020", "4.01.2020", "1.01.2020",
>  "2.01.2020", "3.01.2020", "4.01.2020"), NewValue = c(0L, 100L,
>  800L, 805L, 0L, 300L, 1200L, 0L, 0L, 0L, 400L, 405L, 200L, 800L,
>  1200L, 0L)), class = "data.frame", row.names = c(NA, -16L))
>
>
> Thanks a lot for any help!
> Elahe
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


[R] Group by and add a constant value based on a condition dply

2021-05-26 Thread Elahe chalabi via R-help
Hi everyone,

I have the following dataframe: 



      structure(list(Department = c("A", "A", "A", "A", "A", "A", "A", 
     "A", "B", "B", "B", "B", "B", "B", "B", "B"), Class = c(1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), Value = c(0L, 
    100L, 800L, 800L, 0L, 300L, 1200L, 0L, 0L, 0L, 400L, 400L, 200L, 
    800L, 1200L, 0L), Date = c("1.01.2020", "2.01.2020", "3.01.2020", 
    "4.01.2020", "1.01.2020", "2.01.2020", "3.01.2020", "4.01.2020", 
    "1.01.2020", "2.01.2020", "3.01.2020", "4.01.2020", "1.01.2020", 
    "2.01.2020", "3.01.2020", "4.01.2020")), class = "data.frame", row.names = 
c(NA, 
     -16L))

 using dplyr I need to group by "Depatment" and "Class" and then for all the 
dates that are "4.01.2020"  and have the "Value" greater than zero  add 5 to 
the "Value", meaning the desired dataframe will be (NewValue column) : 



   structure(list(Department = c("A", "A", "A", "A", "A", "A", "A", 
 "A", "B", "B", "B", "B", "B", "B", "B", "B"), Class = c(1L, 1L, 
 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), Value = c(0L, 
 100L, 800L, 800L, 0L, 300L, 1200L, 0L, 0L, 0L, 400L, 400L, 200L, 
 800L, 1200L, 0L), Date = c("1.01.2020", "2.01.2020", "3.01.2020", 
 "4.01.2020", "1.01.2020", "2.01.2020", "3.01.2020", "4.01.2020", 
 "1.01.2020", "2.01.2020", "3.01.2020", "4.01.2020", "1.01.2020", 
 "2.01.2020", "3.01.2020", "4.01.2020"), NewValue = c(0L, 100L, 
 800L, 805L, 0L, 300L, 1200L, 0L, 0L, 0L, 400L, 405L, 200L, 800L, 
 1200L, 0L)), class = "data.frame", row.names = c(NA, -16L))

   
Thanks a lot for any help!
Elahe

__
R-help@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.