繁体   English   中英

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

[英]R Error: Data and vector are not the same length

我正在尝试使用预制脚本绘制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) :  

我相信这是指创建的向量“时间”。 我的数据帧的长度为9或10(不确定我是否在长度上计入$ Strain)。 我尝试创建具有不同长度的时间向量,但始终会返回此错误。

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

非常感谢您的帮助,我是一个完整的初学者。

**脚本是从https://www.princeton.edu/genomics/botstein/protocols/获得的

如果打开脚本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开始。

但是,就我而言(我生成了一些数据),它还会产生其他错误。 我希望这可以为您提供一个起点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM