如何用 R dataframe/data.table 展开单列,

How to unfold a single column with R dataframe/data.table,

我有以下 R data.table(尽管我很高兴为这个小数据集使用数据框)

library(data.table)

dt = readcsv(...)
head(dt)

   column1 column2  column3  column4    column5   column6                                                                                                                                                                                                                                                                          
 1     5   456421   437141 1.044105     32935      893562                                                                                                                                                                                                                                                                         
 2    42   756152   714126 1.058850     45913     1470278                                                                                                                                                                                                                                                                         
 3    36   157817   150205 1.050677     14558      308022                                                                                                                                                                                                                                                                         
 4    20   181700   172716 1.052016     15077      354416       

我想将 column2column3 合并到一列中,保留它们的所有关联行,并使用二进制标签来跟踪这些值的原始来源。我想到的 data.table 格式如下:

   column1 combined column4   column5   column6   from_column_2                                                                                                                                                                                                                                                                  
 1     5   456421   1.044105  32935      893562   TRUE
 2     5   437141   1.044105  32935      893562   FALSE                                                                                                                                                                                                                                                
 3    42   756152   1.058850  45913     1470278   TRUE
 4    42   714126   1.058850  45913     1470278   FALSE                                                                                                                                                                                                                                                
 5    36   157817   1.050677  14558      308022   TRUE
 6    36   150205   1.050677  14558      308022   FALSE                                                                                                                                            
 ....      

我很困惑如何用 data.table 做到这一点。大多数操作是 "table-wide"。至于使用 dplyrtidyr,我不确定我是如何过滤任何东西的...

我会将其实现为:

library(tidyverse)
df = read_csv(...)

df %>%
  gather(from_col, combined, column2, column3) %>%
  mutate(from_column_2 = ifelse(from_col == "column2", T, F))

祝你好运!