Reputation: 341
I have three data set.
Set 1:
A B
23 12
34 81
13 45
11 23
set 2:
A B
.34 1.1
.5 2.0
.4 1.8
set 3:
A B
-23 5
-3 0.1
.8 -2.3
-.6 1.4
4 3.2
Now i have to create a data frame as the following type :
A B Type
23 12 1
34 81 1
.
.
.
11 23 1
.34 1.1 2
.5 2.0 2
.4 1.8 2
-23 5 3
.
.
.
4 3.2 3
My attempt:
set1 <- data.frame(A=c(23,34,13,11),B=c(12,81,45,23))
set2 <- data.frame(A=c(.34,.5,.4),B=c(1.1,2,1.8))
set3 <- data.frame(A=c(-23,-3,.8,-.6,4),B=c(5,.1,-2.3,1.4,3.2))
dat123 <- rbind(set1,set2,set3)
but i couldn't able to add the column Type
.
Upvotes: 1
Views: 701
Reputation: 89097
Like this:
l <- list(set1, set2, set3)
do.call(rbind, Map(data.frame, l, type = seq_along(l)))
Upvotes: 5
Reputation: 12005
set1 <- data.frame(A=c(23,34,13,11),B=c(12,81,45,23))
set2 <- data.frame(A=c(.34,.5,.4),B=c(1.1,2,1.8))
set3 <- data.frame(A=c(-23,-3,.8,-.6,4),B=c(5,.1,-2.3,1.4,3.2))
Type=rep(c(1,2,3), times=c(nrow(set1), nrow(set2), nrow(set3)))
dat123 <- cbind(rbind(set1,set2,set3), Type)
Upvotes: 3