R - apply / mapply - Transpose is Required

I have defined a R function that takes 2 strings as input, and returns 10 columns as output. With a sample function to explain the issue -

func.ret.10.cols = function(x,y) {
# process strings ... yada yada
.....
# process strings ... yada yada
return ( cbind(1,2,3,4,5,6,7,8,9,10))
}

Testing this with a Data Frame -

> df1
                                    V1                                   V2
1 741 zulu zulu street 71 15 41 510741 741 14 21 zulu zulu street 71 510742
2 741 zulu zulu street 71 15 41 510741 741 14 21 zulu zulu street 71 510742

> str(df1)
'data.frame':   2 obs. of  2 variables:
 $ V1: chr  "741 zulu zulu street 71 15 41 510741" "741 zulu zulu street 71 15 41 510741"
 $ V2: chr  "741 14 21 zulu zulu street 71 510742" "741 14 21 zulu zulu street 71 510742"
> 

Now when i call this function, i expect to get a output of 10 columns and 2 rows. However i always get 2 columns and 10 rows.

> apply(df1[,c('V1','V2')], 1, function(x) func.ret.10.cols(x[1],x[2]))
       1  2
 [1,]  1  1
 [2,]  2  2
 [3,]  3  3
 [4,]  4  4
 [5,]  5  5
 [6,]  6  6
 [7,]  7  7
 [8,]  8  8
 [9,]  9  9
[10,] 10 10
> mapply(function(x,y) func.ret.10.cols(x,y), df1$V1, df1$V2)
      741 zulu zulu street 71 15 41 510741 741 zulu zulu street 71 15 41 510741
 [1,]                                    1                                    1
 [2,]                                    2                                    2
 [3,]                                    3                                    3
 [4,]                                    4                                    4
 [5,]                                    5                                    5
 [6,]                                    6                                    6
 [7,]                                    7                                    7
 [8,]                                    8                                    8
 [9,]                                    9                                    9
[10,]                                   10                                   10

I also tried with as.data.frame(cbind(1,2,3....)), but still got the same. Is there something that i am missing.

Although its easy enough to use a t(apply(...), I just want to know what have i overlooked.

Thanks, Manish

Answers


Just have a look at the documentation (?apply)

If each call to FUN returns a vector of length n, then apply returns an array of dimension c(n, dim(X)[MARGIN]) if n > 1.

In other words: The number of rows of the resulting matrix is given by the length of the vectors in your function to be applied.


Need Your Help

Efficiently deploy multiple instances of same WAR ( different contexts, same container )

java tomcat glassfish jetty war

I have one WAR ( app.war ) and one container ( Tomcat, Jetty, Glassfish, whatever ).

use grep to output characters not in pattern

regex grep

I want to grep a file for a pattern but display another part of the line that is not in the pattern.