This is a really specific question. I am fairly new at R but am starting to grasp the basics. I recently have been running sensitivity tests on a model for work. I basically run a low, middle, and high test for each of the 49 parameters in the model, and get a separate data frame containing the results of each of the 21 variables. What I want to do is create a data frame that combines the results of each parameters tests into a single data frame. For example, for the first parameter, I want the first column of the new data frame to be the first variables result for the low test, the second column the first variables result for the middle test, and third to be the first variables result for the high test. Repeating this pattern for the following 20 variables that are in each data frame. A potential problem with this is that each data frame (low,mid,high) each have the same variable column names (V1, V2, V3, etc.) and I want my new data frame to read V1.low, V1.mid, V1.high etc.
Asked
Active
Viewed 163 times
-3
-
3Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to produce a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). This will make it much easier for others to help you. – Jaap Aug 12 '15 at 18:39
-
you can change the names of a data.frame's columns using `names(my_df) <- vector_of_new_names` – arvi1000 Aug 12 '15 at 18:41
1 Answers
0
numbers <- c(1:20)
levels <- c("low","med","high")
names <- NULL
for (number in numbers){
for (level in levels){
names <- c(names,sprintf("V%s%s", number, level))
}
}
x<- matrix(0, nrow = 49, ncol = 60)
colnames(x) <-names
Now you have a empty matrix X with the names you want. You can fill it in with your model output vectors.