比较列中的值

发布于 2025-01-24 02:10:30 字数 726 浏览 0 评论 0原文

请查看以下数据框架作为可再现的示例:

df <- data.frame(Last_year = c('2013', '2020', '2017', '2015', '2016', '2021'), 
year = c('2021', '2020', '2019', '2018', '2017', '2016'))

我想比较列中的值,并在值不同的情况下丢弃行,并丢弃行。 last_year&lt;年

这是我想到的代码:

for(i in 1:nrow(df)){
    if((df1$Last_year[i] != df1$year[i] && df1$Last_year[i] < df1$year[i]) | 
         is.na(df1$year[i]))
         {df <- df[-i,]}
    else 
         next}

我不明白为什么,此代码不会消除所有last_year&lt;一年..你能发现原因吗?

我希望获得的最终数据框架是:

df <- data.frame(Last_year = c('2020', '2021'), 
year = c('2020', '2016'))

与第二个和最后一个值相对应,这是满足我的愿望的一个值 - &gt; last_year&gt;年

please look at the following dataframe as a reproducible example:

df <- data.frame(Last_year = c('2013', '2020', '2017', '2015', '2016', '2021'), 
year = c('2021', '2020', '2019', '2018', '2017', '2016'))

I want to compare the values in the columns and discard the row if the value is different & Last_year<year.

This is the code I come up with:

for(i in 1:nrow(df)){
    if((df1$Last_year[i] != df1$year[i] && df1$Last_year[i] < df1$year[i]) | 
         is.na(df1$year[i]))
         {df <- df[-i,]}
    else 
         next}

I cannot understand why, this code does not eliminate all the last_year < year.. can you spot the reason?

The final dataframe I wish to obtain is:

df <- data.frame(Last_year = c('2020', '2021'), 
year = c('2020', '2016'))

which correspond to the second and the last values, which are the one that satisfy my wish --> Last_year > year

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬心 2025-01-31 02:10:30

与 @Maël的贡献类似,似乎您想保持last_year超过一年的行:

df[df$Last_year > df$year, ]

如果您想避免NAS,则可以使用wher()

df[which(df$Last_year < df$year), ]

Similar to @Maël's contribution, it seems you want to keep rows which last_year is greater than year:

df[df$Last_year > df$year, ]

If you want to avoid NAs, you can use which():

df[which(df$Last_year < df$year), ]
幼儿园老大 2025-01-31 02:10:30

您根本不需要循环。您只需用简单的filter语句从dplyr替换for循环,也可以使用其他循环使用基本R。

library(dplyr)
    
df %>%
  filter(Last_year >= year  & !is.na(year))

或使用子集从基本R:

subset(df, Last_year >= year  & !is.na(year))

输出

  Last_year year
1      2020 2020
2      2021 2016

数据

df <- structure(list(Last_year = c("2013", "2020", "2017", "2015", 
"2016", "2021", "2022"), year = c("2021", "2020", "2019", "2018", 
"2017", "2016", "NA")), class = "data.frame", row.names = c(NA, 
-7L))

You don't need a for loop at all. You can just replace the for loop with a simple filter statement from dplyr or use base R as provided by the others.

library(dplyr)
    
df %>%
  filter(Last_year >= year  & !is.na(year))

Or use subset from base R:

subset(df, Last_year >= year  & !is.na(year))

Output

  Last_year year
1      2020 2020
2      2021 2016

Data

df <- structure(list(Last_year = c("2013", "2020", "2017", "2015", 
"2016", "2021", "2022"), year = c("2021", "2020", "2019", "2018", 
"2017", "2016", "NA")), class = "data.frame", row.names = c(NA, 
-7L))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文