I am trying to name the rows in a matrix, but it adds the prefixes 'X.', 'X..' etc. in front of these names. Also, the row names don't come out properly. For example, the first-row name is supposed to be 'e subscript (t+1)' but it shows something else. The even-numbered row names should be vacant, but they are given names. Could you help please?
Please see the dataset here.
Below is the code I used:
rownames(table2a)=paste(c("$e_t+1$"," ","$r_t+1$", " ","$\\Delta y_{n,t+1}$"," ",
"$s_{n,t+1}$"," ", "$d_t+1-p_t+1$"," ", "$rb_t+1$"," "))
Included libraries: matrix, dplyr, tidyverse, xtable.
Below is the data from dput(table2a):
structure(c(-0.011918875562309, 0.0493186644094629, 0.00943711646402318,
0.0084043692395113, 0.0140061617086464, 0.00795790133389348,
-0.00372516684283399, 0.00631517247007723, 0.00514156266584497,
0.0039339752041611, 0.0148362913561212, 0.00793003246354337,
-0.0807656037164587, 0.0599852917766847, 0.991792361285981, 0.0102220639400435,
-0.00608828061911691, 0.00967903407684488, 0.010343002117867,
0.00768101625973846, 0.0578541455030235, 0.00478481429473926,
-0.00902328873121743, 0.00964513773477125, -0.799680080407018,
0.340494864072598, 0.0519648273240202, 0.0580235615884655, 0.0850517813830584,
0.0549411579861702, -0.0665428760388874, 0.0435997977143392,
-0.032698572959578, 0.027160069487786, 0.114163705951583, 0.0547487519805466,
0.352025366916776, 0.197746547959218, 0.0476825327079758, 0.0336978915546042,
0.0464511908714403, 0.0319077480426568, 0.904849333951824, 0.0253211146465119,
0.132904050913606, 0.0157735418364402, 0.0653710645280691, 0.0317960059066269,
0.939695537568421, 0.612311426298072, -0.0578948128653228, 0.104343687684969,
-0.0744692071400603, 0.0988006057025484, 0.121089017775182, 0.0784054537723728,
0.0345069733304992, 0.048841914052704, -0.090885199308955, 0.0984546022582597,
-0.280821673428002, 0.248826811381596, -0.0288068135696716, 0.0424024540117092,
-0.0239685609446809, 0.0401498953370305, 0.00219488911775388,
0.0318618569231297, 0.066433933135983, 0.0198480335553826, 0.871940074366622,
0.0400092888905855), .Dim = c(12L, 6L), .Dimnames = list(c("$e_t+1$",
" ", "$r_t+1$", " ", "$\\Delta y_{n,t+1}$", " ", "$s_{n,t+1}$",
" ", "$d_t+1-p_t+1$", " ", "$rb_t+1$", " "), c("ex_stock_ret_100.l1",
"real_int_100.l1", "Chg_1month.l1", "spreads.l1", "log_dp.l1",
"rb_rate_100.l1")))
My desired output (row & column names) is as shown in this picture
enter image description here
In case you want to remove that prefix, you can do the following:
rownames(table2a) <- substring(rownames(table2a), 2)
you can remove the first X and all following dots (no matter how many there are) with the gsub command:
rownames(table2a) <- gsub("^X\\.*","",rownames(table2a))
^ = beginning of the string;
X = your actual X;
\\. = a dot;
* = 0 or more of the before mentioned (in this case \\.); so in total ^X\\. means: if you find X as the first letter and all possible dots following directly behind it.
gsub replaces this find with "", meaning nothing, leaving only whatever comes after
EDIT:
to also get rid of every 2nd rowname, add a little something extra:
rownames(table2a) <- gsub("^X\\.*[1-9]*","",rownames(table2a))
which gets rid of any number directly behind the dots. This should leave those rows empty.
I am trying to create a datatable with child rows: the user will be able to click on a name and see a list of links related to that name. However, the number of itens to show is different for each name.
> data1 <- data.frame(name = c("John", "Maria", "Afonso"),
a = c("abc", "def", "rty"),
b=c("ghj","lop",NA),
c=c("zxc","cvb",NA),
d=c(NA, "mko", NA))
> data1
name a b c d
1 John abc ghj zxc <NA>
2 Maria def lop cvb mko
3 Afonso rty <NA> <NA> <NA>
I am using varsExplore::datatable2 to hide specific columns:
varsExplore::datatable2(x=data1, vars=c("a","b","c","d"))
and it produces the below result
Is it possible to modify DT::datatable in order to only render cells that are not "null"? So, for example, if someone clicked on "Afonso", the table would only render "rty", thus hiding "null" values for the other columns (for this row), while still showing those columns if the user clicked "Maria" (that doesn't have any "null").
(Should I try a different approach in order to achieve this behavior?)
A look into the inner working of varsExplore::datatable2
Following your request I took a look into the varsExplore::datatable2 source code. And I found out that varsExplore::datatable2 calls varsExplore:::.callback2 (3: means that it's not an exported function) to create the javascript code. this function also calls varsExplore:::.child_row_table2 which returns a javascript function format(row_data) that formats the rowdata into the table you see.
A proposed solution
I simply used my js knowledge to change the output of varsExplore:::.child_row_table2 and I came up with the following :
.child_row_table2 <- function(x, pos = NULL) {
names_x <- paste0(names(x), ":")
text <- "
var format = function(d) {
text = '<div><table >' +
"
for (i in seq_along(pos)) {
text <- paste(text, glue::glue(
" ( d[{pos[i]}]!==null ? ( '<tr>' +
'<td>' + '{names_x[pos[i]]}' + '</td>' +
'<td>' + d[{pos[i]}] + '</td>' +
'</tr>' ) : '' ) + " ))
}
paste0(text,
"'</table></div>'
return text;};"
)
}
the only change I did was adding the d[{pos[i]}]!==null ? ....... : '' which will only show the column pos[i] when its value d[pos[i]] is not null.
Looking at the fact that loading the package and adding the function to the global environment won't do the trick, I forked it on github and commited the changes you can now install it by running (the github repo is a read-only cran mirror can't submit pull request)
devtools::install_github("moutikabdessabour/varsExplore")
EDIT
if you don't want to redownload the package I found a solution basically you'll need to override the datatable2 function :
first copy the source code into your R file located at path/to/your/Rfile
# the data.table way
data.table::fwrite(list(capture.output(varsExplore::datatable2)), quote=F, sep='\n', file="path/to/your/Rfile", append=T)
# the baseR way
fileConn<-file("path/to/your/Rfile", open='a')
writeLines(capture.output(varsExplore::datatable2), fileConn)
close(fileConn)
then you'll have to substitute the last ligne
DT::datatable(
x,
...,
escape = -2,
options = opts,
callback = DT::JS(.callback2(x = x, pos = c(0, pos)))
)
with :
DT::datatable(
x,
...,
escape = -2,
options = opts,
callback = DT::JS(gsub("('<tr>.+?(d\\[\\d+\\]).+?</tr>')" , "(\\2==null ? '' : \\1)", varsExplore:::.callback2(x = x, pos = c(0, pos))))
)
what this code is basically doing is adding the js condition using a regular expression.
Result
I'm trying to add 4 series using bosun expressions. They are from 1,2,3,4 weeks ago. I shifted them using shift() to have current time. But I can't add them since they have the shift=1w etc tags. How can I add these series together?
Thank you
edit: here's the query for 2 weeks
$period = d("1w")
$duration = d("30m")
$week1end = tod(1 * $period )
$week1start = tod(1 * $period + $duration )
$week2end = tod(2 * $period )
$week2start = tod(2 * $period + $duration )
$q1 = q("avg:1m-avg:os.cpu{host=myhost}", $week1start, $week1end)
$q2 = q("avg:1m-avg:os.cpu{host=myhost}", $week2start, $week2end)
$shiftedq1 = shift($q1, "1w")
$shiftedq2 = shift($q2, "2w")
$shiftedq1+ $shiftedq2
edit: here's what Bosun said
The problem is similar to: How do I add the series present in the output of an over query:
over("avg:1m-avg:os.cpu{host=myhost}", "30m", "1w", 2)
There is a new function called addtags that is pending documentation (see https://raw.githubusercontent.com/bosun-monitor/bosun/master/docs/expressions.md for draft) which seems to work when combined with rename. Changing the last line to:
$shiftedq1+addtags(rename($shiftedq2,"shift=shiftq2"),"shift=1w")
should generate a single result group like { host=hostname, shift=1w, shiftq2=2w }. If you add additional queries for q3 and q4 you probably need to rename the shift tag for those to unique values like shiftq3 and shiftq4.
If you were using a numbersets instead of seriessets, then the Transpose function would let you "Drop" the unwanted tags. This is useful when generating alerts, since crit and warn need a single number value not a series set:
$average_per_q = avg(merge($shiftedq1,$shiftedq2))
$sum_over_all = sum(t($average_per_q,"host"))
Result: { host=hostname } 7.008055555555557
Side note you probably want to use a counter for os.cpu instead of a gauge. Example: $q1 = q("avg:1m-avg:rate{counter,,1}:os.cpu{. Without that rate section you are using the raw counter values instead of the gauge value.
I am writing this code and have recently come across an error. I have no idea why this is happening. In theory, the english alphabet should be being printed. However, instead of the english alphabet, symbols are being printed instead.
I can not paste the symbols for some reason, but if you ran the code yourself, you'll understand what I mean.
My full code is posted below.
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFHIJKLMNOPQRSTUVWXYZ0123456789"
choice = input("Would you like to encrypt or decrypt? [e/d]: ")
if choice == "e":
message = input("Please insert the message you would like to use: ")
keyword = input("Please insert the keyword you would like to use: ")
ik = len(keyword)
i = 0
string = ''
for A in message:
message1 = (ord(A)) - 96
key1 = (ord(keyword[i])) - 96
addition = message1 + key1
string += (chr(addition))
if i >= ik:
i = 0
else:
i += 1
print (string)
You need to add back the 96 you originally took away :) Alternatively, use the Caesar cipher formula as adding back 96 will still result in symbols appearing (I did the ocr coursework already)
addition = message1 + key1 + 96
your code will not work if the keyword is shorter than the message, so use the modulo operator (%) on i with the length of the keyword inside the line:
key1 = (ord(keyword[i])) - 96
I have the following strings:
F:\Sheyenne\ROI\SWIR32_subset\SWIR32_2005210_East_A.dat
F:\Sheyenne\ROI\SWIR32_subset\SWIR32_2005210_Froemke-Hoy.dat
and from each I want to extract the three variables, 1. SWIR32 2. the date and 3. the text following the date. I want to automate this process for about 200 files, so individually selecting the locations won't exactly work for me.
so I want:
variable1=SWIR32
variable2=2005210
variable3=East_A
variable4=SWIR32
variable5=2005210
variable6=Froemke-Hoy
I am going to be using these to add titles to graphs later on, but since the position of the text in each string varies I am unsure how to do this using strmid
I think you want to use a combination of STRPOS and STRSPLIT. Something like the following:
s = ['F:\Sheyenne\ROI\SWIR32_subset\SWIR32_2005210_East_A.dat', $
'F:\Sheyenne\ROI\SWIR32_subset\SWIR32_2005210_Froemke-Hoy.dat']
name = STRARR(s.length)
date = name
txt = name
foreach sub, s, i do begin
sub = STRMID(sub, 1+STRPOS(sub, '\', /REVERSE_SEARCH))
parts = STRSPLIT(sub, '_', /EXTRACT)
name[i] = parts[0]
date[i] = parts[1]
txt[i] = STRJOIN(parts[2:*], '_')
endforeach
You could also do this with a regular expression (using just STRSPLIT) but regular expressions tend to be complicated and error prone.
Hope this helps!