0

I have two different dataframes and they haven't any common column to connect them. Is there any way to create a new dataframe from two different dataframes without any common column? The one of dataframes contains days values.

Example of first dataframe:

 day
 2000-01-01 00:00:00
 2000-01-01 00:01:00
 2000-01-01 00:02:00

Example of the second data frame:

 price
  1
  2
  3

The results of new data frame:

     day              price
 2000-01-01 00:00:00    1
 2000-01-01 00:01:00    2
 2000-01-01 00:02:00    3

3 Answers3

2

What about data.frame? - :

 data.frame(day,price)

Here's an example. Two input data frames:

> a
    x y
1   1 a
2   2 b
3   3 c
4   4 d
5   5 e
6   6 f
7   7 g
8   8 h
9   9 i
10 10 j
> b
    v     w
1  14 FALSE
2   5  TRUE
3   8  TRUE
4   4  TRUE
5  17 FALSE
6   5  TRUE
7  15 FALSE
8  11 FALSE
9  15 FALSE
10  9  TRUE

And here's how data.frame works:

> data.frame(a,b)
    x y  v     w
1   1 a 14 FALSE
2   2 b  5  TRUE
3   3 c  8  TRUE
4   4 d  4  TRUE
5   5 e 17 FALSE
6   6 f  5  TRUE
7   7 g 15 FALSE
8   8 h 11 FALSE
9   9 i 15 FALSE
10 10 j  9  TRUE

Neat.

Glen_b
  • 7,883
  • 2
  • 37
  • 48
1

What about cbind?

dd1 = data.frame(x1 = runif(10), y1=runif(10))
dd2 = data.frame(x2 = runif(10), y2=runif(10))

So

dd = cbind(dd1, dd2)
csgillespie
  • 59,189
  • 14
  • 150
  • 185
0

Here's the data.table approach, which may be useful for larger data.frames:

library(data.table)
### if converting from data.frame:
df1 <- data.frame(a=c(1,2,3))
dt1 <- data.table(df1)
### or more simply:
dt1 <- data.table(a=c(1,2,3))
dt2 <- data.table(b=c(4,5,6))
dt3 <- data.table(dt1,dt2)
dt3

gives:

   a b
1: 1 4
2: 2 5
3: 3 6
dardisco
  • 5,086
  • 2
  • 39
  • 54