将行值与列名匹配,并在R中提取值 [英] Match row value with column name and extract value in R

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

问题描述

我有一个表,需要在其中选择数据"列中的所有值,与列名匹配,然后从该行中获取值.前任.数据列:选择A1,找到具有相同名称的列名称(A1)并取值(1),选择A3,找到具有相同名称的列名称(A3)并取值(11),然后将其打印到表格中.

I have a table where I need to choose all the values in the Data column, match with the column names and take the value from that row. Ex. Data column: choose A1, find column name with same name (A1 )and take the value (1), choose A3, find column name with same name (A3) and take the value (11) and then print it to a table.

有人可以帮我吗,我是R的新手,我不知道从哪里开始.

Can somebody help me, I am new using R and I do not know where to start.

A1  A2  A3   Data
1   5   9   A1  
2   6   10  A2
3   7   11  A3
4   8   12  A4

推荐答案

这怎么办?

df[cbind(1:4, match(df$Data, names(df)))]
[1] "1"  "6"  "11" NA

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

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