我的数据如下:
V1 V2 V3 V4 V5 V6 V7
1: chr11 9554978 9554979 rs114731977 0 + ENSG00000133789
2: chr11 9554978 9554979 rs374765884 0 + ENSG00000133789
3: chr11 9554977 9554979 rs796467675 0 + ENSG00000133789
4: chr11 9554976 9554979 rs796841057 0 + ENSG00000133789
5: chrX 15148554 15148555 rs2317411 0 - ENSG00000130150
6: chrX 15148775 15148776 rs2317410 0 - ENSG00000130150
...
我想要得到的是(例如此示例)两个文件,每个文件包含一组来自$ V4的rs值,以表示$ V7中的唯一值
像这样:
ENSG00000133789.txt:
rs114731977
rs374765884
rs796467675
rs796841057
ENSG00000130150.txt:
rs2317411
rs2317410
一些我试图获得这些值的东西:
data%>%group_by(V7) %>% summarise()
distinct_df = data %>% distinct(V7) %>% select(V7)
他们只给我$ 7列
我也尝试过:
awk -F' ' 'FNR==NR{a[$7]++;next} a[$7]==1' SG_ALL SG_ALL >ss
SG_ALL是数据所在的位置,输入文件,我两次读取,第一次读取行数并将其存储在数组中,第二次执行条件,如果计数为1,则应打印行,然后仅打印< / p>
但是在输出中,我只得到一行:
chr13 41545705 41545706 rs12429969 0 + ENSG00000150907
答案 0 :(得分:0)
这解决了我的问题:
awk -F'''{f = $ 7“ .txt”;打印$ 4 >> f; close(f)}'OFS =''SG_ALL
答案 1 :(得分:0)
以下是使用tidyverse
的选项:
library(tidyverse)
# Generate a data frame containing V4 and V7
df <- data.frame(
V4 = paste0(
'rs',
str_pad(
sample(1:10e6, 3),
width = 6,
side = 'left',
pad = '0'
)
),
V7 = rep(
paste0(
'ENSG',
str_pad(
sample(1:5000, 4),
width = 10,
side = 'left',
pad = '0'
)
),
12
)
)
# Generate unique V7
unique_V7 <- df %>% select(
V7
) %>% unique() %>% pull()
# Write V4 for each (unique) V7
for (cur_V7 in unique_V7) {
df %>% filter(
V7 == cur_V7
) %>% select(
V4
) %>% write_csv(
path = paste0(
cur_V7, '.txt' # filename
),
col_names = FALSE
)
}