简体   繁体   English

从R中给定的列名和行名的数据集中获取值

[英]Get value from dataset at given column name and row name in R

Now I have two dataframes. 现在我有两个数据框。 One of the is huge and acts like a library, the other one is small and empty, it only contains column names and row names, let's call it target . 其中一个很大,就像一个库,另一个又很小又空,它只包含列名和行名,我们称其为target Now I try to fill the target getting the corresponding values from the library . 现在,我尝试填充目标 ,以从库中获取相应的值。

Let's say target looks like this: 假设目标看起来像这样:

Ind   Dog  Cat  Fish  Bird
A     0    0    0     0
B     0    0    0     0
C     0    0    0     0 
D     0    0    0     0

What I want is something that achieves this: 我想要的是可以实现此目标的东西:

for(i in 1:dim(target[,1]) {
  for(j in 1:dim(target[1,]) {
      target[i,j]<-get(library, rowname=target[i,], colname=target[,j]
}}

based on your code, you can try something like this: 根据您的代码,您可以尝试执行以下操作:

for(i in 1:nrow(target){
  for(j in 1:ncol(target){
    target[i,j]<- library[  row.names(target)[i] , col.names(target)[j] ] 
  }}

I assume that library has corresponding row and column names. 我假设该library具有相应的行和列名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM