I export a large amount of simulation data from COMSOL Multiphysics (a powerful finite element modelling software). The exported data files are not in the format I want for plotting, so here’s how I use R to get my data into the shape I want.
First off I have to read the data in and rename/scale some of the columns:
# Read in the data and skip the 4 # line header produced by COMSOL data <- read.csv(“COMSOL_output.csv”, skip = 4) # Extract the names of the data frame columns # and rename for ease where necessary names_vec <- names(data) names_vec_edit <- names_vec names_vec_edit<-"n0" names(data) <- names_vec_edit # Rescale values where necessary data$Time <- data$time/(60*60)
Then I use the data.table package to change the shape of the data for plotting. I want one column denoting the type of variable (cells (n), oxygen (o) or VEGF (v), and then four columns holding statistics such as the mean and standard deviation for the corresponding variable.
# I used setDT to convert to a data table from a data # frame, then I melt to create the desired format data_2 <- data.table::melt(setDT(data), id.vars = c("n0","c_0", "time","av_dead"), measure = list(c(4,7,10,29), c(5,8,11,30), c(6,9,12,31)), value.name = c("n","o","v")) # Then I rename the columns data_2$variable <- as.numeric(data_2$variable) data_2$variable[which(data_7$variable == 1)] <- "Mean" data_2$variable[which(data_7$variable == 2)] <- "Min" data_2$variable[which(data_7$variable == 3)] <- "Max" data_2$variable[which(data_7$variable == 4)] <- "SD"