-2

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)
user2374133
  • 193
  • 1
  • 11
  • 1
    Please `dput` a small sample of your data frame (see [**here**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610)) and show us what you have tried so far. – Henrik Jan 04 '14 at 21:13
  • See edit above - tried to create a sample, but can't seem to get that to work. Frustrating :/ – user2374133 Jan 05 '14 at 01:10
  • The structure of the `sampledata` does not fit the one of `data`. Please use `dput(data)` to provide reproducible data. If it's too large, you can post it at pastebin.com and provide the link. – Sven Hohenstein Jan 05 '14 at 07:24

1 Answers1

1

You can try sapply in combination with [[:

data$new_column1 <- sapply(data$predicted_OL, "[[", "fit") + 1

data$new_column2 <- sapply(data$predicted_OL, "[[", "r2") * 2
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • Thanks. How do I deal with NAs in data$predicted_OL? Running your code gives me Error in FUN(X[[445L]], ...) : subscript out of bounds – user2374133 Jan 04 '14 at 21:29
  • @user2374133 It appears your data has a messy structure. It is not reproducible with the information you have provided. – Sven Hohenstein Jan 05 '14 at 07:26