[英]Using nested for-if-else on a data frame in R
I am trying to use an if-else loop nested in a for loop to go through a data frame and output a new data frame based on conditions from the first df. 我正在尝试使用嵌套在for循环中的if-else循环来遍历数据帧并根据来自第一个df的条件输出新的数据帧。
In this data frame I would like to compare each row N with row N+1, 在此数据帧中,我想将N行与N + 1行进行比较,
if column elements match in column 1 and 2 如果列元素在列1和2中匹配
and the difference between column values in columns 3 and 4 for row N and row N+1 are less than or equal to 1 并且第N行和第N + 1行的第3列和第4列的列值之差小于或等于1
then I would like to write a new row in the N+1 row spot 那么我想在N + 1行位置写一个新行
that has the same elements for col 1 and 2 as those in row N+1 第1行和第2行具有与N + 1行相同的元素
and the minimum value of column 3 when comparing N and N+1 for col 3 比较第3列的N和N + 1时的第3列的最小值
and the maximum value of column 4 for comparing N and N+1 for col 4 和第4列的最大值,用于比较col 4的N和N + 1
Example: 例:
aaa <- c(rep("cat",4), "dog", "dog")
bbb <- c("fit", rep("fat",2), rep("fat", 3))
ccc <- c(6,5,6,9,9,9)
ddd <- c(11,10,10,22,23,24)
df <- data.frame(aaa,bbb,ccc,ddd)
Go from this: 从此:
aaa bbb ccc ddd
cat fit 6 11
cat fat 5 10
cat fat 6 10
cat fat 9 22
dog fat 9 23
dog fat 9 24
To the desired output: 到所需的输出:
aaa bbb ccc ddd
cat fit 6 11
cat fat 5 10
cat fat 9 22
dog fat 9 24
My attempt is this: 我的尝试是这样的:
result <- data.frame()
for (i in c(1:as.numeric(nrow(df))-1)){
if(df[i,1] == df[i+1,1]
&
df[i,2] == df[i+1,2]
&
abs(df[i,3]-df[i+1,3]) <=1
&
abs(df[i,4]-df[i+1,4]) <=1)
{
result[i+1,] <- c(df[i,1],df[i,2],min(df[i,3],df[i+1,3]),max(df[i,4],df[i+1,4]))
result[i,] <- c(NA,NA,NA,NA)
} else {
result[i,] <- df[i,]
}
}
result
I figured it out. 我想到了。 Posting for closure:
发布关闭:
result <- df
for (i in c(1:as.numeric(nrow(df)-1))){
if(df[i,1] == df[i+1,1] &
df[i,2] == df[i+1,2] &
abs(df[i,3]-df[i+1,3])<=1 &
abs(df[i,4]-df[i+1,4])<=1) {
result[i+1,3] <- min(df[i,3],df[i+1,3])
result[i+1,4] <- max(df[i,4],df[i+1,4])
result[i,] <- c(NA,NA,NA,NA)
}
}
result <- na.omit(result)
result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.