按行查找数据框中某个值的所有列

发布于 2025-01-18 11:23:07 字数 283 浏览 3 评论 0原文

我试图在数据框中的每一行中找到具有特定数字的第一列和具有相同值的最后一列。如果数量为 4,请参阅示例数据和所需的输出。

示例数据

ID WZ_1 WZ_2 WZ_3 WZ_4
1  5    4    4    3 
2  4    4    3    3
3  4    4    4    4 

示例输出

ID First Last 
1  WZ_2  WZ_3
2  WZ_1  WZ_2
3  WZ_1  WZ_4 

I am trying to find the first column that has a specific number and the last column of the same value by each row in a dataframe. See example data and desired output if the number was 4.

Example Data

ID WZ_1 WZ_2 WZ_3 WZ_4
1  5    4    4    3 
2  4    4    3    3
3  4    4    4    4 

Example Output

ID First Last 
1  WZ_2  WZ_3
2  WZ_1  WZ_2
3  WZ_1  WZ_4 

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

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

发布评论

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

评论(3

梓梦 2025-01-25 11:23:07
library(data.table)

# dummy data
# use setDT(df) if yours isn't a datatable already
df <- data.table(id = 1:3
                 , a = c(4,4,0)
                 , b = c(0,4,0)
                 , c = c(4,0,4)
                 ); df
   id a b c
1:  1 4 0 4
2:  2 4 4 0
3:  3 0 0 4

# find 1st & last column with target value
df[, .(id
       , first = apply(.SD, 1, \(i) names(df)[min(which(i==4))])
       , last = apply(.SD, 1, \(i) names(df)[max(which(i==4))])
       )
   ]
library(data.table)

# dummy data
# use setDT(df) if yours isn't a datatable already
df <- data.table(id = 1:3
                 , a = c(4,4,0)
                 , b = c(0,4,0)
                 , c = c(4,0,4)
                 ); df
   id a b c
1:  1 4 0 4
2:  2 4 4 0
3:  3 0 0 4

# find 1st & last column with target value
df[, .(id
       , first = apply(.SD, 1, \(i) names(df)[min(which(i==4))])
       , last = apply(.SD, 1, \(i) names(df)[max(which(i==4))])
       )
   ]
倒数 2025-01-25 11:23:07

使用max.col

data.frame(ID = df$ID,
           First = names(df)[max.col(df == 4, ties.method = "first")],
           Last = names(df)[max.col(df == 4, ties.method = "last")])

  ID First Last
1  1  WZ_2 WZ_3
2  2  WZ_1 WZ_2
3  3  WZ_1 WZ_4

数据

df <- read.table(header= T, text= "ID WZ_1 WZ_2 WZ_3 WZ_4
1  5    4    4    3 
2  4    4    3    3
3  4    4    4    4 ")

With max.col:

data.frame(ID = df$ID,
           First = names(df)[max.col(df == 4, ties.method = "first")],
           Last = names(df)[max.col(df == 4, ties.method = "last")])

  ID First Last
1  1  WZ_2 WZ_3
2  2  WZ_1 WZ_2
3  3  WZ_1 WZ_4

data

df <- read.table(header= T, text= "ID WZ_1 WZ_2 WZ_3 WZ_4
1  5    4    4    3 
2  4    4    3    3
3  4    4    4    4 ")
扶醉桌前 2025-01-25 11:23:07

这是一个didyverse选项,我在其中放入了长表格,然后filter仅保留4的值,并且仅保留第一个和最后的出现。然后,我创建一个新列来表示它是第一个值还是最后一个值,然后将其转移回宽格式。

library(tidyverse)

df %>% 
  pivot_longer(-ID) %>% 
  group_by(ID) %>% 
  filter(value == 4) %>% 
  filter(row_number()==1 | row_number()==n()) %>% 
  mutate(col = c("First", "Last")) %>% 
  pivot_wider(names_from = "col", values_from = "name") %>% 
  select(-value)

输出

  <int> <chr> <chr>
1     1 WZ_2  WZ_3 
2     2 WZ_1  WZ_2 
3     3 WZ_1  WZ_4 

数据

df <- structure(list(ID = 1:3, WZ_1 = c(5L, 4L, 4L), WZ_2 = c(4L, 4L, 
4L), WZ_3 = c(4L, 3L, 4L), WZ_4 = c(3L, 3L, 4L)), class = "data.frame", row.names = c(NA, 
-3L))

Here's a tidyverse option, where I put into long form, then filter to keep only the values with 4 and only the first and last occurrence. Then, I create a new column to denote whether it is the first or last value, then pivot back to the wide format.

library(tidyverse)

df %>% 
  pivot_longer(-ID) %>% 
  group_by(ID) %>% 
  filter(value == 4) %>% 
  filter(row_number()==1 | row_number()==n()) %>% 
  mutate(col = c("First", "Last")) %>% 
  pivot_wider(names_from = "col", values_from = "name") %>% 
  select(-value)

Output

  <int> <chr> <chr>
1     1 WZ_2  WZ_3 
2     2 WZ_1  WZ_2 
3     3 WZ_1  WZ_4 

Data

df <- structure(list(ID = 1:3, WZ_1 = c(5L, 4L, 4L), WZ_2 = c(4L, 4L, 
4L), WZ_3 = c(4L, 3L, 4L), WZ_4 = c(3L, 3L, 4L)), class = "data.frame", row.names = c(NA, 
-3L))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文