I currently have a repeating XML segment and from within this segment I am passing multiple fields into a scripting functoid. The scripting functoid contains inline c# which can return a range of different results. I am looking to iterate only when a certain result is found (ZD01), if ZD01 isn't the output then it will simply pass through what the output actually is. I am currently iterating the repeating segment which give me something like below
ZD01 = 1
ZD02 = ZD02
ZD01 = 3
ZD01 = 4
ZD02 = ZD02
ZD01 = 6
What I would like to achieve is
ZD01 = 1
ZD02 = ZD02
ZD01 = 2
ZD01 = 3
ZD02 = ZD02
ZD01 = 4
Is there an easy way to achieve this?
Try to use custom xslt instead of the BizTalk mapper for complex maps and repeating nodes.
An example how to create a custom xslt.
https://www.youtube.com/watch?v=1KZlfOqyfEQ
Related
I am doing some R code practice and is wanting to generate some specific sequence.
I want to use the function seq() to generate sequence, which I have tried the following code, but it seems like character is not available in seq()
n <- c(1 : 100)
seq(from = 1, to = n )
I expect the sequence to be 1 1 2 1 2 3 1 2 3 4.... However, this code isn't able to work
In the following way:
sequence(1:100)
sequence works by creating a vector from 1 until the number passed to sequence() Since this function is vectorized, you can insert multiple numbers at once with c(1:100) at get the necessary output.
I want to test the SAS code my team has produced in R to compare the estimates that we get from each but being new to R am not having much luck. In SAS we have written 3 macros to produce three separate estimates (HFS010, HFS011, HFS012), an example of one given here;
%macro HFS010 (peninc_var, pengn_var, pentax_var, pentype_var, HFS010_x_var);
do i = 1 to dim(pentypex);
if &pentype_var = 1 and &pengn_var = 1 then &HFS010_x_var = &peninc_var;
else if &pentype_var = 1 and &pengn_var = 2 then &HFS010_x_var = &peninc_var + &pentax_var;
end;
%mend HFS010;
Basically the idea is that each macro produces an estimate for gross pension income (so where applicable adds tax deducted from pensions on to pension income value). There are three macros as we want separate estimates for cases where pentype = 1 (HFS010), pentype = 2 (HFS011) and pentype = 3 to 7 (HFS012) and the survey accepts up to 16 entries for pensions.
To attempt to produce an equivalent of the above code in R, I wrote the following;
for(i in 1:16) {
pens_data[[paste0("HFS010_",i)]] <- case_when(
pens_data[[paste0("pentype",i)]] == 1 & pens_data[[paste0("pengn",i)]] == 1 ~ pens_data[[paste0("peninc",i)]],
pens_data[[paste0("pentype",i)]] == 1 & pens_data[[paste0("pengn",i)]] == 2 ~ pens_data[[paste0("peninc",i)]] + pens_data[[paste0("pentax",i)]],
TRUE ~ 0)
This code does not produce errors but upon inspecting the estimates, there were some cases that should have estimates that were left blank.
Does anyone know of a way to write a macro in R? I thought of writing a function potentially for each of HFS010, HFS011, HFS012 but being new to R am not sure how to go about this.
If anyone has any suggestions as to why my R code isn't producing the correct estimates, or how they would write the equivalent of a SAS macro in R it would be greatly appreciated! I have tried to use defmacro but could not get this to work without errors.
Thanks so much!
Ashlee
There are many ways to write this in R. But first a copule of comments:
R works fine with vector, so we should as possible manipulate vectors. This is much faster and allows to avoid slow for loop with side effect.
In order to help other to give you answer please provide a reproducible example that cover both uses cases.
For example:
set.seed(1)
dx <- data.frame(
peninc_var=sample(c(1,3),5,TRUE),
pengn_var=sample(c(1,2),5,TRUE),
pentax_var=1:5)
Here an option in base R. I am creating the new variable HFS010_x_var using ifelse :
dx$HFS010_x_var <-
with(dx,{
## I am adding a last NO condition here to assign missing NA
ifelse(peninc_var==1 & pengn_var==1,peninc_var,
ifelse(peninc_var==1 & pengn_var==2,peninc_var + pentax_var,NA))
})
peninc_var pengn_var pentax_var HFS010_x_var
1: 1 2 1 2
2: 1 2 2 3
3: 3 2 3 NA
4: 3 2 4 NA
5: 1 1 5 1
Another option (more sugar syntax ) is to use data.table:
library(data.table)
setDT(dx)
dx[peninc_var==1 & pengn_var==1,HFS010_x_var := peninc_var]
dx[peninc_var==1 & pengn_var==2,HFS010_x_var := peninc_var+pentax_var]
I'm trying to extract information about the limits and transform of an existing ggplot object. I'm getting close, but need some help. Here's my code
data = data.frame(x=c(1,10,100),y=(c(1,10,100)))
p = ggplot(data=data,aes(x=x,y=y)) + geom_point()
p = p + scale_y_log10()
q = ggplot_build(p)
r = q$panel$y_scales
trans.y = (q$panel$y_scales)[[1]]$trans$name
range.y = (q$panel$y_scales)[[1]]$rang
print(trans.y) gives me exactly what I want
[1] "log-10"
But range.y is a funky S4 object (see below).
> print(range.y)
Reference class object of class "Continuous"
Field "range":
[1] 0 2
> unclass(range.y)
<S4 Type Object>
attr(,".xData")
<environment: 0x11c9a0630>
I don't really understand S4 objects or how to query their attributes and methods. Or, if I'm just going down the wrong rabbit hole here, a better solution would be great :) In Matlab, I could just use the commands "get(gca,'YScale')" and "get(gca,'YLim')", so I wonder if I'm making this harder than it needs to be.
As #MikeWise points out in the comments, this all becomes a lot easier if you update ggplot to v2.0. It now uses ggproto objects instead of proto, and these are more convenient to get info from.
It's easy to find now what you need. Just printing ggplot_build(p) gives you a nice list of all that's there.
ggplot_build(p)$panel$y_scales[[1]]$range here gives you a ggproto object. You can see that contains several parts, one of which is range (again), which contains the data range. All the way down, you end up with:
ggplot_build(p)$panel$y_scales[[1]]$range$range
# [1] 0 2
Where 0 is 10^0 = 1 and 2 is 10^2 = 100.
Another way might be to just look it up in $data part like this:
apply(ggplot_build(p)$data[[1]][1:2], 2, range)
# y x
# 1 0 1
# 2 1 10
# 3 2 100
You can also get the actual range of the plotting window with:
ggplot_build(p)$panel$ranges[[1]]$y.range
[1] -0.1 2.1
I have a model that produces an output csv with some irrelevant material at the end: useful.data
useful.x useful.y useful.z
1 1 1
2 2 2
3 3 3
useless.data
useless.x useless.y useless.z
1 1 1
2 2 2
3 3 3
The issue is the number of rows I want to keep can change depending on the model run. I've never used an if statement in R but I think that looks like my best bet here in that I should use it once I get to the row that says 'useless.data'
Can someone help me with this? Thanks.
Try something like this:
all_content <- readLines("csvFileHere")
numToSkip <- *rows to skip here*
read.csv2(text = all_content, nrows = length(f) - numToSkip, header =
FALSE, stringsAsFactors = FALSE)
With the code above you will be able to change the amount of rows to skip.
Just a little advice. Always make sure if the answer provided is true, so test this answer with your dataset and check it it actually works or not!
This line of code rep(c(0), 2)
creates
x
1 0
2 0
I would like to somehow extend this, in a way appropriate for R, so that I end up with something like the vector below. Basically, I'd like to append integers pairwise as such:
x
1 0
2 0
3 1
4 1
5 2
6 2
I feel like this is an example of R's dislike of loops which has the unintended side effect of makes simple things seem unfamiliar and makes me long for other tools in other languages, like perhaps numpy in Python.
What is the best way I can iterate and increment in this manner?
Copying Nishanth's answer, this does exactly what you want.
rep(0:2, each=2)
data.frame(x = unlist(lapply(0:2, rep, 2)))