从索引中提取data.frame中的元素

时间:2017-09-18 11:27:40

标签: r dataframe

假设我在R中有类似日历的数据框:

df = data.frame(Sun = c("*","*","*","*","*","*","*","*","*","*"),
             Mon= c("*","s","*","*","*","*","*","*","*","*"),
             Tues = c("*","*","*","*","*","*","*","*","*","*"),
             Wedn = c("*","*","*","*","*","*","*","*","*","*"),
             Thur = c("*","*","*","*","*","*","*","*","*","*"),
             Fri = c("*","*","*","*","*","*","*","*","*","*"),
             Sat = c("*","*","*","*","e","*","*","*","*","*"))

> df
   Sun Mon Tues Wedn Thur Fri Sat
1    *   *    *    *    *   *   *
2    *   s    *    *    *   *   *
3    *   *    *    *    *   *   *
4    *   *    *    *    *   *   *
5    *   *    *    *    *   *   e
6    *   *    *    *    *   *   *
7    *   *    *    *    *   *   *
8    *   *    *    *    *   *   *
9    *   *    *    *    *   *   *
10   *   *    *    *    *   *   *

我们可以将此日历编入索引如下:

df_index[1,1] = 1
df_index[1,2] = 2 
.
.
.
df_index[2,1] = 8
df_index[2,2] = 9
等等等等。也就是说,df [1,1]是第一天,df [2,1]是第8天。 (并且df_index [,]不存在,只是为了更好地理解)。

我想要做的是按索引对此数据框进行子集化。例如,我想从第9天到第35天提取并生成新的数据框:

  Sun Mon Tues Wedn Thur Fri Sat
1  NA   s    *    *    *   *   *
2   *   *    *    *    *   *   *
3   *   *    *    *    *   *   *
4   *   *    *    *    *   *   e

2 个答案:

答案 0 :(得分:0)

一周不过是7天,所以要获得周数,你只需要将它除以7.将上限值作为索引并提取子集应该有效!

> df[ceiling(9/7):ceiling(35/7),]
  Sun Mon Tues Wedn Thur Fri Sat
2   *   s    *    *    *   *   *
3   *   *    *    *    *   *   *
4   *   *    *    *    *   *   *
5   *   *    *    *    *   *   e

答案 1 :(得分:0)

您可以对数据框进行矢量化,使得此向量的第一个元素是第一天等。

   > x = as.vector(t(df))
   > x
    [1] "*" "*" "*" "*" "*" "*" "*" "*" "s" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*"
    [20] "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "e" "*" "*" "*"
    [39] "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*"
    [58] "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*"

要获得9至35天,您只需

> x[9:35]
 [1] "s" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*" "*"
[20] "*" "*" "*" "*" "*" "*" "*" "e"

然后,您可以再次将其放入矩阵/数据框中。但是,当您错过一周的第一天时,您还需要首先获得NA值。所以可能应该使用天花板/地板并除以7,如另一个答案所示。