I did some calculations in R and I want to produce it into excel like this

```
DATA1 DATA2
54.364 2.05
56.532
54.21
41.485
65.8745
54.0546
75.156
```

but instead is coming like this

```
DATA1 DATA2
54.364 2.05
56.532 2.05
54.21 2.05
41.485 2.05
65.8745 2.05
54.0546 2.05
75.156 2.05
```

My function to produce it in excel is

```
write.xlsx(c(data.frame(DATA1),data.frame(DATA2)))
```

Although `data1`

has values of 54.364, 56.532, 54.21, 41.485, 65.8745, 54.0546, 75.156 and `data2`

2.05

Excel has a rather bizarre “copy down” feature where it copies a function returning a scalar into every cell in the calling range. It appears that this is happening to you here.

One way to work round this is to use `Application.Caller`

at the top of the function that’s called directly. This returns a `Range`

object denoting the calling range. You can then pad your function return values with `#N/A`

. You do this by inserting `variant`

types into your array set to `VT_ERROR`

and the error vales set to `xlErrNa`

. You can use `CVErr(xlErrNa)`

to do that in one step. Padding with `#N/A`

matches what Excel does with oversized calling ranges for functions returning arrays.

### Answer：

Following code can also be used:

(using @akrun’s data in https://stackoverflow.com/questions/25547210/how-to-produce-this-order-in-r)

```
DATA1 <- c(54.364, 56.532, 54.21, 41.845, 65.8745, 54.0546, 75.156)
DATA2 <- 2.05
DATA3 <- c(2.2, 2.4, 2.32)
outdf = data.frame(data1=numeric(), data2=numeric(), data3=numeric())
for(i in 1:length(DATA1)) outdf[i,]=c(DATA1[i],0,0)
for(i in 1:length(DATA2)) outdf$data2[i]=DATA2[i]
for(i in 1:length(DATA3)) outdf$data3[i]=DATA3[i]
outdf
data1 data2 data3
1 54.3640 2.05 2.20
2 56.5320 0.00 2.40
3 54.2100 0.00 2.32
4 41.8450 0.00 0.00
5 65.8745 0.00 0.00
6 54.0546 0.00 0.00
7 75.1560 0.00 0.00
```

Then you can use outdf with write.xlsx .

Tags: rexcel