I'd like to create a new column in my data table with variance of values that are stored in some other columns that I have in my table. I thought the mutate function from dplyr package would be the best solution for me, but unfortunately it does not seem to work.
Here is the code that I'm using:
require(dplyr)
CPI <- read.csv("CPI.csv", sep = ";")
CPI <- tbl_df(CPI)
CPI <- mutate(CPI, variance = var(CPI.1:CPI.6, na.rm = T))
and this is the error that I get in return:
Error in c(1.3, 1.4, 2.9, 3.1, NA, NA, NA, NA, NA, NA, NA, NA):c(NA, 1.2253333335, :
NA/NaN argument
Is it possible to calculate the variance using mutate? Should I use other solution?