replacing part of a string in a boolean rule with indices - r

i need to replace this part X[,...] in this string with something else
r <- "X[,1]<=-0.00595 & X[,2]<=-0.00605 & X[,20]>-0.00625 & X[,25]>-0.00615"
I would like to get something like this
X[,...] replase to Q
r <- "Q<=-0.00595 & Q<=-0.00605 & Q>-0.00625 & Q>-0.00615"

Here is one possible solution:
r <- "X[,1]<=-0.00595 & X[,2]<=-0.00605 & X[,20]>-0.00625 & X[,25]>-0.00615"
gsub("X\\[,\\d*\\]", "Q", r)
# "Q<=-0.00595 & Q<=-0.00605 & Q>-0.00625 & Q>-0.00615"

Related

Where are these unexpected characters appearing from in write.table?

I have three strings which I want to bind as rows and pass to the clipboard via write.table(). When I do so I find a lot of junk characters are added and I can't find out where they come from. I'm presuming this is an issue with my setup, but here's the data and code to see if this is reproducible.
strings <- c("\\textit{LTG health} & -.91 (-.91, -.90) & -.90 (-.90, -.90) & -.91 (-.91, -.90) & -.85 (-.86, -.84) & -.91 (-.92, -.91) & -.90 (-.91, -.90) & -.91 (-.91, -.90) & -.85 (-.86, -.84) \\\\",
"\\textit{LLTI} & -.85 (-.85, -.84) & -.84 (-.85, -.83) & -.85 (-.86, -.84) & -.79 (-.80, -.78) & -.84 (-.85, -.83) & -.83 (-.84, -.83) & -.83 (-.84, -.82) & -.77 (-.78, -.76) \\\\",
"\\textit{Mortality (2011)} & -.41 (-.43, -.39) & -.39 (-.42, -.37) & -.41 (-.43, -.39) & -.45 (-.47, -.43) & -.56 (-.58, -.54) & -.54 (-.56, -.52) & -.56 (-.58, -.54) & -.59 (-.61, -.57) \\\\"
)
write.table(strings, "clipboard", col.names = F, row.names = F, quote = F)
When I paste into a plain text editor I get the following full of unexpected characters and the third line appears to be lost entirely. I can't post the actual data because SO is detecting the junk characters as spam, so here's a screenshot:
EDITS
If I use paste() to concatenate into a single row, the junk characters don't appear.
Setting the fileEncoding in write.table to UTF-8 does not fix the problem.

sum multiple which statements with loop?

I'm computing a new variable in a large df in r. I wrote some code that fills my empty cells:
data2$departure_time[which(data2$No=="3" & data2$Key=="300111")]<-data2$departure_time[which(data2$No== "2" & data2$Key=="300111")] + data2$travel_time[which(data2$No=="2" & data2$Key=="300111")] + data2$waiting_time[which(data2$o=="2" & data2$Key=="300111")]
this works so far. yet i need to adapt No-statement in the code every time. What needs to be done is just add 1 to each No statement.
So the result would be:
data2$departure_time[which(data2$No=="4" & data2$Key=="300111")]<-data2$departure_time[which(data2$No== "3" & data2$Key=="300111")] + data2$travel_time[which(data2$No=="3" & data2$Key=="300111")] + data2$waiting_time[which(data2$o=="3" & data2$Key=="300111")]
And so on..
I tried to implement a for-loop into my code as I thougt I would be the easiest solution. So I tried the following:
for(in in 3:40){data2$departure_time[which(data2$No=i & data2$Key=="300111")]<-for(j in 2:40){data2$departure_time[which(data2$No= j & data2$Key=="300111")] + data2$travel_time[which(data2$No= j & data2$Key=="300111")] + data2$waiting_time[which(data2$No=j & data2$Key=="300111")]}}
Unfortunately this didn't work out. I never wrote a loop in R and don't know how to solve this otherwise.
Thank you in advance!

Expression Expected Error - string assign value

Okay, so I got to generate this string to be SHA1 hashed. I have it like this
string = "ACCEPTURL=SOMErandomSITE/betaling1.aspxblablaRANDOMNMBRAMOUNT=" & visabedrag3 & "blablaRANDOMNMBRBGCOLOR=#FFFFFFblablaRANDOMNMBRBRAND=VISAblablaRANDOMNMBRBUTTONBGCOLOR=#9BCC83blablaRANDOMNMBRBUTTONTXTCOLOR=#000000blablaRANDOMNMBRCANCELURL=SOMErandomSITE/betaling4.aspxblablaRANDOMNMBRCATALOGURL=SOMErandomSITEblablaRANDOMNMBRCN=" & Session('fac_cntnaam_tsv') Session('fac_cntnaam')Session('bdr_fac_cntnaam_tsv') Session('bdr_fac_cntnaam') & "blablaRANDOMNMBRCOM=Ordernummer " & Shop.Order.Ordernummer & "blablaRANDOMNMBRCURRENCY=EURblablaRANDOMNMBRDECLINEURL=SOMErandomSITE/betaling2.aspxblablaRANDOMNMBREMAIL=" & Session('bdr_fac_cntemail')Session('fac_cntemail') & "blablaRANDOMNMBREXCEPTIONURL=SOMErandomSITE/betaling3.aspxblablaRANDOMNMBRFONTTYPE=arialblablaRANDOMNMBRHOMEURL=SOMErandomSITELANGUAGE=nl_NLblablaRANDOMNMBRLOGO=logo.gifblablaRANDOMNMBRORDERID=" & Shop.Order.Ordernummer & "blablaRANDOMNMBROWNERADDRESS=" & Session('facadres') Session('facadres_nr')Session('bdr_facadres') Session('bdr_facadres_nr') & "blablaRANDOMNMBROWNERZIP=" & Session('facpostcode')Session('facpostcode_letters')Session('bdr_facpostcode')Session('bdr_facpostcode_letters') & "blablaRANDOMNMBRPM=creditcardblablaRANDOMNMBRPSPID=reservecornerblablaRANDOMNMBRTBLBGCOLOR=#ecececblablaRANDOMNMBRTBLTXTCOLOR=#4d4d4dblablaRANDOMNMBRTITLE=Betalen met VisablablaRANDOMNMBRTP=SOMErandomSITE/logo.gifblablaRANDOMNMBRTXTCOLOR=#000000blablaRANDOMNMBR"
But the error I get now is BC30201: Expression expected. What am I doing wrong? It's been years since I've done ASP.net
You have missed Variable Name
string sss="value";// In this line(Code) means variable declaration
where,
string means variable data type
sss means variable name
"value" means variable value
try this
string data = "ACCEPTURL=SOMErandomSITE/betaling1.aspxblablaRANDOMNMBRAMOUNT=" & visabedrag3 & "blablaRANDOMNMBRBGCOLOR=#FFFFFFblablaRANDOMNMBRBRAND=VISAblablaRANDOMNMBRBUTTONBGCOLOR=#9BCC83blablaRANDOMNMBRBUTTONTXTCOLOR=#000000blablaRANDOMNMBRCANCELURL=SOMErandomSITE/betaling4.aspxblablaRANDOMNMBRCATALOGURL=SOMErandomSITEblablaRANDOMNMBRCN=" & Session('fac_cntnaam_tsv') Session('fac_cntnaam')Session('bdr_fac_cntnaam_tsv') Session('bdr_fac_cntnaam') & "blablaRANDOMNMBRCOM=Ordernummer " & Shop.Order.Ordernummer & "blablaRANDOMNMBRCURRENCY=EURblablaRANDOMNMBRDECLINEURL=SOMErandomSITE/betaling2.aspxblablaRANDOMNMBREMAIL=" & Session('bdr_fac_cntemail')Session('fac_cntemail') & "blablaRANDOMNMBREXCEPTIONURL=SOMErandomSITE/betaling3.aspxblablaRANDOMNMBRFONTTYPE=arialblablaRANDOMNMBRHOMEURL=SOMErandomSITELANGUAGE=nl_NLblablaRANDOMNMBRLOGO=logo.gifblablaRANDOMNMBRORDERID=" & Shop.Order.Ordernummer & "blablaRANDOMNMBROWNERADDRESS=" & Session('facadres') Session('facadres_nr')Session('bdr_facadres') Session('bdr_facadres_nr') & "blablaRANDOMNMBROWNERZIP=" & Session('facpostcode')Session('facpostcode_letters')Session('bdr_facpostcode')Session('bdr_facpostcode_letters') & "blablaRANDOMNMBRPM=creditcardblablaRANDOMNMBRPSPID=reservecornerblablaRANDOMNMBRTBLBGCOLOR=#ecececblablaRANDOMNMBRTBLTXTCOLOR=#4d4d4dblablaRANDOMNMBRTITLE=Betalen met VisablablaRANDOMNMBRTP=SOMErandomSITE/logo.gifblablaRANDOMNMBRTXTCOLOR=#000000blablaRANDOMNMBR"
and also you need to concatenation between separate session .
Like & Session('bdr_fac_cntemail') & Session('fac_cntemail') & Session('bdr_fac_cntemail') & Session('fac_cntemail') & etc
Not
& Session('bdr_fac_cntemail') Session('fac_cntemail') &

Create new data set that meets all of 4 conditions

I would like to create a new dataset where the following four conditions are all met.
rowSums(is.na(UNCA[,11:23]))<12
rowSums(is.na(UNCA[,27:39]))<12
rowSums(is.na(UNCA[,40:52]))<12
rowSums(is.na(UNCA[,53:65]))<12
Thanks!
Then use the & operator:
UNCA.new <- UNCA[rowSums(is.na(UNCA[,11:23])) < 12 &
rowSums(is.na(UNCA[,27:39])) < 12 &
rowSums(is.na(UNCA[,40:52])) < 12 &
rowSums(is.na(UNCA[,53:65])) < 12, ]
A single & is a vectorized function, while a double && is unary (typically used in an if statement, for instance).

How to do the square root in PowerPoint VBA?

Here is some math code that adds A to B:
Sub math()
A = 23
B = 2
ABSumTotal = A + B
strMsg = "The answer is " & "$" & ABSumTotal & "."
MsgBox strMsg
End Sub
But how can I calculate a square root of ABSumTotal?
Is it possible in PowerPoint VBA?
Use: Sqr()
strMsg = "The answer is " & "$" & Sqr(ABSumTotal) & "."
Have you tried the function Sqr?
See:
http://msdn.microsoft.com/en-us/library/h2h9y284%28VS.85%29.aspx
You can use use ^ to compute X^(1/2)
Edit: added parenthesis

Resources