匹配列到列名,将值添加到匹配的行/列
匹配列与列名,将值添加到匹配的行/列
我的第一个问题,因此请务必教给我一个
给定的数据框的
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是
dplyr
方法:Here is a
dplyr
way:使用
行/列
定位索引创建一个矩阵,然后进行分配-OUTPUT
Create a matrix with
row/column
position index and then do the assignment-output