質問

I have a series of data short datasets (2-6rows, 9cols) (dat1,dat2,dat3...dat1000) that all have the same structure and I am trying to put them together in ascending order by name into a single data frame with rbind. Here is a small sample of what I am using and what I am trying to create.

#create sample data sets
    set.seed(1)
    dat1 = data.frame( A = (1:2),B = rnorm(2, mean = 0.03,sd = 0.01))
    dat2 = data.frame( A = (1:3),B = rnorm(3, mean = 0.05,sd = 0.01))
    dat3 = data.frame( A = (1:4),B = rnorm(4, mean = 0.05,sd = 0.01))
    dat4 = data.frame( A = (1:8),B = rnorm(8, mean = 0.05,sd = 0.01))

#put them into a single data frame, this is an example of the data frame I would like to end up with

    rbind(dat1,dat2,dat3,dat4)

my question is how can I bind the data frames together with a loop or counting device so I don't have to write out each variable in the rbind(dat1,dat2...dat1000).

役に立ちましたか?

解決

Here's one alternative. Using the data in your example

> do.call(rbind, lapply(paste("dat", 1:4, sep=""), get))
    A          B
1  1 0.02373546
2  2 0.03183643
3  1 0.04164371
4  2 0.06595281
5  3 0.05329508
6  1 0.04179532
7  2 0.05487429
8  3 0.05738325
9  4 0.05575781
10 1 0.04694612
11 2 0.06511781
12 3 0.05389843
13 4 0.04378759
14 5 0.02785300
15 6 0.06124931
16 7 0.04955066
17 8 0.04983810

For your full case use:

do.call(rbind, lapply(paste("dat", 1:1000, sep=""), get))

他のヒント

Use data.table::rbindlist.

It needs each of your datasets to be a data.table, and an element in a list. The example below should help.

eg.

DT1 = data.table(A=1:3,B=letters[1:3])
DT2 = data.table(A=4:5,B=letters[4:5])
l = list(DT1,DT2)
rbindlist(l)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top