How to implement a for loop when multiple dataframes are involved? - r

I am currently working in R and this is a samople of a task i was assigned:
POINA[1] <-sum(as.numeric(ifelse( (datos1$c_res==1 | datos1$c_res==3) & (datos1$r_def==0) & (datos1$eda>=15 & datos1$eda<= 98) & datos1$emp_ppal==1 & datos1$ambito1 != 1 ,1,0)))
POINA[2] <-sum(as.numeric(ifelse( (datos2$c_res==1 | datos2$c_res==3) & (datos2$r_def==0) & (datos2$eda>=15 & datos2$eda<= 98) & datos2$emp_ppal==1 & datos2$ambito1 != 1 ,1,0)))
POINA[3] <-sum(as.numeric(ifelse( (datos3$c_res==1 | datos3$c_res==3) & (datos3$r_def==0) & (datos3$eda>=15 & datos3$eda<= 98) & datos3$emp_ppal==1 & datos3$ambito1 != 1 ,1,0)))
POINA[4] <-sum(as.numeric(ifelse( (datos4$c_res==1 | datos4$c_res==3) & (datos4$r_def==0) & (datos4$eda>=15 & datos4$eda<= 98) & datos4$emp_ppal==1 & datos4$ambito1 != 1 ,1,0)))
POINA[5] <-sum(as.numeric(ifelse( (datos5$c_res==1 | datos5$c_res==3) & (datos5$r_def==0) & (datos5$eda>=15 & datos5$eda<= 98) & datos5$emp_ppal==1 & datos5$ambito1 != 1 ,1,0)))
POINA[6] <-sum(as.numeric(ifelse( (datos6$c_res==1 | datos6$c_res==3) & (datos6$r_def==0) & (datos6$eda>=15 & datos6$eda<= 98) & datos6$emp_ppal==1 & datos6$ambito1 != 1 ,1,0)))
POINA[7] <-sum(as.numeric(ifelse( (datos7$c_res==1 | datos7$c_res==3) & (datos7$r_def==0) & (datos7$eda>=15 & datos7$eda<= 98) & datos7$emp_ppal==1 & datos7$ambito1 != 1 ,1,0)))
POINA[8] <-sum(as.numeric(ifelse( (datos8$c_res==1 | datos8$c_res==3) & (datos8$r_def==0) & (datos8$eda>=15 & datos8$eda<= 98) & datos8$emp_ppal==1 & datos8$ambito1 != 1 ,1,0)))
POINA[9] <-sum(as.numeric(ifelse( (datos9$c_res==1 | datos9$c_res==3) & (datos9$r_def==0) & (datos9$eda>=15 & datos9$eda<= 98) & datos9$emp_ppal==1 & datos9$ambito1 != 1 ,1,0)))
POINA[10] <-sum(as.numeric(ifelse( (datos10$c_res==1 | datos10$c_res==3) & (datos10$r_def==0) & (datos10$eda>=15 & datos10$eda<= 98) & datos10$emp_ppal==1 & datos10$ambito1 != 1 ,1,0)))
I have several dataframes, that for sake of simplicity, are named "datos1".. "datos120". This dataframes are the results of telephone polls. Each dataframe contains different individuals and each poll corresponds to a specific week in the year.
POINA[i] is a numeric vector where each entry is the total sum of surveyed people who fit the criterion specified above.
As can be seen, the criteria remains the same every week but, since each week is a diferent frame datos[i] changes for every POINA[i].
Is there a way such that i dont have to write the 120 weeks one by one?
I have tried doing it manually but there are just to many cases so, any help in making this more efficient would be deeply apreciated

Related

Equation in a table in R

I want a table like this and make it as a kable so I can make footnote:
but I didn't manage so I made a table latex like this :
$$
\begin{array}{lccccc}
\hline
{Copula} & {Distribution} & {Parameter range} & {Complete dependence} & {Independence} \\
\hline
{Normal} & {C_{Normal} (u_1,u_2,\rho)=\Phi_{\rho}(\Phi^{-1}(u_1),\Phi^{-1}(u_2))} & {\rho\in(-1,1)} & {\rho=1,or -1} & {\rho=0} \\
\hline
{Student-t} & {C_t (u_1,u_2;\rho,d)=t_{\rho,d}(t^{-1}_d(u_1),t^{-1}_d(u_2))} & {\rho\in(-1,1)} & {\rho=1,or -1} & {\rho=0} \\
\hline
{Gumbel} & {C_{Gumbel} (u_1,u_2,\beta)=exp\{-[(-ln(u_1))^{\frac{1}{\beta}}+(-ln(u_2))^{\frac{1}{\beta}}]^{\beta}\}} & {\beta\in(0,1)} & {\beta=0} & {\beta=1} \\
\hline
{RG} & {C_{RG} (u_1,u_2,\alpha)=u_1+u_2-1+C_{Gumbel} (1-u_1,1-u_2,\alpha)} & {\alpha\in[1,\infty)} & {\alpha\rightarrow\infty} & {\alpha=1} \\
\hline
{Clayton} & {C_{Clayton} (u_1,u_2,\theta)=max((u_1^{-\theta}+u_2^{-\theta}-1)^{-\frac{1}{\theta}},0)} & {\theta\in[-1,\infty)\{0\}} & {\theta\rightarrow\infty} & {\theta \rightarrow 0} \\
\hline
{RC} & {C_{RC} (u_1,u_2,\alpha)=u_1+u_2-1+C_{Clayton} (1-u_1,1-u_2,\theta)} & {\theta\in[-1,\infty)\{0\}} & {\theta\rightarrow\infty} & {\theta \rightarrow 0} \\
\hline
\end{array}
$$
and it looks like this:
It's not bad until I found it's not easy to add footnotes since I used to make kable.
Is there any way I can add a footnote for it? or how can I convert it into a table so I can use kable to display it?

Exporting Summary Data to CSV in R

Hello everyone I am working on a script that I would like to export to a CSV file.
Everything is working well with the exception that I would like to add column names and headers for the below data.
For instance variable A is the summary data of fixed income trades in 2017. I would like Row 1 in the output file to read as such.
Any help would be greatly appreciated. My code is written below. Thanks in advance!!
#SENDS THE RESULTS TO FILE CALLED OUTFILE.TXT WHICH IS OVERWRITTEN EACH TIME SCRIPT IS RUN
sink("outfile.csv")
#SHORT-TERM PRE-REFUNDED TRADE DATA
A = MSRB[which(MSRB$Coupon.Rate >= 2 & MSRB$Year == 2017 & MSRB$Par.Traded >=500 & MSRB$Class == "PRE-REFUNDED"),]
B = MSRB[which(MSRB$Coupon.Rate >= 2 & MSRB$Year == 2017 & MSRB$Par.Traded >=1000 & MSRB$Class == "PRE-REFUNDED"),]
C = MSRB[which(MSRB$Coupon.Rate >= 2 & MSRB$Year == 2018 & MSRB$Par.Traded >=500 & MSRB$Class == "PRE-REFUNDED"),]
D = MSRB[which(MSRB$Coupon.Rate >= 2 & MSRB$Year == 2018 & MSRB$Par.Traded >=1000 & MSRB$Class == "PRE-REFUNDED"),]
E = MSRB[which(MSRB$Coupon.Rate >= 2 & MSRB$Year == 2019 & MSRB$Par.Traded >=500 & MSRB$Class == "PRE-REFUNDED"),]
F = MSRB[which(MSRB$Coupon.Rate >= 2 & MSRB$Year == 2019 & MSRB$Par.Traded >=1000 & MSRB$Class == "PRE-REFUNDED"),]
#SUMMARY OF PRE-REFUNDED DATA
summary(A$Yield)
summary(B$Yield)
summary(C$Yield)
summary(D$Yield)
summary(E$Yield)
summary(F$Yield)
#END OF OUTPUT FILE
sink()

How to add a horizontal line in tables::tabular() output?

I would like to add a horizontal line (using Hline()) in a moderately complex tables::tabular() output, when there are two factors on one side of the table.
Consider this:
require(tables)
require(car)
latex(
tabular( (Factor(partner.status, "Status") + 1)
* (Factor(fcategory, "Authoritarianism") + 1)
* ((Pct=Percent()))*Format(digits=1)
~ 1, data=Moore)
)
Which will yield:
\begin{tabular}{lllc}
\hline
Status & Authoritarianism & & \multicolumn{1}{c}{All} \\
\hline
high & high & Pct & $\phantom{0}16$ \\
& low & Pct & $\phantom{0}11$ \\
& medium & Pct & $\phantom{0}24$ \\
& All & Pct & $\phantom{0}51$ \\
low & high & Pct & $\phantom{0}18$ \\
& low & Pct & $\phantom{0}22$ \\
& medium & Pct & $\phantom{00}9$ \\
& All & Pct & $\phantom{0}49$ \\
All & high & Pct & $\phantom{0}33$ \\
& low & Pct & $\phantom{0}33$ \\
& medium & Pct & $\phantom{0}33$ \\
& All & Pct & $100$ \\
\hline
\end{tabular}
Which compiles (with the help of \usepackage{booktabs}) to:
In the output above, I would like to place a horizontal line after the 1st and the 2nd All fields from the Authoritarianism column (that is, a line before the low row and another line before the All row). Ho can I do that?
I tried to use Hline() as follows, but the resulting code isn't compilable by LaTeX:
latex(
tabular( (Factor(partner.status, "Status") + 1)
* (Factor(fcategory, "Authoritarianism") + 1 + Hline())
* ((Pct=Percent()))*Format(digits=1)
~ 1, data=Moore)
)

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

R: recommendation on how to compute new columns on multiple condition of others for every row in data.frame

For every entry in rows i need to compute two variables as new columns in a data.frame depending conditional on more than 60 other columns. I would like your recommendation on how to realize that elegant (while and for, with, ifelse, foreach, by or ddply?). I don't like to do that manually like i did for the first cases in the example code and i don't care for performance.
Further: Probably i would not need to ask if i would have understood how to use functions like transform (with ddply or by) and what they do. Thus i hope you can recommend good tutorials on that, maybe relating to my case. I found a lot but in different context and was not able to comprehend it entrily or transcribe it for my case.
My case: I have three columns for each of 20 events representing the kind and date of that event. For each row I need to compute (and save to that data.frame) the difference in time between one special event (depending on whether a special kind happened before or after another) and a date fixed for every entry in rows. Furthermore i need to save the date of that event.
This is how i did (it works, but it is running only through the first cases):
#event.2 (1. event month), event.3 (1. event year), event.4 (1. event kind), event.5 (2. event month), event.6 (2. event year), ...
df$dit[(!is.na(df$event.2) & !is.na(df$event.3) & !is.na(df$event.4) & !is.na(df$event.5) & !is.na(df$event.6) & !is.na(df$event.7))
& (
(df$event.4 == 3 & ((1/12*df$event.2)+df$event.3) > df$fixdate) & (df$event.7 == 1 | df$event.7 == 2)
)] = ((1/12*df$event.2)+df$event.3) - df$fixdate
df$date[(!is.na(df$event.2) & !is.na(df$event.3) & !is.na(df$event.4) & !is.na(df$event.5) & !is.na(df$event.6) & !is.na(df$event.7))
& (
(df$event.4 == 3 & ((1/12*df$event.2)+df$event.3) > df$fixdate) & (df$event.7 == 1 | df$event.7 == 2)
)] = ((1/12*df$event.2)+df$event.3)
df$dit[(!is.na(df$event.2) & !is.na(df$event.3) & !is.na(df$event.4) & !is.na(df$event.5) & !is.na(df$event.6) & !is.na(df$event.7))
& (
(df$event.4 == 1 & ((1/12*df$event.2)+df$event.3) > df$fixdate)
| (df$event.4 == 2 & ((1/12*df$event.2)+df$event.3) > df$fixdate)
)] = 0
df$date[(!is.na(df$event.2) & !is.na(df$event.3) & !is.na(df$event.4) & !is.na(df$event.5) & !is.na(df$event.6) & !is.na(df$event.7))
& (
(df$event.4 == 1 & ((1/12*df$event.2)+df$event.3) > df$fixdate)
| (df$event.4 == 2 & ((1/12*df$event.2)+df$event.3) > df$fixdate)
)] = df$fixdate
df$dit[(!is.na(df$event.2) & !is.na(df$event.3) & !is.na(df$event.4) & !is.na(df$event.5) & !is.na(df$event.6) & !is.na(df$event.7))
& (
(
(df$event.4 == 1 & ((1/12*df$event.2)+df$event.3) < df$fixdate)
& (
(df$event.7 == 1 & ((1/12*df$event.5)+df$event.6) > df$fixdate)
| (df$event.7 == 2 & ((1/12*df$event.5)+df$event.6) > df$fixdate)
)
)
|
(
(df$event.4 == 2 & ((1/12*df$event.2)+df$event.3) < df$fixdate)
& (
(df$event.7 == 1 & ((1/12*df$event.5)+df$event.6) > df$fixdate)
| (df$event.7 == 2 & ((1/12*df$event.5)+df$event.6) > df$fixdate)
)
)
)] = ((1/12*df$event.5)+df$event.6) - df$fixdate
df$date[(!is.na(df$event.2) & !is.na(df$event.3) & !is.na(df$event.4) & !is.na(df$event.5) & !is.na(df$event.6) & !is.na(df$event.7))
& (
(
(df$event.4 == 1 & ((1/12*df$event.2)+df$event.3) < df$fixdate)
& (
(df$event.7 == 1 & ((1/12*df$event.5)+df$event.6) > df$fixdate)
| (df$event.7 == 2 & ((1/12*df$event.5)+df$event.6) > df$fixdate)
)
)
|
(
(df$event.4 == 2 & ((1/12*df$event.2)+df$event.3) < df$fixdate)
& (
(df$event.7 == 1 & ((1/12*df$event.5)+df$event.6) > df$fixdate)
| (df$event.7 == 2 & ((1/12*df$event.5)+df$event.6) > df$fixdate)
)
)
)] = ((1/12*df$event.5)+df$event.6)
You can define your conditions as expressions and use them within transform. The idea is to factorize your conditions at most as possible .
COND1 <- expression(!is.na(event.2) & !is.na(event.3) &
!is.na(event.4) & !is.na(event.5) &
!is.na(event.6) & !is.na(event.7))
COND2 <- expression(event.4 == 3 & ((1/12*event.2)+event.3) > fixdate) &
(event.7 == 1 | event.7 == 2))
COND3 <- expression(event.4 == 1 & ((1/12*event.2)+event.3) > fixdate)
COND4 <- expression(event.4 == 2 & ((1/12*event.2)+event.3) > fixdate)
### you continue here with the rest of conditions....
Then using them within transform you can do something like:
transform(df, date = ifelse(eval(COND1) & eval(COND2),((1/12*event.2)+event.3),NA),
transform(df, date = ifelse(eval(COND1) & (eval(COND3)|eval(COND4)),fixdate,NA))
## Note also that the seond "dit" variable is deduced from "date"
transform(df,dit=date-fixdate)

Resources