[英]In R, how do I select certain columns that contain a certain value in a certain row index?
Let's suppose we have a dataframe like such:假设我们有一个像这样的数据框:
X <- data.frame(A = c(1,3,1), B = c(2,3,9), C = c(3,4,8))
[A, B, C]
[1, 3, 1]
[2, 3, 9]
[3, 4, 8]
I only want to select columns where its value in row 3 is greater than 5. In this case, only B and C would be selected.我只想选择其在第 3 行中的值大于 5 的列。在这种情况下,只会选择 B 和 C。 Furthermore, I only want to reference that row by its index.
此外,我只想通过索引引用该行。 Like row 528, or 326.
像第 528 行或第 326 行。
What is the syntax for doing this?这样做的语法是什么? I checked online and it appeared dplyr's select would work... but I haven't figured out a good way to put it to use.
我在网上查了一下,似乎 dplyr 的 select 可以工作……但我还没有想出一个使用它的好方法。
You could use :你可以使用:
row <- 3
X[row, X[row, ] > 5]
# B C
#3 9 8
This select row number row
and columns where value in row
is greater than 5.这个选择行数
row
和列,其中价值row
大于5。
Using dplyr
:使用
dplyr
:
library(dplyr)
X %>% slice(row) %>% select_if(. > 5)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.