我正在尝试编写一个循环遍历数据帧行并使用有关其他行的信息来确定每个循环的输出的函数。
请考虑以下数据框,该数据框用于表示具有经度坐标,纬度坐标以及代表他们是否生病的值的人:
game.mat<-as.data.frame(matrix(0, nrow = 100, ncol = 3))
colnames(game.mat)<-c("PosX","PosY","Sick")
game.mat[,"PosX"]<-sample(x = c(1:100), 100, replace = TRUE)
game.mat[,"PosY"]<-sample(x = c(1:100), 100, replace = TRUE)
game.mat[,"Sick"]<-sample((c(rep(0,8),rep(1,2))),100,replace=TRUE)
一些人将在基线时患病。我的功能是让感染了x-y坐标的人与患病的人(因此与患病的人相邻的人)感染。我考虑过将这样的函数嵌入ifelse语句中:
search_sick<-function(d,corx,cory){
d2<-d[d$PosX<corx+2&d$PosX>corx-2&d$PosY<cory+2&d$PosY>cory-2,]
if(sum(d2$Sick>0)){
d$Sick<-1
} else{
d$Sick<-0
}
}
但是它会使每个人都感到不适,也许是因为如果有人在患病的人旁边,它会给每个人1的值。我也考虑过使用apply函数。但是据我了解,apply只能一次在单个行中运行,因此将无法检索有关其他行是否具有相邻坐标值的信息。
我希望这是有道理的。乐意提供任何其他信息。
答案 0 :(得分:1)
下面是使用apply
set.seed(1)
game.mat<-as.data.frame(matrix(0, nrow = 100, ncol = 3))
colnames(game.mat)<-c("PosX","PosY","Sick")
game.mat[,"PosX"]<-sample(x = c(1:100), 100, replace = TRUE)
game.mat[,"PosY"]<-sample(x = c(1:100), 100, replace = TRUE)
game.mat[,"Sick"]<-sample((c(rep(0,8),rep(1,2))),100,replace=TRUE)
#plot the sick individuals in red
plot(PosY~PosX, data=game.mat, col=as.factor(Sick), pch=16)
我们将修改您的函数,使其具有灵活的搜索半径“ r”,并返回新感染个体的索引
search_sick<-function(d, corx, cory, r){
indx<-which(d$PosX<corx+r & d$PosX>corx-r & d$PosY<cory+r & d$PosY>cory-r)
}
contagious<-game.mat[game.mat$Sick==1,]
infected<-apply(contagious, 1, function(x) {
search_sick(game.mat, x[1], x[2], r=10)
})
game.mat$T1<-game.mat$Sick
game.mat$T1[unique(unlist(infected))]<-1
#circle points which have become sick
points(PosY~PosX, data=game.mat[game.mat$Sick==0 & game.mat$T1==1,], col="red", cex=2)