saxman
saxman

Reputation: 21

Cannot create a silly list of vectors with R

I can't understand why my data frame is treating stringsAsFactors. Can you please help?

I have:

>  frequentPatterns <- data.frame(
    pattern = character(),
    occurrences = numeric(),
    stringsAsFactors = FALSE
)

from the terminal I execute the following:

> obs2 = c()
> obs2$pattern <- "TAGCAA"
> obs2$count <- 5
> frquentPatterns <- rbind(frequentPatterns,obs2)

The first execution of the lines above loads the values into the data frame correctly. The SECOND run, it results in a warning

In `[<-.factor`(`*tmp*`, ri, value = "AGAAGCGAGATT") :
  invalid factor level, NA generated

I thought, and have read and read, that "stringsAsFactors = FALSE" should result in rbind not attempting to treat character values as factors. Am I smoking crack? Is that not what I have? what am I missing :) Thank you in advance for the help!!

Upvotes: 1

Views: 39

Answers (1)

joran
joran

Reputation: 173677

stringsAsFactors only applies to the initial creation of the data frame. In addition to that misunderstanding there are other things you're doing wrong subsequently.

You start out creating obs2 as if it were going to be a vector, but it's really just NULL. I think you'll find that adding an integer count has caused it to be coerced to a list (not a data frame). Don't rbind a data frame to a list. If you're going to do that (grow objects), (and you shouldn't grow objects), rbind two data frames:

> rbind(frequentPatterns,data.frame(pattern = 'AGAA',count = 5,stringsAsFactors = FALSE))
  pattern count
1    AGAA     5

Finally, it is hardly necessary to be under the influence of drugs to make mistakes when writing code. I know this from personal experience. ;)

Upvotes: 1

Related Questions