2014-02-25 82 views
1

我在做模擬,並試圖重複向列添加錯誤,特別是標題爲Ao的列。在我的輸出中,前30行是正確的;我們有初始數據,第一年的數據改變(錯誤添加到Ao),但之後,我想有30年的錯誤增加,我重複了第二年的Ao直到30年。我的目標是是在每年抽樣後增加錯誤。 IE瀏覽器。第2年是第1年Ao +錯誤。第3年是第2年Ao +錯誤,如此等等。任何助手?乾杯。將rnorm添加到循環中的列

for(t in 1:30){ 
     Error<-rnorm(1000,0,1) 
     m<-rep(year1data$m,30) 
     r<-rep(year1data$r,30) 
     a<-rep(year1data$a,30) 
     g<-rep(year1data$g,30) 
     Year<-rep(2:31, each=TotSpecies) 
     Species<-1:TotSpecies 
     Ao<-year1data$Ao+sample(Error,TotSpecies,replace=FALSE) 
     TotSpeciesdata<-data.frame(Species,Year,Ao,m,r,a,g) 
     TotSpeciesdata<-rbind(year1data,TotSpeciesdata) 
    } 

    > TotSpeciesdata 
    Species Year   Ao m   r   a   g 
    1   1 1 25.770783 43 119.110786 3.2305180 2.6526471 
    2   2 1 53.908914 138 161.894541 0.7342070 0.1151602 
    3   3 1 2.010732 226 193.820489 2.2890904 3.6248105 
    4   4 1 23.742254 332 17.315335 1.4009572 2.0037931 
    5   5 1 4.291080 63 187.591209 0.2563995 2.1553908 
    6   6 1 4.691113 343 116.267867 0.3899113 3.3950085 
    7   7 1 604.133044 224 132.240197 3.0410743 0.7985524 
    8   8 1 13.332567 166 5.367118 0.7921644 1.7861011 
    9   9 1 3.759268 141 212.340970 2.8733737 2.7123141 
    10  10 1 3.647390 209 259.400858 0.1249936 0.6594659 
    11  11 1 23.731109 10 114.171147 2.2437372 0.9867591 
    12  12 1 85.116996 69 167.412993 0.8306823 2.8905148 
    13  13 1 31.684280 277 177.025460 2.7618332 2.9245554 
    14  14 1 30.657523 205 21.710438 2.7661347 1.5911379 
    15  15 1 12.240410 85 210.121109 2.8827455 3.0418454 
    16  1 2 27.038097 43 119.110786 3.2305180 2.6526471 
    17  2 2 54.251600 138 161.894541 0.7342070 0.1151602 
    18  3 2 2.010636 226 193.820489 2.2890904 3.6248105 
    19  4 2 22.699369 332 17.315335 1.4009572 2.0037931 
    20  5 2 4.542589 63 187.591209 0.2563995 2.1553908 
    21  6 2 3.607833 343 116.267867 0.3899113 3.3950085 
    22  7 2 604.480756 224 132.240197 3.0410743 0.7985524 
    23  8 2 13.663513 166 5.367118 0.7921644 1.7861011 
    24  9 2 2.138715 141 212.340970 2.8733737 2.7123141 
    25  10 2 3.642769 209 259.400858 0.1249936 0.6594659 
    26  11 2 22.897993 10 114.171147 2.2437372 0.9867591 
    27  12 2 85.490897 69 167.412993 0.8306823 2.8905148 
    28  13 2 31.689202 277 177.025460 2.7618332 2.9245554 
    29  14 2 30.644419 205 21.710438 2.7661347 1.5911379 
    30  15 2 12.050207 85 210.121109 2.8827455 3.0418454 
    31  1 3 27.038097 43 119.110786 3.2305180 2.6526471 
    32  2 3 54.251600 138 161.894541 0.7342070 0.1151602 
    33  3 3 2.010636 226 193.820489 2.2890904 3.6248105 
    34  4 3 22.699369 332 17.315335 1.4009572 2.0037931 
    35  5 3 4.542589 63 187.591209 0.2563995 2.1553908 
    36  6 3 3.607833 343 116.267867 0.3899113 3.3950085 
    37  7 3 604.480756 224 132.240197 3.0410743 0.7985524 
    38  8 3 13.663513 166 5.367118 0.7921644 1.7861011 
    39  9 3 2.138715 141 212.340970 2.8733737 2.7123141 
    40  10 3 3.642769 209 259.400858 0.1249936 0.6594659 
    41  11 3 22.897993 10 114.171147 2.2437372 0.9867591 
    42  12 3 85.490897 69 167.412993 0.8306823 2.8905148 
    43  13 3 31.689202 277 177.025460 2.7618332 2.9245554 
    44  14 3 30.644419 205 21.710438 2.7661347 1.5911379 
    45  15 3 12.050207 85 210.121109 2.8827455 3.0418454 
