Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R pivot_在矩阵列和正则向量列的混合上更长_R_Tidyr_Tibble - Fatal编程技术网

R pivot_在矩阵列和正则向量列的混合上更长

R pivot_在矩阵列和正则向量列的混合上更长,r,tidyr,tibble,R,Tidyr,Tibble,我有一个tibble,其中一些列是矩阵。下面是一个玩具示例: library(dplyr) library(tidyr) dat <- structure(list(id = 0:5, matrix_column = structure(c(-1.34333431222985, -1.54123232044003, -1.7260282725816, -1.89244

我有一个
tibble
,其中一些列是矩阵。下面是一个玩具示例:

library(dplyr)
library(tidyr)

dat <- structure(list(id = 0:5, matrix_column = structure(c(-1.34333431222985, 
                                                            -1.54123232044003, -1.7260282725816, -1.8924463753132, -2.0376516335872, 
                                                            -2.16069643164938, -0.250406602741403, -0.287716094522968, -0.32269823315914, 
                                                            -0.354360193430544, -0.382155662949252, -0.405883260458378, 1.53709630050992, 
                                                            1.76715755374983, 1.98313378488307, 2.17881959842109, 2.35072520728221, 
                                                            2.4974704619887), .Dim = c(6L, 3L)), vector_column = c(10.453112322311, 
                                                                                                                   10.3019556236512, 10.1273409693709, 9.91474471968391, 9.65093549479026, 
                                                                                                                   9.32601906868098)), row.names = c(NA, -6L), class = c("tbl_df", 
                                                                                                                                                                         "tbl", "data.frame"))

如果我将
pivot\u longer
tidyr
应用到非
id
列,则
vector\u列中的值将被复制,以填充容纳
矩阵\u列所需的另外两列

dat %>% 
  pivot_longer(cols = -id, values_to = "new_column")

# A tibble: 12 x 3
      id name          new_column[,1]   [,2]  [,3]
   <int> <chr>                  <dbl>  <dbl> <dbl>
 1     0 matrix_column          -1.34 -0.250  1.54
 2     0 vector_column          10.5  10.5   10.5 
 3     1 matrix_column          -1.54 -0.288  1.77
 4     1 vector_column          10.3  10.3   10.3 
 5     2 matrix_column          -1.73 -0.323  1.98
 6     2 vector_column          10.1  10.1   10.1 
 7     3 matrix_column          -1.89 -0.354  2.18
 8     3 vector_column           9.91  9.91   9.91
 9     4 matrix_column          -2.04 -0.382  2.35
10     4 vector_column           9.65  9.65   9.65
11     5 matrix_column          -2.16 -0.406  2.50
12     5 vector_column           9.33  9.33   9.33

我的真实数据有几十个矩阵列和向量列。

如果继续使用当前的数据格式(将dataframe和matrix放在一起),您将继续遇到使用它的麻烦。我建议将矩阵转换为dataframe,并将它们添加为各自的列

library(dplyr)
library(tidyr)

dat$matrix_column %>%
  data.frame() %>%
  bind_cols(dat %>% select(-matrix_column)) %>%
  pivot_longer(cols = -id, values_to = "new_column")

#      id name          new_column
#   <int> <chr>              <dbl>
# 1     0 X1                -1.34 
# 2     0 X2                -0.250
# 3     0 X3                 1.54 
# 4     0 vector_column     10.5  
# 5     1 X1                -1.54 
# 6     1 X2                -0.288
# 7     1 X3                 1.77 
# 8     1 vector_column     10.3  
# 9     2 X1                -1.73 
#10     2 X2                -0.323
# … with 14 more rows
库(dplyr)
图书馆(tidyr)
dat$matrix_列%>%
data.frame()%>%
绑定列(数据%>%select(-matrix列))%%>%
pivot_更长(cols=-id,value_to=“new_column”)
#id名称新列
#                  
#10x1-1.34
#20x2-0.250
#30x31.54
#4.0向量_列10.5
#51x1-1.54
#61x2-0.288
#71x31.77
#8.1向量_第10.3列
#9.2 X1-1.73
#102x2-0.323
#…还有14行
# A tibble: 12 x 3
      id name          new_column[,1]   [,2]  [,3]
   <int> <chr>                  <dbl>  <dbl> <dbl>
 1     0 matrix_column          -1.34 -0.250  1.54
 2     0 vector_column          10.5      NA    NA 
 3     1 matrix_column          -1.54 -0.288  1.77
 4     1 vector_column          10.3      NA    NA 
library(dplyr)
library(tidyr)

dat$matrix_column %>%
  data.frame() %>%
  bind_cols(dat %>% select(-matrix_column)) %>%
  pivot_longer(cols = -id, values_to = "new_column")

#      id name          new_column
#   <int> <chr>              <dbl>
# 1     0 X1                -1.34 
# 2     0 X2                -0.250
# 3     0 X3                 1.54 
# 4     0 vector_column     10.5  
# 5     1 X1                -1.54 
# 6     1 X2                -0.288
# 7     1 X3                 1.77 
# 8     1 vector_column     10.3  
# 9     2 X1                -1.73 
#10     2 X2                -0.323
# … with 14 more rows