如何使用某些特定条件填充新列

时间:2016-10-19 09:38:39

标签: r if-statement vlookup cbind

Table Details with new column position

我有这个表包含两列(1st = names,2nd = Positions *)。我想添加标题为" GeneName"的第3列。它应该具有根据$ position列的值。好像位置值落在任何下面提到的类中,相关的GeneName应写在新列中。新列中要填写的3个名称是NIb,VPg和CI。 条件是

NIb = from position 1014 to 2410
VPg = 3660 to 5563 and 
CI  = 5718 to 6282

请参阅随附的图片以便更好地理解。

1 个答案:

答案 0 :(得分:0)

我使用ifelse语句多次这样做。

fg$GeneName<-ifelse(fg$position_by_miRanda>=1014 & fg$position_by_miRanda<=2410,"NIb",fg$GeneName)

fg$GeneName<-ifelse(fg$position_by_miRanda>=3660 & fg$position_by_miRanda<=5563,"CI",fg$GeneName)

fg$GeneName<-ifelse(fg$position_by_miRanda>=5718 & fg$position_by_miRanda<=6282,"VPg",fg$GeneName)