简体   繁体   English

在 R 中,如何选择在某个行索引中包含某个值的某些列?

[英]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.

相关问题 R 程序:select 包含某些单词的列 - R program: select columns that contain certain words 如何提取R中包含特定文本/字符串的列 - how do you extract the columns that contain a certain text/string in R 在R中,如何提取最接近某个值的数组中某个行的列号? - In R, how do I extract the column number of a certain row in an array that's closest to a certain value? 如何基于R中某一行的条件选择列 - How to select columns based on criteria in a certain row in R 如何创建一个新列,指示某些其他列是否包含给定值? - How do I create a new column indicating whether certain other columns contain a given value? 如果行中的另一个数字等于某个值,如何选择行中的特定值进行平均? - How do I select specific values in a row to average if another number in the row equals a certain value? R:如何获取包含特定单词及其关联索引号的列的列名? - R: How to get column names for columns that contain a certain word AND their associated index number? 如何选择满足特定条件的R数据帧中的第一行? - How do I select the first row in an R data frame that meets certain criteria? 选择不包含特定模式R正则表达式的字符串 - Select strings that do not contain a certain pattern R regex 如何根据相应列中的特定值替换特定列中的值? - How do I replace values in certain columns conditional on a certain value in corresponding columns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM