简体   繁体   English

从数据框中选择所有行,包括基于向量R的重复行

[英]select all the rows from data frame including recurring rows based on vector R

I'm trying to bootstrap a data set according to a vector of years: 我正在尝试根据年份的向量来引导数据集:

this is my datasframe df: 这是我的datasframe df:

'data.frame':   103 obs. of  3 variables:
 $ date     : Date, format: "1962-08-01" "1982-08-01" "1983-08-01" ...
 $ flow     : num  0 0 0 0 0 0 0 0 0 0.404 ...
 $ hydroYear: Factor w/ 45 levels "1950","1951",..: 11 27 28 29 33 35 36 37 40 41 ...

I'm using boot like this: 我正在使用这样的启动:

qmat <- boot(data=as.integer(as.vector(unique(df$hydroYear))),statstic=xboot,R=100)

now I want to create a new dataframe out of df where all the rows that have the same df$hydro year as the boot year vector will be there, including repetition. 现在,我想从df中创建一个新的数据框,其中所有与启动年向量具有相同df$hydro年的行都将存在,包括重复。

So for example if me data frame is: 因此,例如,如果我的数据帧是:

flow    hydroYear
1       1951
2       1951
3       1953
4       1954
5       1954
6       1956

and the bootstrap vector is: 引导向量为:

1954 1953 1954 1951

I will get a dataframe that looks like this: 我将得到一个如下所示的数据框:

flow    hydroYear
4       1954
5       1954
3       1953
4       1954
5       1954
1       1951
2       1951

I've tried this: 我已经试过了:

  xboot <-  function(yearboot,b,method){
    tmpyr <- yearboot[b]
    df1 <- df[df$hydroYear %in% tmpyr ,]}

but it doesn't create the recurring rates 但它不会产生重复率

Not the only way to do it, but 这不是唯一的方法,但是

mydf <- data.frame(flow=c(1,2,3,4,5,6), hydroYear=c(1951,1951,1953,1954,1954,1956))
boot <- c(1954,1953,1954,1951)
do.call(rbind, lapply(boot, FUN=function(x){mydf[mydf$hydroYear == x, ]}))

gives

   flow hydroYear
4     4      1954
5     5      1954
3     3      1953
41    4      1954
51    5      1954
1     1      1951
2     2      1951

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

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