基于数据帧

时间:2017-12-22 17:30:53

标签: r dataframe vector conditional character

我有一个带有列的数据框,名为" mutation"。他们可能是SNP,如" C> A",插入如" + TTTAAG"或删除像" -CTTGA"。例如:

**position** **mutation**
1234           C > A
1452           +TTTAAG
2734           -CTTGA

我希望R在变异列中搜索特定字符(">"," +"或" - ")并写&# 34; SNP","插入"或"删除"分别进入dataframe中的一个新列,所以我希望得到以下结果:

**position** **mutation**  **mutation_type**
1234           C > A             SNP
1452           +TTTAAG         insertion
2734           -CTTGA           deletion

我试着做以下事情:

mutation_type <- rep(NA, length(df$position)))
df$mutation_type <- mutation_type #creating a new column with NAs

尝试:

while(grep(pattern = "-", df$mutation)){
  df$mutation_type <- "deletion"
}

只覆盖mutation_type列中的每个单元格。你能给我一个如何解决这个问题的建议吗?

1 个答案:

答案 0 :(得分:2)

使用# admin.py class UserForm(forms.ModelForm): class Meta: model = User fields = ('user_permissions',) class MyModelForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(MyModelForm, self).__init__(*args, **kwargs) self.user_form = UserForm(instance=self.instance and self.instance.django_user, prefix=self.prefix) class Meta: model = MyModel class MyModelAdmin(admin.ModelAdmin): form = MyModelForm change_form_template = 'my_model_change_form.html' # my_model_change_form.html {% extends "admin/change_form.html" %} {% block field_sets %} {% for fieldset in adminform %} {% include "admin/includes/fieldset.html" %} {% endfor %} <form>{{ adminform.form.user_form }}</form> {% endblock %} grep的解决方案:

ifelse