I coded the answer to my issue and it is follow up from Data import and text to column (when in excel A1 = 1+1+1 and 3 is visible)
The code below works but I wonder if there is any way to improve it. I mean I import the same sheet twice as I could not find a way to maintain other columns after I extract formulas from Col_2.
So, I have got excel file with 3 columns. Col_1 is text, Col_2 has got simple formula, for example B2 = 2+2; B3 = 3+3 etc...Col_3 is just to highlight my problem.
I import this file using
My code is rather long for such a simple task? Any ideas how to import formulas and maintain other columns?
library("openxlsx")
library("splitstackshape")
library("gsub")
library("data.table")
setwd("C:/Users/ml404e/Desktop")
# read as workbook object
wb <- loadWorkbook("Book1.xlsx")
# get the 1st sheet
sheet1 <- wb$worksheets[[1]]
# get the cell formulas
sheet1$sheet_data$f
# get the df
df <- as.data.frame(sheet1$sheet_data$f)
#remove NA
df <- na.omit(df)
#split cells
df<- cSplit(df, "sheet1$sheet_data$f","+")
#remove extra string
df <- data.frame(lapply(df, function(x) {gsub("<f>", "", x) }))
df <- data.frame(lapply(df, function(x) {gsub("</f>", "", x) }))
#change columns names
setnames(df,1,"Col_2")
setnames(df,2,"Col_2_2")
#change factor to numaric
df[] <- lapply(df, as.numeric)
#get second time
df2 <- read.xlsx("Book1.xlsx")
df2$Col_2<-NULL
#combine both
df_final <- cbind(df,df2)
#get the initila order
df_final <- df_final[,c(3,1,2,4)]