0

I need to be able to create a data frame from a set of passed in parameters. I realize I can hand type this, but I'm simplifying for the purposes of the question.

Given two vectors:

names <- c('Name', 'Height', 'DateOfBirth') 
types <- c('character', 'integer', 'POSIXct')

How can I create a data frame with those names and data types? This works, but it creates everything as characters:

myDataFrame <- as.data.frame(setNames(replicate(length(myNames),character(0)), myNames))

I tried this, but it doesn't work:

myDataFrame <- as.data.frame(setNames(types, names))

I've searched and don't see anything. The setNames function came from searching.

John Tarr
  • 717
  • 1
  • 9
  • 21
  • 2
    I think [this is a duplicate](http://stackoverflow.com/q/10689055/903061). Though not all the answers meet your criteria, Floo0's does: `read.table(text = "", colClasses = types, col.names = names)` – Gregor Thomas Oct 28 '15 at 18:58
  • Thank you. I hadn't seen Floo0's answer before. Is proper etiquette to leave this question so it re-directs others to the linked answer you gave, or does a duplicate count toward the unanswered count and should therefore be deleted? I know I've found things before by stumbling across the duplicate and then finding the original answer, but wasn't sure on what is proper. – John Tarr Oct 28 '15 at 19:17
  • Yup! Flagging as a duplicate is intended to serve as a helpful pointer. – Gregor Thomas Oct 28 '15 at 19:32

0 Answers0