Search code examples
rcalculated-columns

How to add a new column in data frame using calculation in R?


I want to add a new column with calculation. In the below data frame,

Env<- c("High_inoc","High_NO_inoc","Low_inoc", "Low_NO_inoc")
CV1<- c(30,150,20,100)
CV2<- c(74,99,49,73)
CV3<- c(78,106,56,69)
CV4<- c(86,92,66,70)
CV5<- c(74,98,57,79)

Data<-data.frame(Env,CV1,CV2,CV3,CV4,CV5)

Data$Mean <- rowMeans(Data %>% select(-Env))
Data <- rbind(Data, c("Mean", colMeans(Data %>% select(-Env))))

enter image description here

I'd like to add a new column names 'Env_index' with calculation {each value of 'mean' column - overall mean (76.3) such as 68.4 - 76.3 , 109- 76.3 ,... 78.2 - 76.3

So, I did like this and obtained what I want.

Data$Env_index <- c(68.4-76.3,109-76.3,49.6-76.3,78.2-76.3, 76.3-76.3)

enter image description here

But, I want to directly calculate using code, so if I code like this,

Data$Env_index <- with (data, data$Mean - 76.3)

It generates error. Could you let me know how to calculate?

Thanks,


Solution

  • To make the calculation dynamic which will work on any data you can do :

    Data$Mean <- as.numeric(Data$Mean)
    Data$Env_index <- Data$Mean - Data$Mean[nrow(Data)]
    Data
    
    #           Env CV1   CV2   CV3  CV4 CV5  Mean Env_index
    #1    High_inoc  30    74    78   86  74  68.4      -7.9
    #2 High_NO_inoc 150    99   106   92  98 109.0      32.7
    #3     Low_inoc  20    49    56   66  57  49.6     -26.7
    #4  Low_NO_inoc 100    73    69   70  79  78.2       1.9
    #5         Mean  75 73.75 77.25 78.5  77  76.3       0.0
    

    Data$Mean[nrow(Data)] will select last value of Data$Mean.