Reputation: 867
I have a dataframe with 6 columns which already has some data in it. Now I want to append rows to it with the first column being populated with all the items in a list, 2nd,3rd, and 4th columns having the same string value for all rows and the 5th and 6th having another same string value for all rows.
Example:
dataframe.ex <-data.frame( V1 = sample(c("a","b","c")), V2 = sample(c("x","y","z")), V3 = sample(c("he","she","it")), V4 = sample(c("A","B","C")), V5 = rep("a1",3), V6 = rep("m",3))
V1 V2 V3 V4 V5 V6
1 c y it B a1 m
2 b z she C a1 m
3 a x he A a1 m
I have a vector which contains 3 values
vector.addrows.ex <- c("d","e","f")
The values I want to be populated in columns V2:V4 for all the rows are the same ex. "etc"
.The final two columns would take the same values that are in the other rows.
So finally the dataframe would look like:
V1 V2 V3 V4 V5 V6
1 c y it B a1 m
2 b z she C a1 m
3 a x he A a1 m
4 d etc etc etc a1 m
5 e etc etc etc a1 m
6 f etc etc etc a1 m
Upvotes: 1
Views: 64
Reputation: 887951
We can do
d1 <- data.frame(setNames(as.list(rep(c('etc', 'a1', 'm'), c(3, 1, 1))),
names(dataframe.ex)[-1]), stringsAsFactors=FALSE)
rbind(dataframe.ex, cbind(V1=vector.addrows.ex , d1))
Upvotes: 3