I have a dataframe called data
. Data has a column called predicted_OL
which is comprised of lists. I want to create a new column that is an operation on an element in a list in predicted_OL
. For example, I want to do:
data$new_column1 <- data$predicted_OL$fit + 1
data$new_column2 <- data$predicted_OL$r2 * 2
How do I do this? See example below of what is inside of data$predicted_OL
:
> data$predicted_OL[1]
[[1]]
[[1]]$fit
1
-0.005725406
[[1]]$se.fit
[1] 0.0004004448
[[1]]$df
[1] 249
[[1]]$residual.scale
[1] 0.004585551
[[1]]$r2
[1] 0.04053683
EDIT: Hmm, I have been trying to create a reproducible sample, but for some reason that isn't working. My attempt below produces an error that I can't seem to fix.
modeldata <- data.frame(X = sample(1:20), Y = sample(1:20))
reg <- lm(Y ~ X, data = modeldata)
sample_pred <- function(model,value)
{
next_obs <- data.frame(X = c(value))
result <- predict(model,next_obs,se.fit=TRUE)
c(result, value = value)
}
sampledata <- data.frame(X=sample(1:5))
sampledata$predictedY <- sapply(sampledata$X, sample_pred, model = reg)