LibreOffice Calc - How to reuse multi step formula? - formula

I'm making a balance sheet, Sheet1 is for the ins and outs, and most values are added manually or simple formulas, and Sheet2 is where I created a formula, in the hopes of being able to reuse it.
I'm not an accountant to understand how I could make the calculations easier, and I'm a programmer, so I understand that the way I may be imagining the solution is likely impossible with the way Libreoffice Calc's formulas work.
So, to explain a bit.
On Sheet1, each column is a month, and the value is a tax that will appear one time each month, dependent on another value.
So, the base value is on ROW 17, and on 18, I would like that result to be set. For every month, of course
On Sheet2, I have the function, it contains 5 steps, with the values being reused a lot (hence, simplifying everything into one line would be hell).
This is the complex formula in question, D1 is the input, C6 is the output.
The formula below is the one used on C2, and repeated down to C5.
I would like to keep the constants as a table since it would be easier to update it in the future in case it suffer any changes.
I have been searching for a possible solution but found none, and I believe that it's likely because I'm looking for a solution like a programmer (use Sheet as a function), and I should seek sort of way, but I don't know how Calc works.
In regards to the calculation, I don't know the specific name, but the idea is, from 0 to A1, I have to B1% from A1-0, then from A2-A1, remove B2%, and so on.
Of course the formula's complexity comes from treating lower values, so for example, if D1 was 2K, then I would have to take 7.5% of R$ 96.02, and everything beyond is 0, since there is nothing remaining for them to calculate

Most of the descriptions I found on MULTIPLE.OPERATIONS were confusing, but I found one that made it much easier to understand.
The answer was to use this formula on Sheet1:
=MULTIPLE.OPERATIONS('Sheet2'.$C$6, 'Sheet2'.$D$1, C17)
I can just copy paste it to the side and the calculation will be executed properly.
To explain the arguments:
1 - where the result will appear
2 - the location of the main/first formula variable
3 - the location of dynamic variable you want to insert in that formula (So this is from Sheet1)
More arguments could be used if more variables were needed, but I just needed one.
This is the place with the best explanation I found for the function.
https://wiki.documentfoundation.org/Documentation/Calc_Functions/MULTIPLE.OPERATIONS

Related

summary of row of numbers in R

I just hope to learn how to make a simple statistical summary of the random numbers fra row 1 to 5 in R. (as shown in picture).
And then assign these rows to a single variable.
enter image description here
Hope you can help!
When you type something like 3 on a single line and ask R to "run" it, it doesn't store that anywhere -- it just evaluates it, meaning that it tries to make sense out of whatever you've typed (such as 3, or 2+1, or sqrt(9), all of which would return the same value) and then it more or less evaporates. You can think of your lines 1 through 5 as behaving like you've used a handheld scientific calculator; once you type something like 300 / 100 into such a calculator, it just shows you a 3, and then after you have executed another computation, that 3 is more or less permanently gone.
To do something with your data, you need to do one of two things: either store it into your environment somehow, or to "pipe" your data directly into a useful function.
In your question, you used this script:
1
3
2
7
6
summary()
I don't think it's possible to repair this strategy in the way that you're hoping -- and if it is possible, it's not quite the "right" approach. By typing the numbers on individual lines, you've structured them so that they'll evaluate individually and then evaporate. In order to run the summary() function on those numbers, you will need to bind them together inside a single vector somehow, then feed that vector into summary(). The "store it" approach would be
my_vector <- c(1, 3, 7, 2, 6)
summary(my_vector)
The importance isn't actually the parentheses; it's the function c(), which stands for concatenate, and instructs R to treat those 5 numbers as a collective object (i.e. a vector). We then pass that single object into my_vector().
To use the "piping" approach and avoid having to store something in the environment, you can do this instead (requires R 4.1.0+):
c(1, 3, 7, 2, 6) |> summary()
Note again that the use of c() is required, because we need to bind the five numbers together first. If you have an older version of R, you can get a slightly different pipe operator from the magrittr library instead that will work the same way. The point is that this "binding" part of the process is an essential part that can't be skipped.
Now, the crux of your question: presumably, your data doesn't really look like the example you used. Most likely, it's in some separate .csv file or something like that; if not, hopefully it is easy to get it into that format. Assuming this is true, this means that R will actually be able to do the heavy lifting for you in terms of formatting your data.
As a very simple example, let's say I have a plain text file, my_example.txt, whose contents are
1
3
7
2
6
In this case, I can ask R to parse this file for me. Assuming you're using RStudio, the simplest way to do this is to use the File -> Import Dataset part of the GUI. There are various options dealing with things such as headers, separators, and so forth, but I can't say much meaningful about what you'd need to do there without seeing your actual dataset.
When I import that file, I notice that it does two things in my R console:
my_example <- read.table(...)
View(my_example)
The first line stores an object (called a "data frame" in this case) in my environment; the second shows a nice view of how it's rendered. To get the summary I wanted, I just need to extract the vector of numbers I want, which I see from the view is called V1, which I can do with summary(my_example$V1).
This example is probably not helpful for your actual data set, because there are so many variations on the theme here, but the theme itself is important: point R at a file, as it to render an object, then work with that object. That's the approach I'd recommend instead of typing data as lines within an R script, as it's much faster and less error-prone.
Hopefully this will get you pointed in the right direction in terms of getting your data into R and working with it.

