英文:
Matrix Slicing with Array in R
问题
考虑以下的2D数组:
A =
1 2 3
4 5 6
7 8 9
给定一组与列对应的索引,例如,i = [0, 2],我想要在R中获得一个仅包含第0列和第2列的新矩阵。
1 3
4 6
7 9
在Python中,我们可以使用以下代码行:A[:, i]
。然而,在R中,我不知道如何做到这一点。你能提供一个解决方案吗?
英文:
Consider the following 2D array:
A =
1 2 3
4 5 6
7 8 9
Given a set of indexes corresponding to the columns, for instance, i = [0, 2], I want to obtain a new matrix only with the 0th and 2nd column using R.
1 3
4 6
7 9
In Python, we can use the following line: A[:, i]. However, in R, I have no idea to do this. Could you provide me a solution?
答案1
得分: 1
在R中,数据结构中元素的索引从1开始。
A <- matrix(data = 1:9, nrow = 3, byrow = TRUE)
A[,-2]
或者
A[,c(1,3)]
英文:
In R, the index of elements in data structures starts at 1
A <- matrix(data = 1:9,nrow = 3,byrow = TRUE)
A[,-2]
or
A[,c(1,3)]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论