简体   繁体   中英

How to choose the subset of a dataframe using a vector in R

I have a time series, and I want to choose the data of the last day of every month. So I create a vector of last days in month (using ymd() ), and I want to subset the dataset by this vector, but it gives me this error:

> realized <- subset(realized , realized$date == last)
Warning message:
In `==.default`(realized$date, last) :
  longer object length is not a multiple of shorter object length

As a simple example I try to do this:

x <- c(1,2,3,4,5,6,7,8,9,10,11,12)
y <- c(2,4,6,8,1,7,10,2,6,2,4,9)

z <- data.frame(x,y)

Now I want for example to choose only this x values from data frame:

a <- c(2,4,7,9)

So I write:

z <-subset(z, x==a)

This example has no such error, but it does not still work.

这应该工作:

z <- subset(z, x %in% a)

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