0

I'm working with a large dataset set and I would like to plot several plots. My data looks likes this

SessionID <- (Results-C1-P11-D0 ,  Results-CP0.9-P11-D0, Results-CP0.95-P11-D0, Results-C1-P22-D0 ,  Results-CP0.9-P22-D0, Results-CP0.95-P22-D0, Results-C1-P22-D2 ,  Results-CP0.9-P22-D2, Results-CP0.95-P22-D2 )
Costs <- (10, 20, 30, 40, 50, 60, 70, 80, 90) 

In reality the SessionID contain information on the parameters used for the calculation of the result, i.e. C is the capacity so C1 actually means C=1). I would like to create plots from this data: on the X-axis I would like to plot the parameter C and on the y-axis the Costs only for the results of P=11 and P=22 where D=0. And than the same plot for D=2.

So far I have tried to split the string of the session with the help of this here and here, but I don't know what the most efficient way is to split the information of the SessionID since I ultimately want to come up with a plot that is looped over the different parameters like is done here (as I said I have a large data set with many parameters).

lmo
  • 37,904
  • 9
  • 56
  • 69
ima
  • 155
  • 12
  • Please include your desired output for the example data, that is how you would like the data split. – lmo Aug 26 '17 at 21:19
  • Desired output are plots with the costs where I could alter one of the parameter (i.e. the value of D), and where another parameter can be used for one of the axis on the graph here that would be C. Is this clear enough? – ima Aug 26 '17 at 21:28
  • Hi @ima if my answer helped you with your issue please consider accepting it (check mark to the left). This lets the community know the answer worked and that the issue is closed. You can change to a better answer in the future if necessary. – CPak Sep 09 '17 at 19:12

1 Answers1

1

Use myfun to convert your vectors into a data.frame. They require the packages purrr and dplyr

myfun <- function(S, Costs) {
              require(purrr)
              require(dplyr)
              df <- do.call(rbind.data.frame, strsplit(S, "-")) %>%
                      setNames(c("Results","C","P","D")) %>%
                      cbind(Costs)
              return(df)
         }

df <- myfun(SessionID, Costs)

Output

  Results      C   P  D Costs
1 Results     C1 P11 D0    10
2 Results  CP0.9 P11 D0    20
3 Results CP0.95 P11 D0    30
4 Results     C1 P22 D0    40
5 Results  CP0.9 P22 D0    50
6 Results CP0.95 P22 D0    60
7 Results     C1 P22 D2    70
8 Results  CP0.9 P22 D2    80
9 Results CP0.95 P22 D2    90

Plot

ggplot2 lets you plot this easily

library(ggplot2)
ggplot(data=df, aes(x=C, y=Costs, color=P)) +
 geom_point() +
 facet_wrap(~D) +
 theme_classic()

NOTE You can install the required packages with

install.packages(c("ggplot2", "purrr","dplyr"))
CPak
  • 13,260
  • 3
  • 30
  • 48