Create Lagged Variable in R for Groups

Nizamuddin Siddiqui
Updated on 12-Aug-2020 12:11:53

465 Views

Lagged variable is the type of variable that contains the previous value of the variable for which we want to create the lagged variable and the first value is neglected. Therefore, we will always have one missing value in each of the groups, if we are creating a lagged variable that depends on a grouping variable or factor variable.ExampleConsider the below data frame:> set.seed(2) > Factor Rate df df Factor Rate 1 F1 12 2 F1 54 3 F1 18 4 F1 26 5 F1 14 6 F2 25 7 F2 81 8 F2 47 9 F2 15 10 F2 ... Read More

Multiply Matrix Rows in R with a Vector

Nizamuddin Siddiqui
Updated on 12-Aug-2020 12:08:19

903 Views

When we multiple a matrix with a vector in R, the multiplication is done by column but if we want to do it with rows then we can use transpose function. We can multiply the transpose of the matrix with the vector and then take the transpose of that multiplication this will result in the multiplication by rows.ExampleConsider the below matrix −> M1 M1    [,1] [,2] [,3] [,4] [,5] [1,] 1    6   11   16    21 [2,] 2    7   12   17    22 [3,] 3    8   13   18    23 [4,] 4    9   14   19    24 [5,] 5   10   15   20    25 > V1 M1*V1    [,1] [,2] [,3] [,4] [,5] [1,] 1    6   11   16   21 [2,] 4   14   24   34   44 [3,] 9   24   39   54   69 [4,] 16  36   56   76   96 [5,] 25  50   75  100  125Row-wise Multiplication −> t(t(M1)*V1)    [,1] [,2] [,3] [,4] [,5] [1,] 1   12   33   64  105 [2,] 2   14   36   68 110 [3,] 3   16   39   72 115 [4,] 4   18   42   76 120 [5,] 5   20   45   80 125Let’s have a look at one more example −> M2 M2       [,1] [,2] [,3] [,4] [,5] [1,]   72    5   36   11   76 [2,]   61   38   17   73   25 [3,]   96    9   62   79   64 [4,]   77   53   80   78   50 [5,]   81   15   21   43   23 > V2 V2 [1] 28 20 1 68 86 > t(t(M2)*V2) [,1] [,2] [,3] [,4] [,5] [1,] 2016 100 36 748 6536 [2,] 1708 760 17 4964 2150 [3,] 2688 180 62 5372 5504 [4,] 2156 1060 80 5304 4300 [5,] 2268 300 21 2924 1978

Create Vector with Named Elements in One Line Code in R

Nizamuddin Siddiqui
Updated on 12-Aug-2020 12:00:39

871 Views

Vectors are frequently created in R but most of the times we don’t give names to their elements and if we want to give their names then we can use setNames function. This function will help us to name the vector elements in a single line of code, obviously this will save our time and workspace in R.Examples > V1 V1 A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 > V2 V2 A B C D E F G H I J 1 2 3 4 ... Read More

Plot a Function with ggplot2 in R

Nizamuddin Siddiqui
Updated on 12-Aug-2020 11:57:00

5K+ Views

Plotting a function is very easy with curve function but we can do it with ggplot2 as well. Since ggplot2 provides a better-looking plot, it is common to use it for plotting instead of other plotting functions. To plot a function, we should specify the function under stat_function in ggplot.ExampleConsider the below data frame −> x df library(ggplot2)Plotting of functions is as shown below:> ggplot(df, aes(x))+ + stat_function(fun=function(x) log(x))> ggplot(df, aes(x))+ + stat_function(fun=function(x) log(x)/x)Output> ggplot(df, aes(x))+ + stat_function(fun=function(x) log(x)/(x-3))Output> ggplot(df, aes(x))+ + stat_function(fun=function(x) (exp(x)^2)*2)OutputRead More

Change Line Color for Line Chart Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 12-Aug-2020 11:54:25

870 Views

When we create line chart with each of the lines having different color, we might want to change the color of lines if the colors we used at the first time are not making the chart attractive. This can be done by manually setting the color of the lines in the chart with the help of scale_color_manual function.ExampleConsider the below data frame −> set.seed(2) > Group Time Frequency df df   Group Time Frequency  1 1 Time1  3  2 2 Time2  6  3 3 Time1  5  4 4 Time2  3  5 5 Time1  9  6 1 Time2  9  7 ... Read More

View Saved RData File in Windows

Nizamuddin Siddiqui
Updated on 12-Aug-2020 11:50:46

1K+ Views

We save our data files created in R to use them in the future and these files have an extension .Rdata. To view these files, we can make use of load function that will read the path of the file on your system. Suppose you save the file in Documents folder as I do then you will have to provide the path of the Documents folder and that’s it.ExampleSuppose that you created a data frame df and saved it as df.Rdata file in your system −> set.seed(99) > x1 x2 x3 df df x1 x2 x3 1 2 0.7542310 3.3730539 ... Read More

Sensitivity Analysis in Financial Management

Mandalika
Updated on 12-Aug-2020 11:30:06

748 Views

In a business, decision making is very important aspect. Decision making can direct the business in a successful way or in an unsuccessful way. So, if a business wants to be successful, correct decisions should be taken in given circumstances.A lot of independent variables are involved in decision making mainly in financial aspects of the firm. So, there is a need of a tool or a technique to take appropriate decisions.Sensitivity analysis is a tool or a technique which tells about how independent variable impacts a dependent variable under current conditions. Investors use this tool to evaluate the result of ... Read More

Financial Statements in Financial Management

Mandalika
Updated on 12-Aug-2020 11:29:16

441 Views

Financial statements are the reports prepared by a firm to represent their financial activities in an accounting year. These gives how the firm carries its activities, maintain its cash flows and how well the firm is doing in the market.Nature of financial statements includes −Recording facts.Accounting conversions.Assumptions.Personal judgements.Objectives of financial statements are as follows −To provide information about economic resources of a firm.To provide information about changes in economic resources of a firm.To provide information about net resources of a firm.To provide information about estimation of earning potential of a firm.Types of financial statements includes −Balance sheetAssetsLiabilitiesEquityIncome statementIncomeExpensesProfit or lossCash ... Read More

Explain Cash Flows in Financial Management

Mandalika
Updated on 12-Aug-2020 11:28:19

381 Views

Money is an important factor in the business. A firm should maintain a clear record about income and outing of money to evaluate and estimate their performance. A cash flow statement is a record, which records firms in and out flow of cash in detail. Monitoring, analysing and optimising the cash flow is called cash flow management.Importance of cash flow management is explained below:Solvency and credit worthiness.CAPEX and investment.Improves vendor ad employee relationships.Indicators of cash flow management are as follows −EBIT − It tells about earnings after leverage and tax expenses which are deducted. EBITDA tells about operating efficiency of ... Read More

Forecasting in Financial Management

Mandalika
Updated on 12-Aug-2020 11:27:25

326 Views

The term forecasting refers to predication of future with the given circumstances. In forecasting, both macro and micro economic factors will be considered. Financial forecasting tells about company’s future action.Financial forecasting made through projected financial statements (income statements, balance sheets and cash flows). It helps to make decisions like capital investments, requirement of working capital, funds requirement etc.Features of forecasting include −Relates to future events.Depends on historical and current events.Predication of future events.Forecasting techniques.Quantitative and qualitative techniques are two types of financial forecasting techniques, which are explained below in detail.Quantitative techniquesCasual methods.Simple linear and multiple regression.Days sales technique.Percentage of sales ... Read More

Advertisements