检查两个表,如果一个表的行数较少 - 然后添加行

时间:2013-06-12 15:41:00

标签: r

您好我在文件夹

中的许多文件上运行以下r脚本
runForAll <- function(x) {
  emboss <- read.table(x,header=T)
  x <- table(emboss[,2],emboss[,3])/NROW(emboss[,3])
  y <- as.vector(t(x))
  nms <- expand.grid(colnames(x), rownames(x))
  names(y) <- paste( nms[,2],nms[,1],sep="")
  return(t(y))
}
my.files <- list.files(pattern = "emboss_\\d+\\.ss")
outputs <- lapply(my.files, FUN = runForAll)  

我注意到在x中,某些表的行比其他表少1行。

正常x:

          C           E           H
  A 0.011152416 0.003717472 0.011152416
  C 0.026022305 0.033457249 0.003717472
  D 0.029739777 0.000000000 0.007434944
  E 0.044609665 0.011152416 0.003717472
  F 0.022304833 0.011152416 0.011152416
  G 0.026022305 0.003717472 0.007434944
  H 0.040892193 0.007434944 0.003717472
  I 0.040892193 0.022304833 0.022304833
  K 0.059479554 0.003717472 0.018587361
  L 0.018587361 0.007434944 0.066914498
  M 0.003717472 0.003717472 0.011152416
  N 0.052044610 0.003717472 0.007434944
  P 0.044609665 0.000000000 0.003717472
  Q 0.018587361 0.000000000 0.014869888
  R 0.026022305 0.007434944 0.011152416
  S 0.022304833 0.007434944 0.011152416
  T 0.029739777 0.014869888 0.014869888
  V 0.000000000 0.000000000 0.014869888
  W 0.011152416 0.007434944 0.007434944
  Y 0.018587361 0.029739777 0.022304833

x缺少行:

           C           E           H
  A 0.030874786 0.012006861 0.049742710
  D 0.044596913 0.005145798 0.015437393
  E 0.029159520 0.008576329 0.024013722
  F 0.013722127 0.015437393 0.005145798
  G 0.051457976 0.003430532 0.008576329
  H 0.013722127 0.005145798 0.006861063
  I 0.024013722 0.015437393 0.013722127
  K 0.056603774 0.003430532 0.039451115
  L 0.025728988 0.024013722 0.039451115
  M 0.006861063 0.003430532 0.010291595
  N 0.029159520 0.001715266 0.008576329
  P 0.036020583 0.001715266 0.008576329
  Q 0.020583190 0.005145798 0.015437393
  R 0.037735849 0.008576329 0.020583190
  S 0.030874786 0.005145798 0.027444254
  T 0.034305317 0.006861063 0.017152659
  V 0.010291595 0.012006861 0.018867925
  W 0.006861063 0.000000000 0.010291595
  Y 0.005145798 0.008576329 0.006861063

我该如何检查:

  

x的每个实例有20行   如果x的特定实例少于20行   添加一个充满零的行

即如果一个x只有17行,则添加3行零。

非常感谢。

1 个答案:

答案 0 :(得分:1)

这样做:

x = data.frame(A = c(1:10), B = 1, C = 1)
x[head(seq(nrow(x)+1, 21), -1),] = 0