cross-product data frames

I was wondering if there is a way to take a “cross-product” from data frames with non-numeric entries. I have a one-column df.RICS data frame with rows as records (length 235) and another df.dates with dates as records (length 3004). I need a data frame with each date matched with each row in df.RICS:

dates 1 2004-04-23 2 2004-04-24 3 2004-04-25 4 2004-04-26 5 2004-04-27 6 2004-04-28 7 2004-04-29 8 2004-04-30 9 2004-05-01 10 2004-05-02 RICS 1 AA.N 2 AAP 3 AAP.N 4 AAPL.O 5 ABGL.L 

Is it possible to use expand.grid (df.RICS, df.dates) without converting to numeric values?

Now all I have is:

 > expand.grid(datesAsVec, RICSAsVec, stringsAsFactors = TRUE) Var1 Var2 1 2004-04-23 AA.N Warning message: In format.data.frame(x, digits = digits, na.encode = FALSE) : corrupt data frame: columns will be truncated or padded with NAs 
+1
source share
2 answers
  • If your operation works with character data, you can convert data.frames to character matrices, outer works on them.

     > df <- data.frame (a = LETTERS [1:3], b = 4:6, c = Sys.Date ()) > df abc 1 A 4 2012-06-01 2 B 5 2012-06-01 3 C 6 2012-06-01 > sapply (df, as.character) abc [1,] "A" "4" "2012-06-01" [2,] "B" "5" "2012-06-01" [3,] "C" "6" "2012-06-01" > class (sapply (df, as.character)) [1] "matrix" 
  • If the function is vectorized, instead of using outer you can also appropriately repeat these two words and directly use this function.

  • If the function does not work on the symbol, it will be more difficult ... But you could take a look at POSIXct .

+1
source

You are looking for expand.grid

+2
source

Source: https://habr.com/ru/post/1415743/


All Articles