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 (eg is_NL_0
, is_NL_1
, is_NL_2
), and, for now, fill them with the values from counts
and nrows
semi-colon separated (eg 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.
Not familiar with tidyr
but you could do:
dt[, tmp := paste(counts, nrows, sep = ";")
][, dcast(.SD, transcript_id + variant_id ~ is_NL, value.var = "tmp")]
transcript_id variant_id 0 1 2
1: chr10_60871326_60871443 chr10_60842447_A_G_b38 32968;685 1440;20 337;1
2: chr10_60871326_60871443 chr10_60846892_G_A_b38 33157;690 1251;15 <NA>
3: chr5_96727531_96729611 chr5_96832353_G_T_b38 <NA> 33504;572 3352;52
4: chr5_96727531_96729611 chr5_96834213_T_G_b38 110144;2208 33252;564 3352;52
Data
library(data.table)
dt <- fread(" variant_id transcript_id is_NL counts nrows
chr10_60842447_A_G_b38 chr10_60871326_60871443 0 32968 685
chr10_60842447_A_G_b38 chr10_60871326_60871443 1 1440 20
chr10_60842447_A_G_b38 chr10_60871326_60871443 2 337 1
chr10_60846892_G_A_b38 chr10_60871326_60871443 0 33157 690
chr10_60846892_G_A_b38 chr10_60871326_60871443 1 1251 15
chr5_96832353_G_T_b38 chr5_96727531_96729611 1 33504 572
chr5_96832353_G_T_b38 chr5_96727531_96729611 2 3352 52
chr5_96834213_T_G_b38 chr5_96727531_96729611 0 110144 2208
chr5_96834213_T_G_b38 chr5_96727531_96729611 1 33252 564
chr5_96834213_T_G_b38 chr5_96727531_96729611 2 3352 52")
This should work fine for you case.
library(tidyverse)
df_example <- tibble::tribble(~variant_id,~transcript_id, ~is_NL, ~counts, ~ nrows,
"chr10_60842447_A_G_b38", "chr10_60871326_60871443", 0, 32968, 685,
"chr10_60842447_A_G_b38", "chr10_60871326_60871443", 1 , 1440 , 20,
"chr10_60842447_A_G_b38" ,"chr10_60871326_60871443", 2, 337 , 1,
"chr10_60846892_G_A_b38" ,"chr10_60871326_60871443", 0 , 33157 ,690,
"chr10_60846892_G_A_b38" ,"chr10_60871326_60871443", 1 , 1251 ,15)
df_example %>%
mutate(counts = counts %>% as.character(),
nrows = nrows %>% as.character()) %>%
unite("result",counts,nrows,sep = ";") %>%
pivot_wider(names_from = is_NL,values_from = result)
# A tibble: 2 x 5
variant_id transcript_id `0` `1` `2`
<chr> <chr> <chr> <chr> <chr>
1 chr10_60842447_A_G_b38 chr10_60871326_60871443 32968;685 1440;20 337;1
2 chr10_60846892_G_A_b38 chr10_60871326_60871443 33157;690 1251;15 NA
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.