for循环中r的列差异

时间:2014-08-30 04:22:24

标签: r loops

不确定为什么这不起作用。我想从其余的数据中减去第一列数据,以形成一个新的数据帧。在我看来,最好的方法是使用for循环。这是我的df:

      all  a  b  c  d  e  f  g  h  i  j   k    l  m   n   o    p   q
[1,]   15 14 17  6 NA  2 22 31 10 24  27  13   1  16  19  27   1   6
[2,]   16  5 11 26 29 17  7 12 13 NA  26  21  25  15   4   9   5  21
[3,]   12 11  9 25 21  8 26 16 11  9  30  NA   5  14  20  32  18  25
[4,]   28  7 20 14 18 13  3 29 23 NA  19  20  23  25   2   4  31   3
[5,]   21 26 13  9 12  7 11  4  1  5  10  16  NA  29   5  26  25  16
[6,]    7 28  6 23  4 21 16 20  3 NA   4  29  26  13  24   5  13  29

这是我在R中所做的,我得到的错误是:矩阵上的下标数量不正确

new_df = c()
for(i in 1:ncol(df)){
  new_df[,i] = df[,1] - df[,i]
}

我只想让脚本在第二列和第三列之间减去第一列等。

预期输出只显示前两列和行

       a  b
[1,]   1 -2
[2,]   11 5

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:4)

使用您的数据,

txt <- "all a  b  c  d  e  f  g  h  i   j   k    l  m   n   o   p   q
15 14 17  6 NA  2 22 31 10 24  27  13   1  16  19  27   1   6
16  5 11 26 29 17  7 12 13 NA  26  21  25  15   4   9   5  21
12 11  9 25 21  8 26 16 11  9  30  NA   5  14  20  32  18  25
28  7 20 14 18 13  3 29 23 NA  19  20  23  25   2   4  31   3
21 26 13  9 12  7 11  4  1  5  10  16  NA  29   5  26  25  16
 7 28  6 23  4 21 16 20  3 NA   4  29  26  13  24   5  13  29"

df <- read.table(text = txt, header = TRUE)
不管你信不信,你可以通过以下方式做到:

df[,1] - df[,-1]

> df[,1] - df[,-1]
    a  b   c   d   e   f   g  h  i   j   k   l  m   n   o  p   q
1   1 -2   9  NA  13  -7 -16  5 -9 -12   2  14 -1  -4 -12 14   9
2  11  5 -10 -13  -1   9   4  3 NA -10  -5  -9  1  12   7 11  -5
3   1  3 -13  -9   4 -14  -4  1  3 -18  NA   7 -2  -8 -20 -6 -13
4  21  8  14  10  15  25  -1  5 NA   9   8   5  3  26  24 -3  25
5  -5  8  12   9  14  10  17 20 16  11   5  NA -8  16  -5 -4   5
6 -21  1 -16   3 -14  -9 -13  4 NA   3 -22 -19 -6 -17   2 -6 -22

如果需要新数据框,请指定此项

newdf <- df[,1] - df[,-1]

答案 1 :(得分:0)

您自己的代码适用于data.frames:

df = structure(list(all = c(15L, 16L, 12L, 28L, 21L, 7L), a = c(14L, 
5L, 11L, 7L, 26L, 28L), b = c(17L, 11L, 9L, 20L, 13L, 6L), c = c(6L, 
26L, 25L, 14L, 9L, 23L), d = c(NA, 29L, 21L, 18L, 12L, 4L), e = c(2L, 
17L, 8L, 13L, 7L, 21L), f = c(22L, 7L, 26L, 3L, 11L, 16L), g = c(31L, 
12L, 16L, 29L, 4L, 20L), h = c(10L, 13L, 11L, 23L, 1L, 3L), i = c(24L, 
NA, 9L, NA, 5L, NA), j = c(27L, 26L, 30L, 19L, 10L, 4L), k = c(13L, 
21L, NA, 20L, 16L, 29L), l = c(1L, 25L, 5L, 23L, NA, 26L), m = c(16L, 
15L, 14L, 25L, 29L, 13L), n = c(19L, 4L, 20L, 2L, 5L, 24L), o = c(27L, 
9L, 32L, 4L, 26L, 5L), p = c(1L, 5L, 18L, 31L, 25L, 13L), q = c(6L, 
21L, 25L, 3L, 16L, 29L)), .Names = c("all", "a", "b", "c", "d", 
"e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q"
), class = "data.frame", row.names = c(NA, -6L))


new_df = df
for(i in 1:ncol(df)){
  new_df[,i] = df[,1] - df[,i]
}

new_df

  all   a  b   c   d   e   f   g  h  i   j   k   l  m   n   o  p   q
1   0   1 -2   9  NA  13  -7 -16  5 -9 -12   2  14 -1  -4 -12 14   9
2   0  11  5 -10 -13  -1   9   4  3 NA -10  -5  -9  1  12   7 11  -5
3   0   1  3 -13  -9   4 -14  -4  1  3 -18  NA   7 -2  -8 -20 -6 -13
4   0  21  8  14  10  15  25  -1  5 NA   9   8   5  3  26  24 -3  25
5   0  -5  8  12   9  14  10  17 20 16  11   5  NA -8  16  -5 -4   5
6   0 -21  1 -16   3 -14  -9 -13  4 NA   3 -22 -19 -6 -17   2 -6 -22