2017-02-16 181 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)][]