简体   繁体   English

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

[英]Extract elements from data.frame by index

Suppose I have a calendar-like data frame in R: 假设我在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   *   *    *    *    *   *   *

We may index this calendar as following: 我们可以将该日历编入索引如下:

df_index[1,1] = 1
df_index[1,2] = 2 
.
.
.
df_index[2,1] = 8
df_index[2,2] = 9

so on and so forth. 等等等等。 That is, df[1,1] is the first day and df[2,1] is the 8th day. 也就是说,df [1,1]是第一天,而df [2,1]是第八天。 (And df_index[,] is non-existing, just to help understand better). (并且df_index [,]不存在,只是为了帮助更好地理解)。

What I want to do is to subset this data frame by index. 我要做的是按索引将数据帧子集化。 For example, I want to extract from 9th day to 35th day and generate a new data frame: 例如,我想从第9天到第35天提取数据并生成一个新的数据框:

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

A week is nothing but 7 days so to get the week number you just have to divide it by 7. Taking the ceiling value as index and extracting subset should work! 一周不过是7天而已,因此要获得周数,您只需要将其除以7。以最高值作为索引并提取子集就可以了!

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

You can vectorize the dataframe such that the first element of this vector is the first day, etc. 您可以矢量化数据帧,以使此矢量的第一个元素为第一天,依此类推。

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

To get the 9 to 35 day you simply do 要获得9到35天的时间,您只需

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

You can then put this in a matrix/dataframe again. 然后,您可以再次将其放在矩阵/数据框中。 However, you also need first a NA value as you miss the first day of the week. 但是,您还需要首先输入NA值,因为您错过了一周的第一天。 So probably should work with ceiling/floor and dividing by 7, as suggested by the other answer. 因此,应该使用上限/下限并将其除以7,如另一个答案所建议的那样。

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

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