0

I have 4 columns consisting of the following: stock identification number, date, price, and market cap. Is there a way for me to delete any stock in R (based on their identification number) if the value of their prices at any point in time is zero? Thank you very much.

I tried to write the following loop in R to filter out all of the assets with zero prices using this code:

for (i in 1:length(data$price)) {
  if (data$price[i]=0) {
    data <- filter(!row_number() %in% i)
  }
}

but it doesn't work.

Mark
  • 1
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 02 '23 at 16:12
  • 1
    Hey, welcome to Stack Overflow. Thanks for showing your current approach. We need a minimal reproducible example of the data and a desired output in order to give helpful advice. For example, now I dont know whether each stock ID is appearing only once. As far as I can see, you should have a look at the concept of vectorisation, and once you understand it, you may see that you will not need a `for` loop at all to do this filtering. Mostly, R is shockingly easy to use. Some helpful resources for filtering data is https://moderndive.com/3-wrangling.html – uke Feb 02 '23 at 16:13
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 02 '23 at 16:26

0 Answers0