简体   繁体   English

来自其他向量列的新列表列,带有dplyr和rowwise

[英]New List Column from other vector columns with dplyr and rowwise

I have the below tibble from which I would like to create a 4th column which is the united vector from A, B & C. I understand that dplyr::unite() can do this creating a new character vector, but I'm looking to create a list column with vectors. 我有下面的tibble,我想创建第4列,它是来自A,B和C的联合载体。我明白dplyr :: unite()可以做到这一点创建一个新的角色向量,但我正在寻找使用向量创建列表列。

For now rowwise works, but does not keep the input tibble. 现在rowwise工作,但不保持输入tibble。 Any suggestions for keeping the columns A_Vector to C_Vector? 有关将A_Vector列保存到C_Vector的建议吗?

Here is the code: 这是代码:

library(tidyverse)

My_Data <- tibble(A_Vector = rnorm(10),
                  B_Vector = rnorm(10),
                  C_Vector = rnorm(10)) %>% 
           rowwise() %>% 
           do(Port_Weights = matrix(c(.$A_Vector,.$B_Vector,.$C_Vector),3,1))

And the outcome: 结果如下:

Source: local data frame [10 x 1]
Groups: <by row>

# A tibble: 10 x 1
    Port_Weights
 *        <list>
 1 <dbl [3 x 1]>
 2 <dbl [3 x 1]>
 3 <dbl [3 x 1]>
 4 <dbl [3 x 1]>
 5 <dbl [3 x 1]>
 6 <dbl [3 x 1]>
 7 <dbl [3 x 1]>
 8 <dbl [3 x 1]>
 9 <dbl [3 x 1]>
10 <dbl [3 x 1]>

This does not work: 这不起作用:

My_Data <- tibble(A_Vector = rnorm(10),
                  B_Vector = rnorm(10),
                  C_Vector = rnorm(10)) %>% 
  mutate(Port_Weights = rowwise() %>% do(matrix(c(.$A_Vector,.$B_Vector,.$C_Vector),3,1)))

The long version, which clearly does not make sense: 长版本,显然没有意义:

My_Data <- tibble(A_Vector = rnorm(10),
                  B_Vector = rnorm(10),
                  C_Vector = rnorm(10))

Data_Unite <-   My_Data %>% 
  rowwise() %>% 
  do(Port_Weights = matrix(c(.$A_Vector,.$B_Vector,.$C_Vector),3,1))

My_Data <- as.tibble(cbind(My_Data,Data_Unite))

But does provide the sought after result: 但确实提供了追捧的结果:

# A tibble: 10 x 4
      A_Vector   B_Vector   C_Vector  Port_Weights
 *       <dbl>      <dbl>      <dbl>        <list>
 1 -1.23504457 -0.3750408 -0.4214122 <dbl [3 x 1]>
 2 -0.90678699  0.5261914  1.1191229 <dbl [3 x 1]>
 3 -0.62944085  0.5995529  0.2096462 <dbl [3 x 1]>
 4  2.06171633  1.5399094  2.2972950 <dbl [3 x 1]>
 5  0.08761555  0.1424207 -1.4758585 <dbl [3 x 1]>
 6 -1.07334432 -1.9112787  0.4820864 <dbl [3 x 1]>
 7 -0.18655423 -1.3698855  0.6672621 <dbl [3 x 1]>
 8 -0.97961789 -0.8194373 -0.4158516 <dbl [3 x 1]>
 9  0.68112936 -1.9864507  1.0193449 <dbl [3 x 1]>
10  0.61455438  0.5885380 -1.0925312 <dbl [3 x 1]>

Data : 数据

library(tidyverse)

my_tibble <- tibble(A_Vector = rnorm(10),
                    B_Vector = rnorm(10),
                    C_Vector = rnorm(10))

To add a column to a data frame, use mutate instead of do , and use Map to loop through the three vectors in parallel and construct a matrix out of each row: 要向数据框添加列,请使用mutate而不是do ,并使用Map并行循环遍历这三个向量,并构造每行的矩阵:

my_tibble %>% 
   mutate(Port_Weights = Map(function(...) matrix(c(...), 3, 1), A_Vector, B_Vector, C_Vector))

# A tibble: 10 x 4
#      A_Vector   B_Vector    C_Vector  Port_Weights
#         <dbl>      <dbl>       <dbl>        <list>
# 1  0.62674726 -0.5432169 -1.66763618 <dbl [3 x 1]>
# 2 -0.47346722 -0.4436020 -1.04892634 <dbl [3 x 1]>
# 3  0.19059238 -1.6733052  2.79275828 <dbl [3 x 1]>
# 4 -0.23501873 -1.1664704 -0.19324676 <dbl [3 x 1]>
# 5  0.66552642 -1.3328070 -1.53575954 <dbl [3 x 1]>
# 6 -0.41251920 -0.2056882  1.66537220 <dbl [3 x 1]>
# 7  0.48396052  0.3968486  0.16110407 <dbl [3 x 1]>
# 8  0.43035213 -0.6433268  1.61640228 <dbl [3 x 1]>
# 9  0.06747126 -1.0146385 -0.47824193 <dbl [3 x 1]>
#10  0.79916411 -1.2349901 -0.05151402 <dbl [3 x 1]>

If the element doesn't have to be a matrix: 如果元素不必是矩阵:

my_tibble %>% mutate(Port_Weights = Map(c, A_Vector, B_Vector, C_Vector))

Which is equivalent to (with data.table::transpose ): 这相当于(使用data.table::transpose ):

my_tibble %>% mutate(Port_Weights = data.table::transpose(as.list(.)))

Since you are using the tidyverse , you can also consider the pmap function from the purrr package, which is part of the tidyverse . 由于您使用的是tidyverse ,您还可以考虑purrr包中的pmap函数,它是tidyverse一部分。

set.seed(123)

library(tidyverse)

My_Data <- tibble(A_Vector = rnorm(10),
                  B_Vector = rnorm(10),
                  C_Vector = rnorm(10))

My_Data2 <- My_Data %>%
  mutate(Port_Weights = pmap(.l = list(A_Vector, B_Vector, C_Vector),
                             .f = function(x, y, z) matrix(c(x, y, z), 3, 1)))

My_Data2
# A tibble: 10 x 4
      A_Vector   B_Vector   C_Vector  Port_Weights
         <dbl>      <dbl>      <dbl>        <list>
 1 -0.56047565  1.2240818 -1.0678237 <dbl [3 x 1]>
 2 -0.23017749  0.3598138 -0.2179749 <dbl [3 x 1]>
 3  1.55870831  0.4007715 -1.0260044 <dbl [3 x 1]>
 4  0.07050839  0.1106827 -0.7288912 <dbl [3 x 1]>
 5  0.12928774 -0.5558411 -0.6250393 <dbl [3 x 1]>
 6  1.71506499  1.7869131 -1.6866933 <dbl [3 x 1]>
 7  0.46091621  0.4978505  0.8377870 <dbl [3 x 1]>
 8 -1.26506123 -1.9666172  0.1533731 <dbl [3 x 1]>
 9 -0.68685285  0.7013559 -1.1381369 <dbl [3 x 1]>
10 -0.44566197 -0.4727914  1.2538149 <dbl [3 x 1]>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM