2016-04-27 17 views
0

私はpremadeスクリプトを使ってRで細菌増殖率をプロットしようとしています。基本的に私は関数を使って点の集合のなかで最も急な傾きを与えようとしています。私は、次のデータフレーム "TMP" を使用しています:Rエラー:データとベクトルが同じ長さではありません

> str(tmp) 
'data.frame': 54 obs. of 10 variables: 
$ Strain  : Factor w/ 54 levels "11A023","11A045",..: 1 2 3 4 5 6 7 8 9 10 ... 
$ 0   : num 0.048 0.05 0.047 0.053 0.051 0.051 0.041 0.05 0.049 0.045 ... 
$ 21.5  : num 0.04 0.042 0.037 0.037 0.041 0.03 0.031 0.043 0.037 0.036 ... 
$ 47.5  : num 0.027 0.041 0.032 0.035 0.034 0.026 0.02 0.042 0.034 0.03 ... 
$ 71.5  : num 0.026 0.039 0.028 0.032 0.032 0.022 0.019 0.041 0.03 0.031 ... 
$ 94.5  : num 0.025 0.037 0.027 0.026 0.03 0.017 0.015 0.037 0.028 0.024 ... 
$ 117.8333333: num 0.023 0.031 0.026 0.035 0.029 0.017 0.017 0.034 0.027 0.022 ... 
$ 144.5  : num 0.021 0.032 0.031 0.029 0.035 0.022 0.012 0.034 0.03 0.023 ... 
$ 154.75  : num 0.022 0.032 0.031 0.033 0.042 0.026 0.016 0.041 0.036 0.025 ... 
$ 194  : num 0.02 0.034 0.034 0.03 0.04 0.022 0.014 0.038 0.034 0.028 ... 

し、次のコード:

tmp = read.csv("sorted_data.csv") #substitute your file name for 'sorted_data' 
source("find_gr.R") #this command loads the script (find_gr) that contains the analysis functions (needs to be in the present working directory) 
time <- seq(0,9.25) #edit as appropriate 
           #note that the growth rate output will be scaled by the time units you use here (per hour, per min, per century, etc.) 

M = nrow(tmp) 
N = ncol(tmp) 

pdf("growth_rate_plots.pdf", paper="letter", width=7.5, height=10) #substitute your desired file name for 'growth_rate_plots' 
growth.rates = NULL 

for (i in 1:M) { 
    print(i) 
    gr <- findgr(tmp[i, 3:N], time, tmp[i, 2], int=12, r2=0.6) #3 in [i, 3:N] is the column number where the data starts; 
    #2 in [i, 2] is the column containg the label you want on the plot; 
    #int is number of points taken at one time as an interval to find the highest slope; 
    #vary (i.e. lower) r2, i.e. rsquared as needed, blanks can be a problem here 
    growth.rates <- rbind(growth.rates, gr) 
} 
dev.off() 

私は、コードを実行すると、私は次のエラーを取得する:

Error: Your data and time are not the same length. 
Error in findgr(tmp[i, 3:N], time, tmp[i, 2], int = 12, r2 = 0.6) : 

私はこれが作成されたベクトル「時間」を参照すると信じています。私のデータフレームは長さが9または10です(長さが$ Strainの場合はわかりません)。さまざまな長さの時間ベクトルを作成しようとしましたが、常にこのエラーが返されます。

私が間違っていることはありますか?私は何を探していますか?

何か助けてくれてありがとう、私はこれで完全な初心者です。スクリプトfind_gr.Rを開くと

**スクリプトはhttps://www.princeton.edu/genomics/botstein/protocols/

+1

'find_gr.R'ソースから読み込まれた関数' findgr'の引数を追加できますか? –

+0

このコードはちょっと混乱しています。このコードは、このサイトに適した[mcve]の範囲を超えています。私は時間をかけてそれを1行ずつ調べ、入力が何であるか、各行が何をするのかを調べることを提案します。おそらくあなたが聞きたかったものではなく、運が良かったでしょう! – Stedy

答えて

0

から得ました。最初の行は:

findgr = function(x, t, plottitle, int=15, r2.cutoff=0.6) { 
... 
#are x and t the same length? 
if (length(x) != length(t)) { 
    cat("Error: Your data and time are not the same length.\n") 
    stop() 
} 

xとtの長さが同じである必要があります。あなたはそこに何を置いているのか見てみましょう。あなたは入れている:

gr <- findgr(tmp[i, 3:N], time, .... 

時間は次のようになります。

time <- seq(0, length(tmp[i, 3:N])-1) 

-1シーケンスは、私の場合、ただし0

から始まるので、(私はいくつかのデータを生成した)それはいくつかの他のエラーを生成します。私はこれがあなたに出発点を与えることを願っています。

関連する問題