在数据帧的行中向字符串添加增量计数器

时间:2017-08-28 10:26:05

标签: r

我想为所有"未知"添加一个计数器。数据框中的字符串。 这是数据框

structure(list(Phylum = structure(c(1L, 2L, 2L, 2L, 2L, 2L), .Label = c("Acidobacteria", 
"Actinobacteria"), class = "factor"), Class = structure(c(1L, 
2L, 2L, 2L, 2L, 2L), .Label = c("Acidobacteria bacterium RIFCSPLOWO2_02_FULL_64_15", 
"Actinobacteria"), class = "factor"), Order = structure(c(3L, 
1L, 2L, 2L, 2L, 2L), .Label = c("Actinomycetales", "Corynebacteriales", 
"unknown"), class = "factor"), Family = structure(c(3L, 1L, 2L, 
2L, 2L, 2L), .Label = c("Actinomycetaceae", "Corynebacteriaceae", 
"unknown"), class = "factor"), Genus = structure(c(3L, 2L, 1L, 
1L, 1L, 1L), .Label = c("Corynebacterium", "Trueperella", "unknown"
), class = "factor"), Species = structure(c(1L, 1L, 1L, 1L, 1L, 
1L), .Label = "unknown", class = "factor"), Genecoverage = c(5.58715596330275, 
761.405303030303, 42.5656565656566, 91.910447761194, 63.1912223121422, 
87.9927983539095)), .Names = c("Phylum", "Class", "Order", "Family", 
"Genus", "Species", "Genecoverage"), row.names = c(NA, 6L), class = "data.frame")

对于第一行,我想替换所有" unknown"出现" unknown_1"。
对于第二行,替换所有" unknown"出现" unknown_2"等等...

你能帮忙吗?

感谢

1 个答案:

答案 0 :(得分:1)

我们可以通过a使用序列值

对非数字列和replace非数字列“未知”进行子集化
paste