匹配列到列名,将值添加到匹配的行/列

发布于 2025-02-13 18:02:28 字数 1227 浏览 0 评论 0原文

匹配列与列名,将值添加到匹配的行/列

我的第一个问题,因此请务必教给我一个

给定的数据框的

df<- structure(list(ID = c("ID001", "ID001", "ID003", "ID004", "ID003", 
                      "ID004"), ID001 = c(1L, 0L, 1L, 0L, 1L, 1L), ID002 = c(0L, 
                        0L, 0L, 0L, 0L, 0L), ID003 = c(1L, 0L, 1L, 1L, 0L, 0L), ID004 = c(1L, 
                           1L, 0L, 0L, 1L, 1L)), class = "data.frame", row.names = c(NA, -6L)) 



   ID     ID001 ID002 ID003 ID004
1 ID001     1     0     1     1
2 ID001     0     0     0     1
3 ID003     1     0     1     0
4 ID004     0     0     1     0
5 ID003     1     0     0     1
6 ID004     1     0     0     1

课程,我的循环效率低以更新“ ID”列与列名称匹配的条目,我们添加到值

for(rows in 1:nrow(df)) {
  df[rows, match(df[rows,'ID'], names(df))] <- df[rows, match(df[rows,'ID'], names(df))] + 1
}

df

     ID ID001 ID002 ID003 ID004
1 ID001     2     0     1     1
2 ID001     1     0     0     1
3 ID003     1     0     2     0
4 ID004     0     0     1     1
5 ID003     1     0     1     1
6 ID004     1     0     0     2

这是所需的输出。但是我需要在数百万的行和慢速上运行它。我猜这可以通过应用程序或类似方式改进多种方法,但是我没有尝试过,希望看到它的完成方式。

Match Column to Column Names, add value to row/column of Matches

My first question, so be sure to teach me a lesson

Given Data Frame

df<- structure(list(ID = c("ID001", "ID001", "ID003", "ID004", "ID003", 
                      "ID004"), ID001 = c(1L, 0L, 1L, 0L, 1L, 1L), ID002 = c(0L, 
                        0L, 0L, 0L, 0L, 0L), ID003 = c(1L, 0L, 1L, 1L, 0L, 0L), ID004 = c(1L, 
                           1L, 0L, 0L, 1L, 1L)), class = "data.frame", row.names = c(NA, -6L)) 



   ID     ID001 ID002 ID003 ID004
1 ID001     1     0     1     1
2 ID001     0     0     0     1
3 ID003     1     0     1     0
4 ID004     0     0     1     0
5 ID003     1     0     0     1
6 ID004     1     0     0     1

I have an inefficient for loop for updating entries where the 'ID' column is matching a column name, we add to the value

for(rows in 1:nrow(df)) {
  df[rows, match(df[rows,'ID'], names(df))] <- df[rows, match(df[rows,'ID'], names(df))] + 1
}

df

     ID ID001 ID002 ID003 ID004
1 ID001     2     0     1     1
2 ID001     1     0     0     1
3 ID003     1     0     2     0
4 ID004     0     0     1     1
5 ID003     1     0     1     1
6 ID004     1     0     0     2

this is the desired output. But I need to run this on millions of rows and its slow. I'm guessing this can be improved more than one way, maybe with apply or similar, but I've not attempted this and hoping to see how its done.

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

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

发布评论

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

评论(2

半暖夏伤 2025-02-20 18:02:28

这是dplyr方法:

library(dplyr)

df %>% 
  mutate(across(-ID, ~ifelse(ID == cur_column(), .+1, .)))
     ID ID001 ID002 ID003 ID004
1 ID001     2     0     1     1
2 ID001     1     0     0     1
3 ID003     1     0     2     0
4 ID004     0     0     1     1
5 ID003     1     0     1     1
6 ID004     1     0     0     2

Here is a dplyr way:

library(dplyr)

df %>% 
  mutate(across(-ID, ~ifelse(ID == cur_column(), .+1, .)))
     ID ID001 ID002 ID003 ID004
1 ID001     2     0     1     1
2 ID001     1     0     0     1
3 ID003     1     0     2     0
4 ID004     0     0     1     1
5 ID003     1     0     1     1
6 ID004     1     0     0     2
半仙 2025-02-20 18:02:28

使用行/列定位索引创建一个矩阵,然后进行分配

i1 <- cbind(seq_len(nrow(df)), match(df$ID, names(df)[-1]))
df[-1][i1] <- df[-1][i1] + 1

-OUTPUT

> df
     ID ID001 ID002 ID003 ID004
1 ID001     2     0     1     1
2 ID001     1     0     0     1
3 ID003     1     0     2     0
4 ID004     0     0     1     1
5 ID003     1     0     1     1
6 ID004     1     0     0     2

Create a matrix with row/column position index and then do the assignment

i1 <- cbind(seq_len(nrow(df)), match(df$ID, names(df)[-1]))
df[-1][i1] <- df[-1][i1] + 1

-output

> df
     ID ID001 ID002 ID003 ID004
1 ID001     2     0     1     1
2 ID001     1     0     0     1
3 ID003     1     0     2     0
4 ID004     0     0     1     1
5 ID003     1     0     1     1
6 ID004     1     0     0     2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文