Skip to content
Prev 342822 / 398506 Next

Compare data in two rows and replace objects in data frame

You could try data.table

#dat is the dataset


library(data.table)
v1 <- setNames(c("HT", "A", "B", "Aht", "Bht"), c("11", "10", "01", "1-", "-1"))
dat2 <- setDT(dat1)[, lapply(.SD, function(x) v1[paste(x, collapse="")]), by=CloneID]

A.K.
On Monday, August 4, 2014 5:55 AM, raz <barvazduck at gmail.com> wrote:
Dear all,

I have a data frame 144 x 20000 values.
I need to take every value in the first row and compare to the second row,
and the same for rows 3-4 and 5-6 and so on.
the output should be one line for each of the two row comparison.
the comparison is:
if row1==1 and row2==1 <-'HT'
if row1==1 and row2==0 <-'A'
if row1==0 and row2==1 <-'B'
if row1==1 and row2=='-' <-'Aht'
if row1=='-' and row2==1 <-'Bht'

for example:
if the data is:
CloneID? ? genotype 2001? ? genotype 2002? ? genotype 2003
2471250? ? 1? ? 1? ? 1
2471250? ? 0? ? 0? ? 0
2433062? ? 0? ? 0? ? 0
2433062? ? 1? ? 1? ? 1
100021605? ? 1? ? 1? ? 0
100021605? ? 1? ? 0? ? 1
100005599? ? 1? ? 1? ? 0
100005599? ? 1? ? 1? ? 1
100002798? ? 1? ? 1? ? 0
100002798? ? 1? ? 1? ? 1

then the output should be:
CloneID? ? genotype 2001? ? genotype 2002? ? genotype 2003
2471250? ? A? ? A? ? A
2433062? ? B? ? B? ? B
100021605? ? HT? ? A? ? B
100005599? ? HT? ? HT? ? B
100002798? ? HT? ? HT? ? B

I tried this for the whole data, but its so slow:

AX <- data.frame(lapply(AX, as.character), stringsAsFactors=FALSE)


for (i in seq(1,nrow(AX),by=2)){
for (j in 6:144){
if (AX[i,j]==1 & AX[i+1,j]==0){
AX[i,j]<-'A'
}
if (AX[i,j]==0 & AX[i+1,j]==1){
AX[i,j]<-'B'
}
if (AX[i,j]==1 & AX[i+1,j]==1){
AX[i,j]<-'HT'
}
if (AX[i,j]==1 & AX[i+1,j]=="-"){
AX[i,j]<-'Aht'
}
if (AX[i,j]=="-" & AX[i+1,j]==1){
AX[i,j]<-'Bht'
}
}
}

AX1<-AX[!duplicated(AX[,3]),]
AX2<-AX[duplicated(AX[,3]),]

Thanks for any help,

Raz