R:重新编码之前/之后的n个观测值

R:重新编码之前/之后的n个观测值,r,R,我有一个0/1虚拟变量的数据帧。每个虚拟变量只取值1一次。对于每一列,我想用值1替换从观测值开始计数的n个前/后观测值,使之成为一个特定值,比如1 对于单个向量,n=1: c(0, 0, 1, 0, 0) 我想得到 c(0, 1, 1, 1, 0) 使用n列并允许不同数量的前/后观测值替换(例如,n-1前和n后)的一般方法是什么 谢谢你的帮助 您可以执行以下操作: vec <- c(0, 0, 1, 0, 0) sapply(1:length(vec), function(i) {

我有一个0/1虚拟变量的数据帧。每个虚拟变量只取值1一次。对于每一列,我想用值1替换从观测值开始计数的n个前/后观测值,使之成为一个特定值,比如1

对于单个向量,n=1:

c(0, 0, 1, 0, 0)
我想得到

c(0, 1, 1, 1, 0)
使用n列并允许不同数量的前/后观测值替换(例如,n-1前和n后)的一般方法是什么


谢谢你的帮助

您可以执行以下操作:

vec <- c(0, 0, 1, 0, 0)

sapply(1:length(vec), function(i) {
  minval <- max(0, i - 1)
  maxval <- min(i + 1, length(vec))
  return(sum(vec[minval:maxval]))
})
# [1] 0 1 1 1 0

您可以执行以下操作:

vec <- c(0, 0, 1, 0, 0)

sapply(1:length(vec), function(i) {
  minval <- max(0, i - 1)
  maxval <- min(i + 1, length(vec))
  return(sum(vec[minval:maxval]))
})
# [1] 0 1 1 1 0
这可能是一个开始:

myv <- c(0, 0, 1, 0, 0)

#make a copy
res <- myv

#check where the ones are
test <- which(myv==1)

#create indices to be replaced

n=1 #variable n
replace_indices <- c(test+(1:n),test-(1:n))
#filter out negatives (may happen with larger n)
replace_indices <- replace_indices[replace_indices>0]
#replace items in 'res' that need to be replaced with 1

res[replace_indices] <- 1
res

    > res
    [1] 0 1 1 1 0
这可能是一个开始:

myv <- c(0, 0, 1, 0, 0)

#make a copy
res <- myv

#check where the ones are
test <- which(myv==1)

#create indices to be replaced

n=1 #variable n
replace_indices <- c(test+(1:n),test-(1:n))
#filter out negatives (may happen with larger n)
replace_indices <- replace_indices[replace_indices>0]
#replace items in 'res' that need to be replaced with 1

res[replace_indices] <- 1
res

    > res
    [1] 0 1 1 1 0
另一种选择:

f <- function(x, pre, post) {
  idx <- which.max(x)
  x[max(1, (idx-pre)):min(length(x), (idx+post))] <- 1
  x
}
样本数据:

df <- data.frame(x = c(0, 0, 1, 0, 0), y = c(0, 1, 0, 0, 0))
应用程序:

df[] <- lapply(df, f, pre=2, post=1)
#df
#  x y
#1 1 1
#2 1 1
#3 1 1
#4 1 0
#5 0 0
另一种选择:

f <- function(x, pre, post) {
  idx <- which.max(x)
  x[max(1, (idx-pre)):min(length(x), (idx+post))] <- 1
  x
}
样本数据:

df <- data.frame(x = c(0, 0, 1, 0, 0), y = c(0, 1, 0, 0, 0))
应用程序:

df[] <- lapply(df, f, pre=2, post=1)
#df
#  x y
#1 1 1
#2 1 1
#3 1 1
#4 1 0
#5 0 0

这可能是一个解决方案:

dat<-data.frame(x=c(0,0,1,0,0,0),y=c(0,0,0,1,0,0),z=c(0,1,0,0,0,0))
which_to_change<-data.frame(prev=c(2,2,1),foll=c(1,1,3))
for(i in 1:nrow(which_to_change)){
  dat[(which(dat[,i]==1)-which_to_change[i,1]):(which(dat[,i]==1)+which_to_change[i,2]),i]<-1
}

这可能是一个解决方案:

dat<-data.frame(x=c(0,0,1,0,0,0),y=c(0,0,0,1,0,0),z=c(0,1,0,0,0,0))
which_to_change<-data.frame(prev=c(2,2,1),foll=c(1,1,3))
for(i in 1:nrow(which_to_change)){
  dat[(which(dat[,i]==1)-which_to_change[i,1]):(which(dat[,i]==1)+which_to_change[i,2]),i]<-1
}

类似于as.numericfilterx,rep1,3,circular=TRUE。类似于as.numericfilterx,rep1,3,circular=TRUE。非常简单和快速的解决方案,但是,您缺少一个检查,例如:在向量x上运行代码,您可以使用这一行执行:x[max0,ind-1:minind+x,lengthx]非常简单和快速的解决方案,您缺少一个检查,例如:在向量x上运行代码,您可以使用此行执行此操作:x[max0,ind-1:minind+x,lengthx]