Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205399

How do I make column values into new columns in data table?

$
0
0

I have a table that looks like this:

> dt
                 variant_id           transcript_id is_NL counts nrows
  1: chr10_60842447_A_G_b38 chr10_60871326_60871443     0  32968   685
  2: chr10_60842447_A_G_b38 chr10_60871326_60871443     1   1440    20
  3: chr10_60842447_A_G_b38 chr10_60871326_60871443     2    337     1
  4: chr10_60846892_G_A_b38 chr10_60871326_60871443     0  33157   690
  5: chr10_60846892_G_A_b38 chr10_60871326_60871443     1   1251    15
 ---                                                                  
227:  chr5_96832353_G_T_b38  chr5_96727531_96729611     1  33504   572
228:  chr5_96832353_G_T_b38  chr5_96727531_96729611     2   3352    52
229:  chr5_96834213_T_G_b38  chr5_96727531_96729611     0 110144  2208
230:  chr5_96834213_T_G_b38  chr5_96727531_96729611     1  33252   564
231:  chr5_96834213_T_G_b38  chr5_96727531_96729611     2   3352    52

I want to take the values of is_NL and make them into separate columns (e.g. is_NL_0, is_NL_1, is_NL_2), and, for now, fill them with the values from counts and nrows semi-colon separated (e.g. 32968;685). I've been using tidyr's pivot_wider to do this but, because I'm inexperienced with this package, I've been having a little trouble:

> dt %>% pivot_wider(-c(transcript_id, variant_id), names_from = "is_NL", values_from = paste0(dt$counts, ";", dt$nrows), names_prefix = "NL_") %>% as.data.table
Error: Unknown columns `32968;685`, `1440;20`, `337;1`, `33157;690`, `1251;15` and ... 
Run `rlang::last_error()` to see where the error occurred.

I'm going to keep working on this but would like to know how I could do this in a way that would make sense.


Viewing all articles
Browse latest Browse all 205399

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>