每个索引的子集或过滤器 data.frame 例如每行逐列
Subset or filter data.frame per indices e.g. column-wise per row
假设你有这样一个 data.frame:
df <- data.frame(matrix(1:12, 4))
df
X1 X2 X3
1 1 5 9
2 2 6 10
3 3 7 11
4 4 8 12
必须按这些列索引按行过滤:
b=c(2,1,3,2)
所以预期的输出应该是这样的:
c(5, 2, 11, 8)
显然,使用以下方法不是解决方案。
df[ 1:nrow(df), b]
到目前为止,我正在使用 mapply 的一种有效方法:
mapply(function(x, y) x[y], as.data.frame(t(df)), b, USE.NAMES = F)
[1] 5 2 11 8
但我想知道是否有更优雅的解决方案?
您可以使用数字矩阵索引;检查 ?"[ 部分下的 矩阵和数组 :
A third form of indexing is via a numeric matrix with the one column
for each dimension: each row of the index matrix then selects a single
element of the array, and the result is a vector. Negative indices are
not allowed in the index matrix. NA and zero values are allowed: rows
of an index matrix containing a zero are ignored, whereas rows
containing an NA produce an NA in the result.
原始数据框有2个维度,所以可以构造一个两列的索引矩阵,第一列代表行索引,第二列代表列索引,每对从中提取一个元素文档中所述的数据框:
b=c(2,1,3,2)
df[cbind(seq_len(nrow(df)), b)]
# [1] 5 2 11 8
假设你有这样一个 data.frame:
df <- data.frame(matrix(1:12, 4))
df
X1 X2 X3
1 1 5 9
2 2 6 10
3 3 7 11
4 4 8 12
必须按这些列索引按行过滤:
b=c(2,1,3,2)
所以预期的输出应该是这样的:
c(5, 2, 11, 8)
显然,使用以下方法不是解决方案。
df[ 1:nrow(df), b]
到目前为止,我正在使用 mapply 的一种有效方法:
mapply(function(x, y) x[y], as.data.frame(t(df)), b, USE.NAMES = F)
[1] 5 2 11 8
但我想知道是否有更优雅的解决方案?
您可以使用数字矩阵索引;检查 ?"[ 部分下的 矩阵和数组 :
A third form of indexing is via a numeric matrix with the one column for each dimension: each row of the index matrix then selects a single element of the array, and the result is a vector. Negative indices are not allowed in the index matrix. NA and zero values are allowed: rows of an index matrix containing a zero are ignored, whereas rows containing an NA produce an NA in the result.
原始数据框有2个维度,所以可以构造一个两列的索引矩阵,第一列代表行索引,第二列代表列索引,每对从中提取一个元素文档中所述的数据框:
b=c(2,1,3,2)
df[cbind(seq_len(nrow(df)), b)]
# [1] 5 2 11 8