0

I currently have a integer vector exampleVector, and a data frame exampleDF, and I would like to add each element of exampleVector as a new column, whose elements are NA, in the data frame exampleDF. For illustration, I currently have:

exampleVector <- 1:6
exampleDF <- data.frame(First=1:4, Second=4:7,Third=7:10)
exampleDF
  First Second Third  
1     1      4     7  
2     2      5     8  
3     3      6     9  
4     4      7    10  

And what I would like to be able to create is

exampleDF
  First Second Third    1    2    3    4    5    6
1     1      4     7 <NA> <NA> <NA> <NA> <NA> <NA>
2     2      5     8 <NA> <NA> <NA> <NA> <NA> <NA>
3     3      6     9 <NA> <NA> <NA> <NA> <NA> <NA>
4     4      7    10 <NA> <NA> <NA> <NA> <NA> <NA>

Where exampleDF[4:9] are character vectors.

I am aware that I would be able to do this using variations of the below commands:

exampleDF$"1" <- as.character(NA)
exampleDF[["1"]] <- as.character(NA)
exampleDF[c("1","2","3","4","5","6")] <- as.character(NA)

But I need something that is more flexible. Everything that I've been able to find online has been about adding one column to multiple data frames, and to do so they suggest mapply and cbind.

My apologies if I'm missing something obvious here - I am very new to the R language and I'm trying to do this without a for loop, if possible, as recent interactions have led me to believe that this is mostly considered a hack in R scripts, as the apply functions are typically sufficient.

rawr
  • 20,481
  • 4
  • 44
  • 78
JZeolla
  • 128
  • 1
  • 1
  • 8
  • Could you explain what you mean by flexible? Because your last example doesn't require an explicit for loop – Simon Apr 10 '16 at 03:50
  • 1
    is this flexible enough? exampleDF[as.character(exampleVector)] <- NA_character_ – chinsoon12 Apr 10 '16 at 04:21
  • how was `exampleDF[c("1","2","3","4","5","6")] <- as.character(NA)` not flexible enough and @chinsoon12 how is yours different than what the OP already has? – rawr Apr 10 '16 at 04:39
  • @rawr his exampleVector is type integer. And he is typing the number one by one as a character in OP. – chinsoon12 Apr 10 '16 at 04:55
  • @chinsoon12 you're telling me op can `as.character(NA)` but not `as.character(exampleVector)` ? I don't believe that – rawr Apr 10 '16 at 05:06
  • 1
    Just a short remark on the last sentence: The `*apply` functions are wrappers for `for` loops; they are more compact and less error-prone than `for` loops, but they are usually not faster. It is true that `for` loops should generally be avoided in R, but the goal should be to exploit the vectorized capabilities of R, and not to use `apply` instead of `for`. More information [here](http://stackoverflow.com/a/2276001/4770166). – RHertel Apr 10 '16 at 07:27
  • Naming columns with integers is asking for trouble downstream – Pierre L Apr 10 '16 at 09:30
  • This was just an example, the final state is for `exampleVector` to be UUIDs. – JZeolla Apr 10 '16 at 16:40

1 Answers1

0

Since your exampleVector is numeric, you should convert it to characters when you enter it into the code. Otherwise it will be interpreted as a selection based on indexes.

exampleVector <- 1:6
exampleDF <- data.frame(First=1:4, Second=4:7,Third=7:10)


exampleDF[as.character(exampleVector)] <- NA_character_

Note that there's no restriction in this setup that protects you against getting a data frame with the same name occurring several times. That might create problems later on (if you want to subset your data frame by names), so I would have added a sanity check to ensure that you do get unique names.

  • There we go - I was trying `exampleDF[exampleVector] <- as.character(NA)` and getting into all kinds of trouble. I didn't realize that it would take `exampleVector` and treat it as an index because it's an integer vector. – JZeolla Apr 10 '16 at 16:48