大家好,我需要用相邻列中的字符替换数据框中的字符: 因此,我想用列族中正确的姓氏替换列属中的字符“ uncultured”。我为您提供了一些示例数据,尽管我的真实数据中有1300多个列,但这些列仍是我的样子。 我可以用合适的家族替换列属中NA值的地方,但后来我陷入困境:
molekc$genus[is.na(molekc$genus)] <- molekc$family[is.na(molekc$genus)]
family <- c("choreo","Nassophorea","choreo","Phyllopharyngea","Choreo","Oligo")
genus <- c("choreo","uncultured","choreo","uncultured","Pithites","Tintinnopsis")
stack <- data.frame(family,genus, stringsAsFactors = FALSE)
答案 0 :(得分:1)
这是ifelse的解决方案:
stack$genus <- with(stack, ifelse(genus =='uncultured', family, genus))
或者,您可以对NA和“未培养的”都这样做:
stack$genus <- with(stack, ifelse(genus =='uncultured' | is.na(genus), family, genus))