Correlations and what brackets indicate

I have this code, from Julian Farawy's linear models book:
round(cor(seatpos[,-9]),2)
I am unsure what [,-9],2 is doing - could someone please assist?
When you are learning new stuff nested functions can be difficult. This same computation could be accomplished in steps, which might be easier for you to see what KeonV and MrFlick are suggesting.
Here is an alternative way of doing this the same functions but easier steps to differentiate with simple explanations.
sub_seatpos<- seatpos[,-9]
this says take a subset of all rows and all columns EXCEPT column number nine and save it into sub_seatpos (this subseting was done in the initial code, but not saved into a new variable. This just makes seeing how each step works easier).
and reflects the bold portion below
round(cor(seatpos[,-9]),2)
cor_seatpos <- cor(sub_seatpos)
This takes the correlation for sub_seatpos and saves them into a variable named cor_seatpos. It reflects the part listed below in bold
round( cor( seatpos[,-9] ),2)
The final step just says round the correlation to 2 decimal places and would look like this in separate lines of code.
round(cor_seatpos, 2)
it is reflected in the bold below
round( cor(seatpos[,-9]),2)
What makes this confusing is that all of the functions are nested. As you become more proficient, this becomes less of a difficulty to read. But it can be confusing with new functions.

What is wrong with this lines of my R script?What I am missing?

