0

私は R にまったく慣れていないため、投稿が通常の形式ではないことを事前にお詫びします (使用してみdput()ましたが、奇妙な出力が得られ、データセットをアップロードする方法がわかりません。本当に申し訳ありません)。

6 つの列 (サイト、開始日、終了日、撮影日、種、個体) を持つデータセットがあります。例えば:

site    year    startdate   enddate photodate   species indiv
M1_7    2012    19/07/2012  10/08/2012  20/07/2012  Sylvicapra grimmia  1
M1_7    2012    19/07/2012  10/08/2012  23/07/2012  Crocuta crocuta 1
M1_7    2012    19/07/2012  10/08/2012  23/07/2012  Potamochoerus larvatus  1
M1_7    2012    19/07/2012  10/08/2012  25/07/2012  Hystrix cristata    1
M1_7    2012    19/07/2012  10/08/2012  27/07/2012  Potamochoerus larvatus  1
M1_7    2012    19/07/2012  10/08/2012  27/07/2012  Sylvicapra grimmia  1
M1_7    2012    19/07/2012  10/08/2012  28/07/2012  Hippotragus equinus     1
M1_7    2012    19/07/2012  10/08/2012  30/07/2012  Crocuta crocuta 1
M1_7    2012    19/07/2012  10/08/2012  01/08/2012  Equus q. boehmi 1
M1_7    2012    19/07/2012  10/08/2012  01/08/2012  Crocuta crocuta 1
M1_7    2012    19/07/2012  10/08/2012  05/08/2012  Potamochoerus larvatus  1
M1_7    2012    19/07/2012  10/08/2012  07/08/2012  Hippotragus equinus     1
M1_9    2012    21/07/2012  11/08/2012  24/07/2012  Pedetes capensis    1
M1_9    2012    21/07/2012  11/08/2012  24/07/2012  Crocuta crocuta 2
M1_9    2012    21/07/2012  11/08/2012  24/07/2012  Pedetes capensis    1
M1_9    2012    21/07/2012  11/08/2012  27/07/2012  Pedetes capensis    1
M1_9    2012    21/07/2012  11/08/2012  01/08/2012  Alcelaphus b. lichtensteinii    1
M1_9    2012    21/07/2012  11/08/2012  03/08/2012  Pedetes capensis    1
M1_9    2012    21/07/2012  11/08/2012  04/08/2012  Crocuta crocuta 1
M1_9    2012    21/07/2012  11/08/2012  06/08/2012  Pedetes capensis    1
M1_9    2012    21/07/2012  11/08/2012  07/08/2012  Pedetes capensis    1
M1_9    2012    21/07/2012  11/08/2012  08/08/2012  Pedetes capensis    1
M1_11   2012    21/07/2012  11/08/2012  26/07/2012  Mellivora capensis  1
M1_11   2012    21/07/2012  11/08/2012  03/08/2012  Sylvicapra grimmia  1
M1_11   2012    21/07/2012  11/08/2012  07/08/2012  Hystrix cristata    1
M1_11   2012    21/07/2012  11/08/2012  08/08/2012  Potamochoerus larvatus  1

列1がサイトに対応し、列2がサイト内の「開始日」と「終了日」の間の日付のシーケンスに対応し、列3:49が種名に対応する49列のマトリックスを作成するループを作成しようとしています。列 3:49 の下のセル内に、特定の種の特定の日付のカウント データ (indiv) を合計して得られたデータを入力したいと思います。

これまでのところ、必要なものに対応する空のマトリックスを作成することしかできませんでしたが、データを入力することはできませんでした. これは私が使用したコードです:

mlele2012<- read.delim("C:\\multiple regression\\mlele 2012 empty matrix creation.txt")
africa <- read.delim("C:\\species accumulation curves\\COMPLETE species list.txt")
specieslistx<-unique(africa)
specieslistx<-t(specieslistx) 

oldtemp<-NULL 
temp <- rep(0, length(specieslistx ))

strptime(mlele2012$photodate, "%Y-%m-%d")
strptime(mlele2012$startdate, "%d/%m/%Y")
strptime(mlele2012$enddate, "%d/%m/%Y")

#create empty dataframe with dimensions: no. of sites x no. of dates in each

for(i in levels(mlele2012$site))    { ##for each site

    sitetemp <- subset(mlele2012, site == i) ###subset of dataset , for the particular site i##

    sitetemp$startdate<- as.Date(sitetemp$startdate, "%d/%m/%Y")
    sitetemp$enddate<- as.Date(sitetemp$enddate, "%d/%m/%Y")

    sitedatelist<-seq(as.Date(sitetemp$startdate[1]), as.Date(sitetemp$enddate[1]), "days")

    empty<-matrix(0,length(sitedatelist),length(specieslistx))
    sitedatelist1<-as.character(sitedatelist)
    row.names(empty)<-(sitedatelist1)
    colnames(empty)<-specieslistx

    addsitecol<-matrix(0,length(sitedatelist),1)
    extendempty<-cbind(addsitecol,empty)
    extendempty[,1]<-i
    oldtemp<-rbind(oldtemp, extendempty)
}

write.csv(oldtemp, "Mlele 2012 dry empty.csv")

さらに、同じ形式/ディメンションで別のマトリックスを作成するために抽出しようとしましたが、余分な日付はありません(つまり、「開始日」と「終了日」の間のシーケンスではなく、「photodate」列の日付のみ)。最終的に必要なものを取得するために、最終的に2つのマトリックスを何らかの方法でマージできることを望んでいました. 残念ながら、エラーはないようですが、このコードは機能しません。これが私のコードの2番目の部分です:

for(i in mlele2012$site)    {    
   sitetemp <- subset(mlele2012, site == i) ###subset of dataset "allsites", for the particular site i##
   for(j in sitetemp$photodate){
      datetemp <- subset(sitetemp, photodate == j) ###subset of dataset "africaa", for the particular date i#
      uniquespperdate <- unique(datetemp$species)###unique species within each date (row) i#
      temp <- rep(0, length(specieslistx)) #create a temporary vector of 0s with the same length as the species list###

      for(a in uniquespperdate){
         sptemp <- subset(datetemp , species == a) ###subset of dataset "sitetemp", for the particular sp j##
         countdata<-sum(sptemp$indiv)
         index <- pmatch(a, names(temp)) ###match the unique species per date to the location on the species list###
         #there is a problem here, it works when run as a single line but not within a loop
         temp[index] <- countdata   ###for the locations listed in "index", assign the count data to the temporary vector###
         names(temp)<- specieslistx
      }
   }        
   oldtemp <- rbind(oldtemp, temp) ### bind the new temp file to the old temp file, i.e. update the list as the loop runs###
}

どんな助けでも大歓迎です。質問をより明確にするために提供できる詳細があればお知らせください。

4

2 に答える 2

1

私はあなたのサンプルでそこにほとんどの道を得ることができます:

> ftable(xtabs(indiv~site+year+species, data=dat) )
           species boehmi capensis cristata crocuta equinus grimmia larvatus lichtensteinii
site  year                                                                                 
M1_11 2012              0        1        1       0       0       1        1              0
M1_7  2012              1        0        1       3       2       2        3              0
M1_9  2012              0        7        0       3       0       0        0              1

要求された dput バージョンを提供しなかったため、属/種を 2 つの列として使用してデータを入力しました。

于 2013-05-13T21:07:54.227 に答える