【问题标题】:Select two sets of column based on row indices根据行索引选择两组列
【发布时间】:2019-01-01 11:10:40
【问题描述】:

工作示例是展示我正在寻找的内容的最佳方式。

Given input df
df <- data.frame( l = letters[1:10], n = 1:10)
   l  n
1  a  1
2  b  2
3  c  3
4  d  4
5  e  5
6  f  6
7  g  7
8  h  8
9  i  9
10 j 10

我想根据起始索引向量和长度从 l 列中选择行。例如

start <- c(2, 4)
len <- 2

我想得到输出

b c
d e

我试过了

df[(start):(start+len),1]
[1] b c d
Levels: a b c d e f g h i j
Warning messages:
1: In (start):(start + len) :
  numerical expression has 2 elements: only the first used
2: In (start):(start + len) :
  numerical expression has 2 elements: only the first used

应用也不起作用。

apply(start, 1, function(x, d) {d[x:(x+2),1]}, d = df)
Error in apply(start, 1, function(x, d) { : 
  dim(X) must have a positive length

【问题讨论】:

    标签: r dataframe indexing subset


    【解决方案1】:

    我们可以在“开始”上使用lapply 来获取sequence,并将length.out 指定为“len”。然后将“l”提取为vector

    df$l[unlist(lapply(start, function(x) seq(x, length.out =len)))]
    

    或作为listvectors

    lapply(start, function(x) as.character(df$l)[seq(x, length.out = len)])
    

    【讨论】:

      【解决方案2】:

      这里有两个选项可以获得您指定的确切输出,但首先要确保您的 df$l 不是一个因素。

      df <- data.frame(l = letters[1:10], n = 1:10, stringsAsFactors = FALSE)
      start <- c(2, 4)
      len <- 2
      
      for (s in start) {cat(df[s:(s+len-1), 1]); cat("\n")}
      # b c
      # d e
      
      cat(sapply(start, function(x) {paste(df[x:(x+len-1), 1], collapse = " ")}), sep = "\n")
      # b c
      # d e
      

      【讨论】:

        猜你喜欢
        • 2013-10-09
        • 1970-01-01
        • 1970-01-01
        • 2018-02-08
        • 1970-01-01
        • 2020-09-04
        • 1970-01-01
        • 2018-12-06
        • 2018-06-21
        相关资源
        最近更新 更多