Ok, So I got this long line of code as a part of a script which someone wrote ( I know it seems horrible). So I tried to simplify it.
dH=((-HMF )*( 1.013*10^10*((T+273.2)/298)*exp((292131/1.987)*(1/298-1/(T+273.2)))/(1+(exp((331573/1.987)*(1/284.9-1/(T+273.2))))))*( 4.371*10^-8*((RH+273.2)/298)*exp((55763.5/1.987)*(1/298-1/(RH+273.2)))/(1+(exp((77245.3/1.987)*(1/365.3-1/(RH+273.2))))))*(H[hour]*I[hour]))-((LGR1)*( 123.8*((T+273.2)/298)*exp((-390540/1.987)*(1/298-1/(T+273.2)))/(1+(exp((-402880/1.987)*(1/300.1-1/(T+273.2)))))) *H[hour]*L1a[hour])-((LGR2)*( 123.8*((T+273.2)/298)*exp((-390540/1.987)*(1/298-1/(T+273.2)))/(1+(exp((-402880/1.987)*(1/300.1-1/(T+273.2)))))) *H[hour]*L2a[hour])- ((LGR3)*( 123.8*((T+273.2)/298)*exp((-390540/1.987)*(1/298-1/(T+273.2)))/(1+(exp((-402880/1.987)*(1/300.1-1/(T+273.2)))))) *H[hour]*L3a[hour])
I simplified it like this:
a<-(1.013*10^10*((T+273.2)/298)*exp((292131/1.987)*(1/298-1/(T+273.2)))/(1+(exp((331573/1.987)*(1/284.9-1/(T+273.2))))))
b<-( 4.371*10^-8*((RH+273.2)/298)*exp((55763.5/1.987)*(1/298-1/(RH+273.2)))/(1+(exp((77245.3/1.987)*(1/365.3-1/(RH+273.2))))))
c<-(123.8*((T+273.2)/298)*exp((-390540/1.987)*(1/298-1/(T+273.2)))/(1+(exp((-402880/1.987)*(1/300.1-1/(T+273.2))))))
d<-(1.7168*((T+273.2)/298)*exp((14275.4/1.987)*(1/298-1/(T+273.2)))/(1+(exp((49087.1/1.987)*(1/298.85-1/(T+273.2))))))
dH=((-HMF )*a*b*(H[hour]*I[hour]))-(LGR1*c*H[hour]*L1a[hour])-(LGR2*c*H[hour]*L2a[hour])-(LGR3*c*H[hour]*L3a[hour])
So what basically the model does is that it takes T and RH for different hours and LGR1,LGR2 and LGR3 are constant values. Also L1a, L2a and L3a are also claculated for different hours and a,b,c and d are used to calculate L1a, L2a and L3a for different hours.
The odd thing is that when I only and simply replace the messy long formula with a,b,c, and d my output model changes which I expect not to. I know it might be vague but I was not sure if I can post the full script here.
Thanks in advance for your advice
I took it into an editor that is syntax-aware and use the parenthesis matching capacity to break into its 4 arithmetic terms (separated by minus signs):
dH=
((-HMF )*( 1.013*10^10*((T+273.2)/298)*exp((292131/1.987)*(1/298-1/(T+273.2)))/(1+(exp((331573/1.987)*(1/284.9-1/(T+273.2))))))*( 4.371*10^-8*((RH+273.2)/298)*exp((55763.5/1.987)*(1/298-1/(RH+273.2)))/(1+(exp((77245.3/1.987)*(1/365.3-1/(RH+273.2))))))*(H[hour]*I[hour]))-
((LGR1)*( 123.8*((T+273.2)/298)*exp((-390540/1.987)*(1/298-1/(T+273.2)))/(1+(exp((-402880/1.987)*(1/300.1-1/(T+273.2)))))) *H[hour]*L1a[hour])-
((LGR2)*( 123.8*((T+273.2)/298)*exp((-390540/1.987)*(1/298-1/(T+273.2)))/(1+(exp((-402880/1.987)*(1/300.1-1/(T+273.2)))))) *H[hour]*L2a[hour])-
((LGR3)*( 123.8*((T+273.2)/298)*exp((-390540/1.987)*(1/298-1/(T+273.2)))/(1+(exp((-402880/1.987)*(1/300.1-1/(T+273.2)))))) *H[hour]*L3a[hour])
The fact that your terms seem to start in different sections of that expression make me think you separated terms inappropriately. It does appear (in my editor that the last three terms all share a common factor and tht the only items that vary in those three terms are the first and last factors:
( 123.8*((T+273.2)/298)*exp((-390540/1.987)*(1/298-1/(T+273.2)))/(1+(exp((-402880/1.987)*(1/300.1-1/(T+273.2)))))) *H[hour]

MS Project - Column with formula does not calculate correctly

I am trying to add an indicator light to my MS Project sheet similar to this one: Late Indicator Tool. I'm using a simplified formula: IIf([% Complete]<>100,DateDiff("d",[Deadline],[Finish]))
For any row that I enter all the information by hand, the formula works perfectly. However, the formula returns 0 for any rows where I paste data in from other project files (even if all I paste in is the task name).
Even if I attempt to use an even simpler formula ([Deadline]-[Finish]), it still returns 0 (and breaks even further by returning 4294925695.29 or 4294925708.67 instead of #Error in the rows where the Deadline is NA).
Has anyone else had any issues with calculated columns in MS Project and can help me fix it?
EDIT: I gave up on this approach when I discovered a work-around: There is a column called "Finish Variance" that will automatically calculate the difference between the Finish date and the date in the "Baseline Finish" column (which I am now using instead of "Deadline").
Your first problem sounds like your project may be corrupted (or the file that you are pasting from). I suggest building a small sample project to see if you can replicate this error. (I could not replicate it.)
As for the second problem, when the Deadline is NA, Project is substituting a default value of the largest unsigned 32-bit integer (2^32-1). To avoid this unintended value, use an If statement in your formula to return your own value in case Deadline is NA.

using value of a function & nested function in R

I wrote a function in R - called "filtre": it takes a dataframe, and for each line it says whether it should go in say bin 1 or 2. At the end, we have two data frames that sum up to the original input, and corresponding respectively to all lines thrown in either bin 1 or 2. These two sets of bin 1 and 2 are referred to as filtre1 and filtre2. For convenience the values of filtre1 and filtre2 are calculated but not returned, because it is an intermediary thing in a bigger process (plus they are quite big data frame). I have the following issue:
(i) When I later on want to use filtre1 (or filtre2), they simply don't show up... like if their value was stuck within the function, and would not be recognised elsewhere - which would oblige me to copy the whole function every time I feel like using it - quite painful and heavy.
I suspect this is a rather simple thing, but I did search on the web and did not find the answer really (I was not sure of best key words). Sorry for any inconvenience.
Thxs / g.
It's pretty hard to know the optimum way of achieve what you want as you do not provide proper example, but I'll give it a try. If your variables filtre1 and filtre2 are defined inside of your function and you do not return them, of course they do not show up on your environment. But you could just return the classification and make filtre1 and filtre2 afterwards:
#example data
df<-data.frame(id=1:20,x=sample(1:20,20,replace=TRUE))
filtre<-function(df){
#example function, this could of course be done by bins<-df$x<10
bins<-numeric(nrow(df))
for(i in 1:nrow(df))
if(df$x<10)
bins[i]<-1
return(bins)
}
bins<-filtre(df)
filtre1<-df[bins==1,]
filtre2<-df[bins==0,]

Resources