简体   繁体   中英

removing elements in one vector from another in 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]]
  }

这应该可以解决问题

  setdiff(dates, weekends)

Or this

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

days[!days %in% weekend]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM