【发布时间】: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