简体   繁体   English

R 循环遍历数据帧并使用条件迭代地创建新数据帧

[英]R looping through data frame & creating new data frame iteratively using a condition

I am looking for a method to loop through a data frame in R, removing the max value above a specific condition, creating a new data frame excluding that row, re-calculating the values & running iteratively until there are no more rows above a specific condition - with the output excluding all of the rows removed.我正在寻找一种方法来遍历 R 中的数据框,删除特定条件之上的最大值,创建一个不包括该行的新数据框,重新计算值并迭代运行,直到没有更多行高于特定条件条件 - output 不包括删除的所有行。 Here is a simple dataset (I need it step by step because the values change at each time of iteration with the data I work with).这是一个简单的数据集(我需要逐步使用它,因为每次迭代时值都会随着我使用的数据而变化)。 The condition for this example would be when df$ratio > 1.9.此示例的条件是 df$ratio > 1.9。

height <- c(100, 110, 105, 130, 160, 150, 140, 145)
bodymass <- c(60, 65, 66, 75, 90, 85, 70, 72)
df <- data.frame(height, bodymass)
df$ratio <- df$height / df$bodymass

For this data there would be the following kind of iterations;对于此数据,将有以下类型的迭代;

-> df$ratio <- df$height / df$bodymass
-> df$ratio > 1.9 #Condition
-> Calculate max df$ratio in df > 1.9 #first loop would remove 2.013
-> Create new df excluding that value #Now loop back to start 
-> df$ratio <- df$height / df$bodymass #Recalculate (This is really important)
-> df$ratio > 1.9 #Condition
-> Calculate max df$ratio in df > 1.9 #second loop would remove 2.000
-> Create new df excluding that value #Now loop back to start 
-> df$ratio <- df$height / df$bodymass #Recalculate (This is really important)
-> df$ratio > 1.9 #Condition - none left now so can exit the loop.
-> output df excluding values > 1.9.

The recalculation is the most important step in this really.重新计算确实是其中最重要的一步。 I can do this manually no problem but have datasets with thousands of rows so needs to be automatic.我可以手动执行此操作没问题,但有数千行的数据集,因此需要自动执行。 Any help would be greatly appreciated.任何帮助将不胜感激。 Thank you.谢谢你。

You can try using while loop:您可以尝试使用while循环:

#Calculate initial ratio
df$ratio <- df$height / df$bodymass

#Continue while loop till any value is above 1.9
while(any(df$ratio > 1.9)) {
  #Remove the row with max ratio
  df <- df[-which.max(df$ratio), ]
  #Recalculate ratio
  df$ratio <- df$height / df$bodymass
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM