Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert multiple cumulative columns to normal form and plot in R

So I have a data frame of the following form represented in a very simple way:

Sno Box1 Box2
1 5 8
2 7 9
3 13 12

But it consists of 50 such boxes and 100 such serial numbers

Now I am facing two problems:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Firstly, I want to convert this cumulative data to normal data

Sno Box1 Box2
1 5 8
2 2 1
3 6 3

Secondly, I want to plot each of these columns in a graph. So the plot 1 will produce a graph for Box 1 with the SNo as the x-axis and the box 1 values as the y-axis. Similarly, plot 2 will produce a graph for Box 2 with the SNo as the x-axis and the box 2 values as the y-axis, and so on.

So the end result would be 50 graphs represented in a paper, which will show the data in a daily basis, using R.

>Solution :

Your data is (please provide code, not tables/ pictures)

df <- data.frame(Sno= 1:3, Box1= c(5,7,13), Box2= c(8,9,12))

We can simply use difference between rows to make it "normal" and not cumulative:

df <- cbind.data.frame(Sno= df$Sno,
                       rbind.data.frame(df[1, 2:ncol(df)],
                                        df[2:(nrow(df)), 2:ncol(df)] - 
                                        df[1:(nrow(df)-1), 2:ncol(df)])
                                        )

And you can plot all the columns like that:

for(col_i in names(df)[-1]){
  plot(df$Sno, df[ , col_i], ylab= col_i)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading