0

I have a df named "crimes" with mutiple observations and columns(for instance: category, id, month, street_id, street_name) and I have to create a new df named "anti_social_behavior" where I should add the columns id, month and street_name from "crimes" when the category of the observation is "anti-social-behavior".

I don't know how to add the columns of "crimes" in the new df based on this condition, I'd highly appreciate your help.

My code so far is this:

anti_social_behavior <- data.frame()

for (i in 1:length(crimes)){
  if (crimes[i,1] == "anti-social-behaviour"){
    #this is the part of the code I am missing 
  }
}
benson23
  • 16,369
  • 9
  • 19
  • 38
  • 1
    Why did you delete your previous question? As mentioned in your earlier question you can use `anti_social_behavior <- crimes[crimes$category == "anti-social-behaviour", c('id', 'month', 'street_name')]` which is a duplicate of this post https://stackoverflow.com/questions/1686569/filter-data-frame-rows-by-a-logical-condition. You should not use loops and `if` statement here. – Ronak Shah Jan 19 '21 at 06:23
  • Thank you, I thought I didn't express myself clear and I wanted to reformulate my question with the loop and If. – Marta Salvatore Jan 19 '21 at 06:29

0 Answers0