我有一个庞大的数据集,其中包含来自不同人群的基因型信息。我想按人口对数据进行排序,但我不知道如何。

我想按“ pedigree_dhl”排序。我正在使用以下代码,但始终收到错误消息。

newdata <- project[pedigree_dhl == CCB133$*1,  ]


我的问题还在于,“谱系-dhl”包含各个基因型的所有名称。 ``pedigree-dhl''列中只有前7个字母是人群名称。在本示例中:CCB133。如何告诉R我要提取所有包含CCB133的列的数据?

  Allele1 Allele2      SNP_name gs_entry pedigree_dhl
1       T       T ZM011407_0151      656    CCB133$*1
2       T       T ZM009374_0354      656    CCB133$*1
3       C       C ZM003499_0591      656    CCB133$*1
4       A       A ZM003898_0594      656    CCB133$*1
5       C       C ZM004887_0313      656    CCB133$*1
6       G       G ZM000583_1096      656    CCB133$*1

最佳答案

您可能需要考虑grep,就像在Using regexp to select rows in R dataframe的答案中一样。适应您的数据:

df <- read.table(text="  Allele1 Allele2      SNP_name gs_entry pedigree_dhl
1       T       T ZM011407_0151      656    CCB133$*1
2       T       T ZM009374_0354      656    CCB133$*1
3       C       C ZM003499_0591      656    CCB133$*1
4       A       A ZM003898_0594      656    CCB133$*1
5       C       C ZM004887_0313      656    CCB133$*1
6       G       G ZM000583_1096      656    CCB133$*1", header=T)

# put into df1 all rows where pedigree_dhl starts with CCB133$
p1 <- 'CCB133$'
df1 <- subset(df, grepl(p1, pedigree_dhl) )


但是您的问题意味着您可能想要选择七个字母的名称,或者只是按照系谱名称对行进行排序,而将所有行放在一个排序的数据框中可能更容易。这三个操作:子设置,提取新列或排序都可以独立进行。

# If you want to create a new column based
# on the first seven letter of SNP_name (or any other variable)

df$SNP_7 <- substr(df$SNP_name, start=1, stop=7)

# If you want to order by pedigree_dhl
# then you don't need to select out the rows into a new dataframe

df <- df[ with(df, order(df$pedigree_dhl)), ]


所有这些可能都是显而易见的-我添加它们只是为了完整性。

关于r - 基于前7个字母的子集数据/提取数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10319369/

10-12 13:57
查看更多