R错误:数据和向量长度不同

时间:2016-04-27 04:58:33

标签: r

我试图使用预制脚本绘制R中的细菌生长率。基本上我试图使用一个函数给我一组点之间最陡的斜率。我使用以下数据框" tmp":

> 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) :  

我相信这是指矢量&#39; time&#39;创建。我的数据帧长度为9或10(不确定我的长度是否为$ Strain)。我尝试创建一个长度不一的时间向量,但总是会返回此错误。

我做错了什么吗?我应该寻找什么?

非常感谢任何帮助,我是一个完全的初学者。

**脚本来自https://www.princeton.edu/genomics/botstein/protocols/

1 个答案:

答案 0 :(得分:0)

如果您打开脚本find_gr.R。第一行说:

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开始。

然而在我的情况下(我生成了一些数据)它会产生一些其他错误。我希望这能为你提供一个起点。