简体   繁体   English

从R中的另一个向量中删除一个向量中的元素

[英]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.我是 R 新手,无法找到一种方法从另一个向量中删除一个向量的所有元素。 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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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