Removing rows in a data frame based on multiple criteria in R with loop function

淺唱寂寞╮ 提交于 2021-02-17 03:34:49

问题


I hope that I have formatted my question correctly as this is my first time posting and fairly new to R. Below is a small sample of some athlete movement data that I am currently using. My dataset has about 18000 row items and hence wanted to format it with quick R code.

The sample data is attached herewith for your reference.

I would like to remove rows of the data frame based on multiple criteria like when selecting price for particular company, with particular product and MRP between a certain range only those rows should be there in the dataframe and all other rows not matching above conditions should be deleted.

I am trying to use the following code in R for getting the desired output but somehow it is showing error while executing.

filter(feb_raw,(feb_raw$Company.=="STL" & feb_raw$Product.=="N24" & feb_raw$MRP<=1360 & feb_raw$MRP>=1150))

Wondering how I can do this if it's possible with a loop function. Any help will be greatly appreciated.

Thanks in advance.


回答1:


I assume you're using filter from the dplyr package. To be sure, use dplyr::filter() instead of just filter(). Something I notice is that you have a period after some of your variable names. These don't appear in the screenshot of your data, so maybe that is the problem. Also, when using filter, you don't need feb_raw$ with each variable.

So, potentially corrected code might look like the following:

dplyr::filter(feb_raw, Company=="STL" & Product=="N24" & MRP<=1360 & MRP>=1150)


来源:https://stackoverflow.com/questions/60700551/removing-rows-in-a-data-frame-based-on-multiple-criteria-in-r-with-loop-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!