我有两个数据框:
harv<-structure(list(spp = c("Other species (please list species name; i.e. Tarpon)",
"Other species (please list species name; i.e. Amberjack)", "Red Drum (a.k.a. Redfish or Red)",
"Other species (please list species name; i.e. Tarpon)", "Other species (please list species name; i.e. Amberjack)",
"Red Drum (a.k.a. Redfish or Red)", "Other species (please list species name; i.e. Tarpon)"
), number = c(1, 2, 2, 4, 5, 7, 9)), class = "data.frame", row.names = c(NA,
-7L))
oth<-structure(list(spp = c("Tink", "Chii", "Blue", "Red", "Blue")), class = "data.frame", row.names = c(NA,
-5L))
我想迭代循环,以便每次 spp 名称包含“Other”时,它都会按照
oth
数据框的顺序替换它。这是 harv
数据框的样子:
spp number
1 Other species (please list species name; i.e. Tarpon) 1
2 Other species (please list species name; i.e. Amberjack) 2
3 Red Drum (a.k.a. Redfish or Red) 2
4 Other species (please list species name; i.e. Tarpon) 4
5 Other species (please list species name; i.e. Amberjack) 5
6 Red Drum (a.k.a. Redfish or Red) 7
7 Other species (please list species name; i.e. Tarpon) 9
当我运行这个循环时:
for (i in seq_along(oth$spp)) {
# Find indices where "Other" is present in spp
indices <- grep("Other", harv$spp)
# Replace spp values at the found indices with oth$spp[i]
harv$spp[indices] <- oth$spp[i]
}
它为我提供了一个数据框,仅提取 oth$spp[i] 向量中的名称
"Tink"
。数据框如下所示:
spp number
1 Tink 1
2 Tink 2
3 Red Drum (a.k.a. Redfish or Red) 2
4 Tink 4
5 Tink 5
6 Red Drum (a.k.a. Redfish or Red) 7
7 Tink 9
如何创建一个循环,以便每个“其他”
harv$spp
依次替换为 oth
向量中的内容?
我想获得一个如下所示的数据框,将每个“其他”替换为
oth
数据框中的内容,顺序与该数据框相同:
spp number
1 Tink 1
2 Chii 2
3 Red Drum (a.k.a. Redfish or Red) 2
4 Blue 4
5 Red 5
6 Red Drum (a.k.a. Redfish or Red) 7
7 Blue 9
无需循环——在基 R 中执行此操作所需的函数是向量化,这意味着该函数将对向量的所有元素进行操作,而无需一次循环并作用于每个元素:
idx <- which(startsWith(harv$spp, "Other"))
harv$spp[idx] <- oth$spp[seq_along(idx)]
还要小心在这里使用
grep
,它采用正则表达式来匹配字符串模式(也许这就是您想要做的)。按照您编写的方式,这将与字符串中任何位置的“Other”匹配。