简体   繁体   English

如果第一列中的元素与另一个data.frame中的另一个元素匹配,如何替换数据帧第二列中的要素?

[英]How to replace elemets in second column of data frame if the element in first column match another element in another data.frame?

I want to replace characters in second column of a data.frame if their first column matches in another data.frame. 我想替换data.frame第二列中的字符,如果它们的第一列与另一个data.frame中的字符匹配。 So the files are like this: 所以文件是这样的:

File1: 文件1:

        Genotype          Group Type
1  08ZB02005DH01  08ZB02005DH01 Line
2  08ZB07005DH04  08ZB07005DH04 Line
3  08ZB08B06DH02  08ZB08B06DH02 Line
4  08ZB13005DH04  08ZB13005DH04 Line
5  08ZB18B24DH01  08ZB18B24DH01 Line
6 JRP4RA6121-002 JRP4RA6121-002 Line

File2: 文件2:

       Genotype      POL       RE       ZE
1 08ZB08B06DH02 102.6110 94.94690 97.33455
2 08ZB18B24DH01 102.3411 94.18070 96.29505
3 08ZB02005DH01 101.0357 95.59205 96.62890
4 08ZB13005DH04 101.9972 96.57825 98.62155
5 08ZB07005DH04 103.5134 92.92665 96.10605

This is the code that I use: 这是我使用的代码:

Group_code1 <- 'TS'                     
Group_code2 <- 'PS'
for (i in 1:dim(file1)[1]) {
    if ((match(file1[i,1],file2[,1], nomatch=0)) >= 1) {test[i,2]<-Group_code2} else { test[i,2]<-Group_code2}

  }

what I expect as output is: 我期望的输出是:

        Genotype Group Type
1  08ZB02005DH01  TS Line
2  08ZB07005DH04  TS Line
3  08ZB08B06DH02  TS Line
4  08ZB13005DH04  TS Line
5  08ZB18B24DH01  TS Line
6 JRP4RA6121-002  PS Line

but I get this error: 但是我得到这个错误:

Warning messages:
1: In `[<-.factor`(`*tmp*`, iseq, value = "PS") :
  invalid factor level, NA generated

I would like to keep this part of the code(or at least something similar): 我想保留这部分代码(或至少类似的东西):

Group_code1 <- 'TS'                     
Group_code2 <- 'PS'

thanks in advance 提前致谢

如果df1是您的第一个data.framedf2是第二个,则可以尝试:

df1$Group<-c("PS","TS")[ (df1$Group %in% df2$Genotype) +1]

An option using data.table 使用data.table的选项

library(data.table) 
setkey(setDT(df1), Group)[df2[,1, drop=FALSE],
            Group:='TS'][Group!='TS', Group:='PS'][]
#         Genotype Group Type
#1:  08ZB02005DH01    TS Line
#2:  08ZB07005DH04    TS Line
#3:  08ZB08B06DH02    TS Line
#4:  08ZB13005DH04    TS Line
#5:  08ZB18B24DH01    TS Line
#6: JRP4RA6121-002    PS Line

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

相关问题 将data.frame的一列与另一data.frame中的所有列进行匹配 - Match one column of a data.frame with all the columns in another data.frame 根据另一个data.frame替换data.frame中的某些列值 - replace some column values from a data.frame based on another data.frame 如何在另一个data.frame中替换用“/”分隔的字符串以匹配它们? - How to replace strings separated by "/" for their match in another data.frame? 如何将一列与另一列的出现次数一起添加到data.frame - how add a column to a data.frame with the occurence number of an another column 使用data.frame中的一列选择另一个 - Use a column in a data.frame to select another 计算元素在data.frame列中出现的次数 - Counting how many times an element occurs in the column of a data.frame R如何使用另一个data.frame中的值更新data.frame中的列 - R How to update a column in data.frame using values from another data.frame 比较数据框中某列中的元素与另一数据框中同一列中的另一个元素以获取R中的对应行 - comparing an element in a column in a data frame with another element in the same column in another data frame for corresponding rows in R 如何为另一个数据框中的元素指定列名 - How to assign a column name to an element in another data frame 如何将 data.frame 中的变量循环到另一个列中 - How to loop variables from a data.frame into another into a single column
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM