0

I want to accomplish this:

df <- data.frame(val1 = c(10,11, 12, 13))
df2 <-  data.frame(c1 = 1, c2 = 2, c3 = 3, c4 = 4)
df <- data.frame(rep(df, NCOL(df2)))
df2 <- df2[rep(1, NROW(df)),]
df3 <- df + df2
df3
  val1 val1.1 val1.2 val1.3
1   11     12     13     14
2   12     13     14     15
3   13     14     15     16
4   14     15     16     17

First, I am wondering if there is an easier way to do it.

second.

a different df could be

h <- temp[EM.Names[[i]]]
    > head(h)
          MSCILATAM
    1  9.870000e-03
    2 -6.286546e-05
    3  1.035069e-02
    4  1.070432e-02
    5  5.072980e-03
    6  1.486852e-03

and a different df2 could be

> fixed.avg.yield.diff
    VENEZUELA          PERU      COLOMBIA        MEXICO        BRAZIL     ARGENTINA         CHILE 
 0.0037480080  0.0004009513  0.0034571043 -0.0014477117  0.0280813115  0.0006466359 -0.0000884484 
> class(h)
[1] "data.frame"
> > class(fixed.avg.yield.diff)
[1] "numeric"

In this situation, the above solution will not work. The reason is if I try to convert "fixed.avg.yield.diff" to a data.frame it is structured like this:

 > data.frame(fixed.avg.yield.diff)
              fixed.avg.yield.diff
    VENEZUELA         0.0037480080
    PERU              0.0004009513
    COLOMBIA          0.0034571043
    MEXICO           -0.0014477117
    BRAZIL            0.0280813115
    ARGENTINA         0.0006466359
    CHILE            -0.0000884484
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
user3385769
  • 161
  • 6
  • 16
  • I'm very confused by your question, but are you just looking to merge data frames? – maloneypatr Dec 14 '14 at 18:48
  • thanks @maloneypatr. I edited above. For some reason I just cannot get the hang of just doing simple arithmetic "matrix" operations on data.frames which I often fine my self needing to do. – user3385769 Dec 14 '14 at 19:20
  • 1
    I think your methodology is fine, you can replace `df2 <- df2[rep(1, NROW(df)),]` with `df2 <- matrix(rep(df2, NROW(df)), byrow = TRUE, nrow= NROW(df))` if you have a numeric vector instead of a data frame in `df2` – David Arenburg Dec 14 '14 at 20:54

1 Answers1

0

Something like this??

outer(unlist(df),unlist(df2),"+")
#       c1 c2 c3 c4
# val11 11 12 13 14
# val12 12 13 14 15
# val13 13 14 15 16
# val14 14 15 16 17

Using your other example,

outer(unlist(h),unlist(fixed.avg.yield.diff),"+")
#              VENEZUELA         PERU    COLOMBIA        MEXICO     BRAZIL    ARGENTINA         CHILE
# MSCILATAM1 0.013618008 0.0102709513 0.013327104  0.0084222883 0.03795131 0.0105166359  0.0097815516
# MSCILATAM2 0.003685143 0.0003380858 0.003394239 -0.0015105772 0.02801845 0.0005837704 -0.0001513139
# MSCILATAM3 0.014098698 0.0107516413 0.013807794  0.0089029783 0.03843200 0.0109973259  0.0102622416
# MSCILATAM4 0.014452328 0.0111052713 0.014161424  0.0092566083 0.03878563 0.0113509559  0.0106158716
# MSCILATAM5 0.008820988 0.0054739313 0.008530084  0.0036252683 0.03315429 0.0057196159  0.0049845316
# MSCILATAM6 0.005234860 0.0018878033 0.004943956  0.0000391403 0.02956816 0.0021334879  0.0013984036
jlhoward
  • 58,004
  • 7
  • 97
  • 140