creating new column based on rows being equal in R

早过忘川 提交于 2019-12-25 04:28:41

问题


Here is a simple question about creating a new column conditional on a row duplicate in one column matching criterion in different column. Specifically, if the row is a duplicate in column "pairs", create new column "new" based on rows in column "y" being equal/unequal.

In the actual data frame I have even more conditions for other columns but my main issue is with making these conditions dependent on the rows being the same in the "pairs" column.

Many thanks!

pairs y   new    

 1    1    1    
 1    0    1      
 2    1    0     
 2    1    0    
 3    3    1
 3    1    1

回答1:


Assuming values are always paired, i.e., there are only two row in each group:

DF <- read.table(text="pairs y   new    
1    1    1    
1    0    1      
2    1    0     
2    1    0    
3    3    1
3    1    1", header=TRUE)

library(plyr)
#for integers:
ddply(DF, .(pairs), transform, new1 = 1*(diff(y) != 0L))
#for numerics:
ddply(DF, .(pairs), transform, new1 = 1*(abs(diff(y)) > .Machine$double.eps ^ 0.5))


来源:https://stackoverflow.com/questions/22557675/creating-new-column-based-on-rows-being-equal-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!