简体   繁体   English

在 R 中,如何将一个数据框中选定行的值与另一个数据框中选定的列匹配?

[英]In R, how do I match values from selected rows in one data frame with selected columns in another?

I have two separate dataframes.我有两个单独的数据框。 A few of the columns from one dataframe have the same name as some rows in the other.一个数据框中的一些列与另一个数据框中的某些行具有相同的名称。 How can I match values corresponding to a few rows in one df to values corresponding to a few columns (with the same names as the rows) in another df?如何将对应于一个 df 中几行的值与对应于另一个 df 中几列(与行同名)的值匹配?

In this example, I want to know if for each row in the first df, the values (A/B/C/D) under each question (E4Q_) matches the value at row E4Q_ and column 'Answer' in df2.在此示例中,我想知道对于第一个 df 中的每一行,每个问题 (E4Q_) 下的值 (A/B/C/D) 是否与 df2 中 E4Q_ 行和“Answer”列的值匹配。 After this, I want to either print TRUE/FALSE or 1/0 in a third data frame which is set up like the first.在此之后,我想在第三个数据框中打印 TRUE/FALSE 或 1/0,该数据框中的设置与第一个类似。

df1:
Name E4Q1 E4Q2 E4Q3 E4Q4 E4Q5 
XYZ  B    B    A    A    B
DEF  D    D    D    C    B
GHJ  -    -    B    A    C

    df2:
Question Answer
E4Q1     B
E4Q2     D
E4Q3     C
E4Q4     A
E4Q5     C

df3:
Name E4Q1 E4Q2 E4Q3 E4Q4 E4Q5 
XYZ  1    0    0    1    0
DEF  0    1    0    0    0
GHJ  0    0    0    1    1

If you are using the current version of R you have stringsAsFactos=FALSE as the default.如果您使用的是 R 的当前版本,则默认使用 stringsAsFactos=FALSE。 This task is an excellent example of why that change was made.这个任务是一个很好的例子,说明为什么要做出改变。 Factos would complicate this process completely. Factos 会使这个过程完全复杂化。 If you are like me you need to do the data entry with that explicitly set:如果您像我一样,则需要使用明确设置的数据输入:

 df1 <- read.table(text= "Name E4Q1 E4Q2 E4Q3 E4Q4 E4Q5 
 XYZ  B    B    A    A    B
 DEF  D    D    D    C    B
 GHJ  -    -    B    A    C", head=TRUE,stringsAsFactors=FALSE)
 
 df2 <- read.table(text=" Question Answer
 E4Q1     B
 E4Q2     D
 E4Q3     C
 E4Q4     A
 E4Q5     C", head=TRUE,stringsAsFactors=FALSE)

df3 <- df1 # same structure but Question columns will be changed to 1/0

First try : loop through col names and compare results to correct values in df2:首先尝试:遍历列名称并将结果与​​ df​​2 中的正确值进行比较:

 for( i in names(df1[-1]) ){ df3[[i]] <- df1[[i]] == df2[df2$Question==i, 2] }
 df3
  Name  E4Q1  E4Q2  E4Q3  E4Q4  E4Q5
1  XYZ  TRUE FALSE FALSE  TRUE FALSE
2  DEF FALSE  TRUE FALSE FALSE FALSE
3  GHJ FALSE FALSE FALSE  TRUE  TRUE

So need to coerce to numeric, either with as.numeric or by adding 0.所以需要强制转换为数字,要么使用 as.numeric 要么添加 0。

 for( i in names(df1[-1]) ){ df3[[i]] <- ( df1[[i]] == df2[df2$Question==i, 2])+0 }
 df3
  Name E4Q1 E4Q2 E4Q3 E4Q4 E4Q5
1  XYZ    1    0    0    1    0
2  DEF    0    1    0    0    0
3  GHJ    0    0    0    1    1
 

暂无
暂无

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

相关问题 将选定列中的值和一个数据框中的匹配行覆盖到另一个数据框中,R - Overwrite values from selected columns and matching rows from one data frame into another, R 如何在数据框中按行特征选择的多个列和行上执行函数? - How do I perform a function over multiple columns and rows selected by row characteristics in a data frame? 如何将一个数据帧中的单个ID与R中另一个数据帧中的ID的倍数匹配? - How do I match single ID's in one data frame to multiples of the IDs in another data frame in R? 如果一行中的任何值与向量中的值匹配,我如何在 R 中对数据框中的行进行子集化? - How can I subset rows in a data frame in R if any value in one row match values in a vector? 在随机选择的行中更改 R 数据框中的值 - Changing values in an R data frame in randomly selected rows 如何在R中将一个df中的行与另一个df中的列进行匹配 - How to match rows from one df with columns in another df in R 计算从 R 中的列表元素中选择的特定行的数据框列的平均值 - Compute mean on data frame columns for specific rows selected from list elements in R 在 R 中,如何将值添加到一个值与另一个数据框的值匹配的行的末尾? - In R, how can I add values onto the end of rows where one value matches that of another data frame? 在R中,如何通过另一个data.frame中的值来子集data.frame? - In R, how do I subset a data.frame by values from another data.frame? 在 R 中,我如何 select 来自一个数据帧的所有变量在另一个数据帧中? - In R, how do I select all variables from one data frame that are in another data frame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM