在哪个列中有特定变量的值

发布于 2025-02-10 16:21:42 字数 393 浏览 1 评论 0原文

我有此数据框:

a <- c(2,5,90,77,56,65,85,75,12,24,52,32)
b <- c(45,78,98,55,63,12,23,38,75,68,99,73)
c <- c(77,85,3,22,4,69,86,39,78,36,96,11)
d <- c(52,68,4,25,79,120,97,20,7,19,37,67)
e <- c(14,73,91,87,94,38,1,685,47,102,666,74)

df <- data.frame(a,b,c,d,e)

此变量:

bb <- 120

我需要知道变量“ BB”值的DF的列号。我该怎么办? 所有大家!

I have this dataframe:

a <- c(2,5,90,77,56,65,85,75,12,24,52,32)
b <- c(45,78,98,55,63,12,23,38,75,68,99,73)
c <- c(77,85,3,22,4,69,86,39,78,36,96,11)
d <- c(52,68,4,25,79,120,97,20,7,19,37,67)
e <- c(14,73,91,87,94,38,1,685,47,102,666,74)

df <- data.frame(a,b,c,d,e)

and this variable:

bb <- 120

I need to know the column number of df in which there is the value of the variable "bb". How can I do?
Thx everyone!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷荒 2025-02-17 16:21:42

我们可以使用使用带有arr.ind = true在创建逻辑矩阵后提取行/col索引。然后,提取第二列以获取列索引,

which(df == bb, arr.ind = TRUE)[,2]
col 
  4 

如果列中有重复的元素以进行比较,请用unique包装以返回唯一的列索引

unique(which(df == bb, arr.ind = TRUE)[,2])
[1] 4

We could use which with arr.ind = TRUE to extract the row/col index after creating a logical matrix. Then, extract the second column to get the column index

which(df == bb, arr.ind = TRUE)[,2]
col 
  4 

If there are duplicate elements in the column for the value compared, wrap with unique to return the unique column index

unique(which(df == bb, arr.ind = TRUE)[,2])
[1] 4
冷弦 2025-02-17 16:21:42

我认为我们可以使用grep

grep(bb, df)
[1] 4

I think we could use grep

grep(bb, df)
[1] 4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文