Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing elements in one vector from another in R

Tags:

r

I'm new to R and am having trouble finding a way to remove all of the elements of one vector from another. I have a vector of dates called "dates", and want to remove the dates that are weekends (which are in the vector "weekends".

The code below works, but I know there must be a more efficient way to do it rather than one at a time... Let me know!

  for (index in 1:length(weekends)) {
    datesReformatted <- datesReformatted[datesReformatted != weekends[index]]
  }
like image 278
user1428668 Avatar asked Dec 06 '22 14:12

user1428668


2 Answers

this should do the trick

  setdiff(dates, weekends)
like image 94
Matthew Plourde Avatar answered Dec 27 '22 19:12

Matthew Plourde


Or this

days <- c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
weekend <- c("Saturday", "Sunday")

days[!days %in% weekend]
like image 29
Dario Galanti Avatar answered Dec 27 '22 19:12

Dario Galanti