Reputation: 934
I'm currently generating a dataframe as such -
modelOption_RT <- data.frame(
typeID = rep(c(2,1), each = 4),
ID = c(1:8),
modelOption = c("good", "avg", "bad", "marginCost", "year1Premium")[c(1,2,3,4,5,1,2,3)],
descrip = c("Favorable", "Average", "Adverse", "Margin Cost", "Year 1 Premium")[c(1,2,3,4,5,1,2,3)],
# Generate random values for model options
vals = sort(
round(
runif(5, min=0, max=1),
digits = 2
), decreasing = TRUE)[c(1,2,3,4,5,1,2,3)]
)
> modelOption_RT
typeID ID modelOption descrip vals
1 2 1 good Favorable 0.61
2 2 2 avg Average 0.36
3 2 3 bad Adverse 0.24
4 2 4 marginCost Margin Cost 0.11
5 1 5 year1Premium Year 1 Premium 0.01
6 1 6 good Favorable 0.61
7 1 7 avg Average 0.36
8 1 8 bad Adverse 0.24
There is just one problem with this - all the values currently in the val column represent a percentage, but:
Any suggestions? The only idea I had left was to create the dataframe as is, and the manually change the val value for year1Premium outside the dataframe function. Not sure if it's efficient.
i.e
modelOption_RT[modelOption_RT$modelOption == "year1Premium", "vals"] <- runif(1, min=0, max=10000)
Upvotes: 1
Views: 212
Reputation: 73692
You can use dplyr
's mutate()
and replace()
.
set.seed(96311)
library(dplyr)
modelOption_RT <- data.frame(
typeID = rep(1:2, each = 4),
ID = 1:8,
modelOption = c("good", "avg", "bad", "marginCost",
"year1Premium")[c(1:5, 1:3)],
descrip = c("Favorable", "Average", "Adverse", "Margin Cost",
"Year 1 Premium")[c(1:5, 1:3)],
vals = sort(round(runif(8, min=0, max=1), digits = 2), decreasing = TRUE)) %>%
mutate(vals = replace(vals, modelOption == "year1Premium",
runif(1, min=0, max=10000)))
modelOption_RT
# typeID ID modelOption descrip vals
# 1 1 1 good Favorable 0.95
# 2 1 2 avg Average 0.66
# 3 1 3 bad Adverse 0.56
# 4 1 4 marginCost Margin Cost 0.49
# 5 2 5 year1Premium Year 1 Premium 6032.65
# 6 2 6 good Favorable 0.32
# 7 2 7 avg Average 0.17
# 8 2 8 bad Adverse 0.03
Upvotes: 1