@Fernando pretty much has the idea right: your "row" should be a list
if you want to combine it with a data.frame
.
Here are a couple of examples:
## A sample `data.frame`
mydf <- data.frame(A = c("one", "two"), B = 1:2, C = 3:4,
D = 5:6, stringsAsFactors = FALSE)
## Your initial vector...
addMe <- c(1, 10, 12)
## ... now as a list
addMe <- c("name", as.list(addMe))
addMe
# [[1]]
# [1] "name"
#
# [[2]]
# [1] 1
#
# [[3]]
# [1] 10
#
# [[4]]
# [1] 12
#
## Put it all together...
new <- rbind(mydf, addMe)
new
# A B C D
# 1 one 1 3 5
# 2 two 2 4 6
# 3 name 1 10 12
str(new)
# 'data.frame': 3 obs. of 4 variables:
# $ A: chr "one" "two" "name"
# $ B: num 1 2 1
# $ C: num 3 4 10
# $ D: num 5 6 12
Alternatively, create a one row (or more, if necessary) data.frame
and bind that with the existing data.frame
:
addMe <- c(1, 10, 12)
rbind(mydf, setNames(cbind("name", data.frame(t(addMe))), names(mydf)))
# A B C D
# 1 one 1 3 5
# 2 two 2 4 6
# 3 name 1 10 12
Major note: This depends on your first column being a character, not factor, variable. If they are factors, you need to make sure the variable contains the right levels
(or convert to character first, then re-factor).