比较列中的值
请查看以下数据框架作为可再现的示例:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与 @Maël的贡献类似,似乎您想保持last_year超过一年的行:
如果您想避免NAS,则可以使用
wher()
:Similar to @Maël's contribution, it seems you want to keep rows which last_year is greater than year:
If you want to avoid NAs, you can use
which()
:您根本不需要循环。您只需用简单的
filter
语句从dplyr
替换for循环,也可以使用其他循环使用基本R。或使用
子集
从基本R:输出
数据
You don't need a for loop at all. You can just replace the for loop with a simple
filter
statement fromdplyr
or use base R as provided by the others.Or use
subset
from base R:Output
Data