+1

歡迎來到Stack Overflow!請以[可重現的格式](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)提供您的數據,例如使用'dput'。目前還不清楚你想要什麼。考慮提供一個預期輸出的例子。 – Thomas

回答

4

你有你的方法的主要問題是該行:

TotSpeciesdata<-data.frame(Species,Year,Ao,m,r,a,g) 

因爲Year30 * TotSpecies載體,但所有的人都只是TotSpecies長。因此,實際上,當您創建數據框時,您將回收除Year以外的所有列,這將導致第2年的數據重複30次,等等。如果你只有Year <- rep(i + 1, TotSpecies)我認爲你的邏輯會正常工作。這就是說,這裏是另一種方法:

這種意志,每個物種,創建開始Ao該物種爲5年遞增隨機遊走(只是做了,對於顯示器而言):

set.seed(1) 
year1data <- data.frame(species=1:10, year=1, Ao=runif(10, 1, 700)) 
TotSpeciesData <- do.call(
    rbind, 
    lapply(
    split(year1data, year1data$species), 
    function(data) 
     with(
     data, 
     data.frame(species=species, year=year, Ao=c(Ao, Ao + cumsum(rnorm(5))) 
)))) 
head(TotSpeciesData, 15) 

請注意,我排除了列mg,因爲它們似乎與您的特定問題沒有直接關係,但您可以相對輕鬆地添加它們。我也只是做了5年除了1年,所以你可以在這裏看到的結果,但是這也容易改變:

species year  Ao 
1.1  1 1 186.5906 
1.2  1 1 185.7701 
1.3  1 1 186.2575 
1.4  1 1 186.9958 
1.5  1 1 187.5716 
1.6  1 1 187.2662 
2.1  2 1 261.1146 
2.2  2 1 262.6264 
2.3  2 1 263.0162 
2.4  2 1 262.3950 
2.5  2 1 260.1803 
2.6  2 1 261.3052 
3.1  3 1 401.4245 
3.2  3 1 401.3796 
3.3  3 1 401.3634 
+0

非常感謝您的幫助。第一個提示:Year <-rep(i + 1,TotSpecies)不起作用,但我能夠調整您給出的替代方案。非常感激! – KL2014

+0

不幸的是,所提供的代碼似乎沒有正常工作。看到我的評論如下。 – KL2014

0

據指出,您在上面提供的,或者至少是代碼我已經編輯過,每15年重複一次,而不是以逐步的方式獨一無二的年份。我編輯它,如下所示:

TotSpeciesData <- do.call(
    rbind, #bind the table by rows 
    lapply( #applying the function in list form 
    split(year1data, year1data$Species), #splits data into groups by species 
    function(data) 
     with(
     data, 
     data.frame(Species=Species, Year=1:Community, Ao=c(Ao, Ao + cumsum(rnorm((TotSpecies-1),0,2))),m=m, r=r, a=a, g=g) #data frame is Species, Year, 
     ))) 
    TotSpeciesData$Ao[TotSpeciesData$Ao<0]<-0     #any values less than 0 go to 0 
TotSpeciesData<-TotSpeciesData[order(TotSpeciesData$Year),] #orders the data frame by Year 

當我做這樣的代碼:

TotSpeciesData[TotSpeciesData$Species==1 & TotSpeciesData$Year %in% c(1,2,16,17),] 

我結束了表示數據正在重演的輸出。

Species Year  Ao m  r  a  g 
1.1  1 1 48.49161 239 332.9625 3.791778 2.723104 
1.2  1 2 49.62851 239 332.9625 3.791778 2.723104 
1.16  1 16 48.49161 239 332.9625 3.791778 2.723104 
1.17  1 17 49.62851 239 332.9625 3.791778 2.723104 

對此的評論?

+0

您需要確保您的'Year'和'Ao'向量長度相同。沒有數據很難確定,但我認爲你需要'Ao = c(Ao,Ao + cumsum(rnorm((Community-1),0,2)))'(注意我用'Community替換了'TotSpecies' '),那麼你的'Year'和'Ao'向量的長度就相同了。 – BrodieG