对于每一行,提取与单元格中另一个值匹配的列名中的值 [英] For each row extract the value in the column name that match another value in the cell

查看:22
本文介绍了对于每一行,提取与单元格中另一个值匹配的列名中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,可以通过 for 循环轻松解决.但是,由于我在一个数据框中有十万行,这将需要很长时间的计算时间,因此我正在寻找一种快速而智能的解决方案.

I have a question which can be easily solved with a for-loop. However, since I have hundred-thousands rows in a dataframe, this would take very long computational time, and thus I am looking for a quick and smart solution.

对于数据框中的每一行,我想粘贴列名与第一列 (INDEX) 中的值匹配的单元格的值

For each row in my dataframe, I would like to paste the value of the cell whose column name matches the one from the first column (INDEX)

数据框看起来像这样

> mydata
  INDEX    1   2    3   4    5   6
1     2 18.9 9.5 22.6 4.7 16.2 7.4
2     2 18.9 9.5 22.6 4.7 16.2 7.4
3     2 18.9 9.5 22.6 4.7 16.2 7.4
4     4 18.9 9.5 22.6 4.7 16.2 7.4
5     4 18.9 9.5 22.6 4.7 16.2 7.4
6     5 18.9 9.5 22.6 4.7 16.2 7.4

这是重现它的代码:

mydata <- data.frame(INDEX=c(2,2,2,4,4,5), ONE=(rep(18.9,6)), TWO=(rep(9.5,6)), 
                     THREE=(rep(22.6,6)), FOUR=(rep(4.7,6)), FIVE=(rep(16.2,6)), SIX=(rep(7.4,6)))
colnames(mydata) <- c("INDEX",1,2,3,4,5,6)

这是带有新计算变量的新数据框:

And this is the new dataframe with the newly calculated variable:

> new_mydf
  INDEX    1   2    3   4    5   6 VARIABLE
3     2 18.9 9.5 22.6 4.7 16.2 7.4      9.5
2     2 18.9 9.5 22.6 4.7 16.2 7.4      9.5
1     2 18.9 9.5 22.6 4.7 16.2 7.4      9.5
5     4 18.9 9.5 22.6 4.7 16.2 7.4      4.7
4     4 18.9 9.5 22.6 4.7 16.2 7.4      4.7
6     5 18.9 9.5 22.6 4.7 16.2 7.4     16.2

我使用下面的 for 循环解决了它,但是,正如我上面写的,我正在寻找一个更直接的解决方案(也许使用像 dplyr 这样的包,或其他函数?),因为循环对我来说很慢扩展数据集

I solved it using the for-loop here below, but, as I wrote above, I am looking for a more straightforward solution (maybe using packages like dplyr, or other functions?), as the loop is to slow for my extended dataset

id = mydata$INDEX
new_mydf <- data.frame()
for (i in 1:length(id)) {
  mydata_row <- mydata[i,]
  value <- mydata_row$INDEX
  mydata_row["VARIABLE"] <- mydata_row[,names(mydata_row) == value]
  new_mydf <- rbind(mydata_row,new_mydf)
}
new_mydf <- new_mydf[ order(new_mydf[,1]), ] 

推荐答案

根据您的循环,将 apply 与匿名函数一起使用可能会更快(使用您的 mydata> 初始定义):

Based on your loop, this use of apply with an anonymous function may be faster (with your mydata initial definition) :

mydata$VARIABLE<-apply(mydata, 1, function(x) { x[names(x)==x[names(x)=="INDEX"]] })

它甚至可以在字符中使用 INDEX :

Edit : And it works even with INDEX in characters :

mydata <- data.frame(INDEX=c("B","B","B","D","D","E"), "A"=(rep(18.9,6)), "B"=(rep(9.5,6)), 
                 "C"=(rep(22.6,6)), "D"=(rep(4.7,6)), "E"=(rep(16.2,6)), "F"=(rep(7.4,6)))

mydata$VARIABLE<-apply(mydata, 1, function(x) { x[names(x)==x[names(x)=="INDEX"]] })

<代码>>我的数据索引 A B C D E F 变量1 B 18.9 9.5 22.6 4.7 16.2 7.4 9.52 乙 18.9 9.5 22.6 4.7 16.2 7.4 9.53 B 18.9 9.5 22.6 4.7 16.2 7.4 9.54 D 18.9 9.5 22.6 4.7 16.2 7.4 4.75 D 18.9 9.5 22.6 4.7 16.2 7.4 4.76 E 18.9 9.5 22.6 4.7 16.2 7.4 16.2

这篇关于对于每一行,提取与单元格中另一个值匹配的列名中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