Home > database >  Convert multiple cumulative columns to normal form and plot in R
Convert multiple cumulative columns to normal form and plot in R

Time:11-17

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:

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.

CodePudding user response:

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)
}
  • Related