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

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.

Related

Place parentheses around characters separated by comma using regex in r

I'd like to add parentheses around grouped text separated by a comma using stringr. So if there is text that is separated by one or more commas, then I'd like parentheses around the text. There will always be a "=" before this type of string begins and there will either be a space or nothing (vector ends) after the string. Is there a generalized way to do this? Here's a sample problem:
Sample:
a <- data.frame(Rule = c("A=0 & B=Grp1,Grp2", "A=0 & B=Grp1,Grp3,Grp4 & C=1"))
a
Rule
1 A=0 & B=Grp1,Grp2
2 A=0 & B=Grp1,Grp3,Grp4 & C=1
Desired Output:
Rule
1 A=0 & B=(Grp1,Grp2)
2 A=0 & B=(Grp1,Grp3,Grp4) & C=1
Here is another potential solution. I have altered the example input to show that it works with multiple "Grp's" per line:
library(stringr)
a <- data.frame(Rule = c("A=0 & B=Grp1,Grp2",
"A=0 & B=Grp1,Grp3,Grp4 & C=1 & D=Grp5,Grp6"))
str_replace_all(a$Rule, "=([^, &]+,[^ $]+)", "=(\\1)")
#> [1] "A=0 & B=(Grp1,Grp2)"
#> [2] "A=0 & B=(Grp1,Grp3,Grp4) & C=1 & D=(Grp5,Grp6)"
Created on 2022-11-23 by the reprex package (v2.0.1)
Explanation:
regex = "=([^, &]+,[^ $]+)", "=(\\1)"
=( starting with an equals sign, capture a group
[^, &]+, with one or more characters that aren't ",", " ", and "&" followed by a comma
[^ $]+) followed by one or more characters that aren't " " or the end of the line ("$")
=(\\1) then replace the equals sign and add parentheses around the captured group (e.g. the Grp1,Grp2)
This should work:
Find: (([A-Za-z\d]+,)+[A-Za-z\d]+)
Replace: ($1)
Explanation:
[A-Za-z\d] is any alphanumeric character.
The inner group looks for 1 or more copies of groups of alphanum characters separated by commas. (e.g. Abcd1,Abcd2,)
The outer group then looks for the closing alphanumeric group, which doesn't have a comma after it. (e.g. Abcd3)
These are concatenated then the whole group is captured.
Last thing to do is the replacement, which is pretty self explanatory.

replacing part of a string in a boolean rule with indices

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"

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') &

Mathjax rendering issue

For an equation, mathjax renders like as shown in image. Please help me to find the problem and fix. In codecogs latex editor, this is rendered properly. In MathType equation editor also it displays well.
Actual code follows.
\begin{align}f(x) &= Sin\: [\pi ]x-Sin[\pi ^{2}]x+Cos[-\pi ^{3}]x \\\\
[\pi ] &=[3.14]=3\\\\ [\pi ^{2}]&=[9.8596]=9\\\\
[-\pi ^{3}]&=[-9.8596]=-10 \\\\
f(\frac{\pi }{6}) &= Sin(3\times \frac{\pi }{6})-Sin(9\times \frac{\pi }
{6})+Cos(-10\times \frac{\pi }{6})\\\\
&=Sin\frac{\pi }{2}-Sin\frac{3\pi }{2}+Cos(\frac{5\pi }{3})\\\\
&= 1-\left ( -1 \right )+\frac{1}{2}\\\\
& =2+\frac{1}{2}\\\\
&=\frac{5}{2} \end{align}
It seems the character [ is causing some problem with align environment and MathJax.
This seems to work for me (at least on JaxEdit and http://math.stackexchange.com):
\begin{align}
f(x) & = Sin(\pi)x - Sin(\pi^2)x + Cos(-\pi^3)*x \\
\left[\pi\right] & = \left[3.14\right] = 3 \\
\left[\pi ^{2}\right] & = \left[9.8596\right]=9 \\
\left[-\pi ^{3}\right] & = \left[-9.8596\right]=-10 \\
f(\frac{\pi }{6}) & = Sin(3\times \frac{\pi }{6})-Sin(9\times \frac{\pi}{6})+Cos(-10\times \frac{\pi }{6})\\
& = Sin\frac{\pi }{2}-Sin\frac{3\pi }{2}+Cos(\frac{5\pi }{3})\\
& = 1-\left ( -1 \right )+\frac{1}{2}\\
& = 2+\frac{1}{2}\\
& = \frac{5}{2}
\end{align}
Or use an equivalent symbole from one of the package that is installed with your MathJax solution

Resources