dimoftest <- dim(test) # 获取数据维数
factoftest <- c() #定义factor数组
k <- 2 #定义你的那个变量的列k,你自己设定
#循环找出k列大于k-1列10%的序号,并将factor数组设为1,否则为0
for(i in 1:dimoftest){
if((test[i,k]- test[i,k-1])/test[i,k-1] >0.1) factoftest[i] = 1
else factoftest[i] = 2
}
factoftest #显示数组
#创建一个factor因子,标记为1,2,文本也设为1,2
newfactor <- factor(factoftest,levels = c(1,2), labels = c("1","2"))
#合并到数据中
test <- data.frame(test, newfactor)
#查看
test
不明白的话继续问
which语句Description
Give the TRUE indices of a logical object, allowing for array indices.
Usage
which(x, arr.ind = FALSE, useNames = TRUE)
arrayInd(ind, .dim, .dimnames = NULL, useNames = FALSE)