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.