使用向量的r子集数组 [英] r subset array using vector
本文介绍了使用向量的r子集数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我觉得这个问题应该已经回答了,但是我没有找到.我有一个数组,我想使用向量将其子集化.我知道很难做到这一点,但是我敢肯定必须有一种简单的方法.有什么想法吗?
I feel like this question should have already been answered, but I found none. I have an array and I want to subset it using a vector. I know how to do it the hard way, but I'm sure there's got to be an easy way. Any ideas?
这是我的例子:
dat <- data.frame(a = rep(letters[1:3], 2), b = rep(letters[1:2], 3), c = c(rep("a", 5), "b"), x = rnorm(6), stringsAsFactors = FALSE)
l <- by(dat[ , "x"], dat[ , 1:3], mean)
l["a", "a", "a"] # works
l[c("a", "a", "a")] # does not work
所以我想我需要一种方法来将c()
包装器形式c("a", "a", "a")
传递给l
.
So I guess I need to a way to remove the c()
wrapper form c("a", "a", "a")
before passing it to l
.
推荐答案
可以使用矩阵代替向量:
Instead of a vector you could use a matrix:
l[matrix(rep("a", 3), nrow=1)]
这篇关于使用向量的r子集数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文