2017-02-16 182 views
0

独特的专栏中,我目前没有与年龄和CPUE的数据集。我需要一个循环(或其他代码)遍历位数来创建数据集

  1. 子集的数据帧分成独特数据帧
  2. 为每个唯一的年龄,通过1%CPUE
  3. 发现从10至90百分位值每个百分CPUE值添加到数据集(无论是年龄子集或按年龄确定)作为新列
  4. 基础上添加CPUE的门槛,这将是类似于ifelse语句另一列,其中列将如果告诉你cpue阈值是否满足。

我到目前为止的代码是无环,并显示如下:

#####subsetting for each age 
    yr1=ycsnew[which(ycsnew$age==1),] 
    yr2=ycsnew[which(ycsnew$age==2),] 
    yr3=ycsnew[which(ycsnew$age==3),] 
    yr4=ycsnew[which(ycsnew$age==4),] 
    yr5=ycsnew[which(ycsnew$age==5),] 
    yr6=ycsnew[which(ycsnew$age==6),] 
    yr7=ycsnew[which(ycsnew$age==7),] 

    ####creating a vector using for example the 80th percentile (I would like 
    this to be continuous from 0.1 to 0.9 by 0.01) 
    q1=quantile(yr1$logcpueplus1,0.8)[[1]] 
    q2=quantile(yr2$logcpueplus1,0.8)[[1]] 
    q3=quantile(yr3$logcpueplus1,0.8)[[1]] 
    q4=quantile(yr4$logcpueplus1,0.8)[[1]] 
    q5=quantile(yr5$logcpueplus1,0.8)[[1]] 
    q6=quantile(yr6$logcpueplus1,0.8)[[1]] 
    q7=quantile(yr7$logcpueplus1,0.8)[[1]] 

    ####using ifelse statement to create column for probability greater than 
    or equal to percentile value (pgtq8=ProbabilityGreaterThanQ80) 
    yr1$pgtq8=ifelse(yr1$logcpueplus1>=q1,1,0) 
    yr2$pgtq8=ifelse(yr2$logcpueplus1>=q2,1,0) 
    yr3$pgtq8=ifelse(yr3$logcpueplus1>=q3,1,0) 

    ##then I ended up binding everything back together 
    ycsnew2=rbind(yr1,yr2,yr3,yr4,yr5,yr6,yr7) 

我开始循环播放自己的过程,而这至今都得到:

###1 subset into different year datasets 
    age_split=split(ycsnew,ycsnew$age) 
    new_names <- c("one", "two", "three","four","five","six","seven") 
    for (i in 1:length(age_split)) { 
     assign(new_names[i], age_split[[i]]) 
    } 

    ###for each age, loop through to get values for 0.1-0.9 by0.01 
    N=(seq(0.1,0.9,0.01)) 
    one_percentiles=matrix(rep("puppy",81),nrow=81,ncol=1) 
    sequence1=c(seq(0.1,0.9,0.01)) 
    for(n in unique(seq(1,81,1))){ 
     for(i in sequence1){ 
     ps=quantile(one$logcpueplus1,i)[[1]] 
     one_percentiles[[n]]=ps 

     } 
    } 

我我现在陷入因为“我”的价值不是通过“序列1”对象循环正确,我只是得到一个“one_percentiles”矩阵充满位数值只有我= 0.1或I = 0.9。

我一定会通过回路添加ifelse()语句到数据集作为一个新列以及难倒,所以任何有识之士将不胜感激。

我的数据集如下。

logcpueplus1 age 
1  0.13353139 7 
2  0.13353139 6 
3  0.06899287 2 
4  0.08004271 1 
5  0.13353139 6 
6  0.06899287 7 
7  0.04879016 4 
8  0.04879016 4 
9  0.13353139 7 
10 0.06899287 7 
12 0.06899287 6 
13 0.06899287 2 
14 0.06899287 2 
15 0.06899287 7 
16 0.06899287 6 
17 0.09531018 4 
21 0.13353139 7 
22 0.18232156 4 
24 0.04879016 4 
25 0.09531018 4 
26 0.06899287 2 
28 0.06899287 7 
30 0.04879016 4 
37 0.08004271 1 
39 0.04879016 4 
40 0.08004271 1 
41 0.13353139 6 
42 0.25131443 6 
43 0.13353139 7 
44 0.04879016 4 
47 0.08004271 1 
49 0.04879016 4 
50 0.13353139 5 
52 0.19415601 6 
53 0.13353139 7 
54 0.19415601 6 
55 0.30538165 5 
56 0.04879016 3 
57 0.06899287 1 
59 0.06899287 6 
60 0.35667494 5 
61 0.09531018 3 
63 0.19415601 6 
64 0.25131443 5 
65 0.09531018 3 
66 0.06899287 6 
67 0.19415601 6 
69 0.06899287 1 
70 0.13976194 3 
71 0.13353139 5 
73 0.04879016 3 
77 0.06899287 6 
78 0.04879016 3 
79 0.06899287 7 
+0

我不遵循所有百分比的东西。你可以选择一个单一的“年龄”值,比如说“年龄= 1”,并显示你想要的年龄结果? – Gregor

回答

0

这是我最后使用,适合所有的我的需要的选项。

ycsB=ycsnew 
dim(ycsnew) 
names(ycsB) 
see2=(seq(0.1,0.9,0.01)) 

ycsB[, 21:(21+length(see2)-1)]=NA 
names(ycsB) 
ages=unique(ycsnew$age) 

for(s in 1:length(see2)){ 

    quant_name=see2[s] 

for(b in 1:length(ages)){ 

    age_subset=subset(ycsB, ycsB$age==ages[b]) 
    age_quantile= quantile(age_subset$logcpueplus1,quant_name) 

    ycsB[which(ycsB$age==ages[b]), (26+s)]=age_quantile 
    for(j in 1:length(ages)){ 

     if_s=ifelse(age_subset$logcpueplus1>=age_quantile,1,0) 
     ycsB[which(ycsB$age==ages[b]), (107+s)]=if_s 
     } 
    } 
} 
1

使用dplyr并在df是你的数据帧。

library(dplyr) 
df <- df %>% group_by(age) %>% 
    mutate(q = quantile(logcpueplus1,0.8), 
      pgtq8 = ifelse(logcpueplus1 >= q,1,0)) 
df 
#Source: local data frame [54 x 4] 
#Groups: age [7] 
# logcpueplus1 age   q  pgtq8 
# <dbl>   <int>  <dbl> <dbl> 
#1 0.13353139  7  0.13353139  1 
#2 0.13353139  6  0.19415601  0 
#3 0.06899287  2  0.06899287  1 
#4 0.08004271  1  0.08004271  1 
#5 0.13353139  6  0.19415601  0 
#6 0.06899287  7  0.13353139  0 
#7 0.04879016  4  0.09531018  0 
#8 0.04879016  4  0.09531018  0 
#9 0.13353139  7  0.13353139  1 
#10 0.06899287  7  0.13353139  0 
# ... with 44 more rows 
+0

这就是我一直在寻找,如果我有足够的积分,我会表决吧。 – hcg

0

下面是使用data.table

library(data.table) 
setDT(df1)[, q := quantile(logcpueplus1, 0.8), age 
      ][, pgtq8 := as.integer(logcpueplus1 >= q)][]