0

I'm trying to save a numeric vector that is calculated in a for loop, and then save that into a new column in a dataframe at the end of the for loop.

d.dataframe = NULL
for (i in 1:length(viral_raw)) {

  scores <- PWMscoreStartingAt(human.pwm, viral_raw[[i]], starting.at = 1:99)

  d.dataframe$i <-scores
}

But right now its overwriting the i'th column everytime

4
  • 2
    delete d.dataframe$i line and then add d.dataframe=cbind(d.dataframe,scores) Commented Nov 8, 2016 at 16:04
  • @User7598 that worked beautifully! thanks! Commented Nov 8, 2016 at 16:11
  • great - just added as an answer Commented Nov 8, 2016 at 16:36
  • Just FYI, d.dataframe$i <-scores doesn't overwrite the ith column, it overwrites the column named "i" in each iteration. See the section "Recursive (list-like) objects" in the help file from help("$"), although this entire help file is worth a careful read. Commented Nov 8, 2016 at 16:40

1 Answer 1

1

As I commented above, you should delete d.dataframe$i line and then add d.dataframe=cbind(d.dataframe,scores)

The final code will look like:

d.dataframe = NULL
for (i in 1:length(viral_raw)) {

  scores <- PWMscoreStartingAt(human.pwm, viral_raw[[i]], starting.at = 1:99)

  d.dataframe=cbind(d.dataframe,scores)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.